C#标题式消息窗口
在我们的生活中,无论是使用QQ,360安全卫士等软件的过程中,经常会遇到从右下方的托盘处弹出
一个消息提示窗,比如新闻,产品更新等内容。那么它们是如何实现的呢?
下面来做个简单的Demo介绍一下。
首先制作一个窗体,如下图:
主要代码事件:
1 private void btnTest_Click(object sender, EventArgs e)
2 {
3 TestSmallWindow form = new TestSmallWindow();
4 form.HeightMax = 120;//窗体滚动的高度
5 form.WidthMax = 148;//窗体滚动的宽度
6 form.ScrollShow();
7 }
2 {
3 TestSmallWindow form = new TestSmallWindow();
4 form.HeightMax = 120;//窗体滚动的高度
5 form.WidthMax = 148;//窗体滚动的宽度
6 form.ScrollShow();
7 }
之后会弹出第二个窗体,也就是消息提示窗体:
其实这也是一个窗体,是一个不规则的Windows窗体,那么在这个窗体中,我们需要处理那些事件呢?
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9 namespace MSNMessageForm
10 {
11 public partial class TestSmallWindow : Form
12 {
13 private int heightMax, widthMax;
14 public int StayTime = 3000;//窗体停留时间(默认为5秒)
15 //定义窗体大小
16 public int HeightMax
17 {
18 set
19 {
20 heightMax = value;
21 }
22 get
23 {
24 return heightMax;
25 }
26 }
27
28 public int WidthMax
29 {
30 set
31 {
32 widthMax = value;
33 }
34 get
35 {
36 return widthMax;
37 }
38 }
39 public TestSmallWindow()
40 {
41 InitializeComponent();
42 }
43
44 private void button1_Click(object sender, EventArgs e)
45 {
46 this.Close();
47 }
48
49 private void TestSmallWindow_Load(object sender, EventArgs e)
50 {
51 Screen[] screens = Screen.AllScreens;
52 Screen screen = screens[0];//获取屏幕变量
53 this.Location = new Point(screen.WorkingArea.Width - widthMax - this.Width, screen.WorkingArea.Height - this.Height);
54 //WorkingArea为Windows桌面的工作区
55 this.timer2.Interval = StayTime;
56 }
57
58 public void ScrollShow()
59 {
60 this.Height = 0;
61 this.Show();
62 this.timer1.Enabled = true;
63 }
64 private void ScrollUp()
65 {
66 if (Height < heightMax)
67 {
68 this.Height += 3;
69 this.Location = new Point(this.Location.X, this.Location.Y - 3);
70 }
71 else
72 {
73 this.timer1.Enabled = false;
74 this.timer2.Enabled = true;
75 }
76 }
77
78 private void ScrollDown()
79 {
80 if (Height > 3)
81 {
82 this.Height -= 3;
83 this.Location = new Point(this.Location.X, this.Location.Y + 3);
84 }
85 else
86 {
87 this.timer3.Enabled = false;
88 this.Close();
89 }
90 }
91
92 private void timer3_Tick(object sender, EventArgs e)//设置由上向下自动消失窗体
93 {
94 ScrollDown();
95 }
96
97 private void timer2_Tick(object sender, EventArgs e)//
98 {
99 timer2.Enabled = false;
100 timer3.Enabled = true;
101 }
102
103 private void timer1_Tick(object sender, EventArgs e)//此处设置由下向上弹出我们制造的窗体
104 {
105 ScrollUp();
106 }
107 }
108 }
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9 namespace MSNMessageForm
10 {
11 public partial class TestSmallWindow : Form
12 {
13 private int heightMax, widthMax;
14 public int StayTime = 3000;//窗体停留时间(默认为5秒)
15 //定义窗体大小
16 public int HeightMax
17 {
18 set
19 {
20 heightMax = value;
21 }
22 get
23 {
24 return heightMax;
25 }
26 }
27
28 public int WidthMax
29 {
30 set
31 {
32 widthMax = value;
33 }
34 get
35 {
36 return widthMax;
37 }
38 }
39 public TestSmallWindow()
40 {
41 InitializeComponent();
42 }
43
44 private void button1_Click(object sender, EventArgs e)
45 {
46 this.Close();
47 }
48
49 private void TestSmallWindow_Load(object sender, EventArgs e)
50 {
51 Screen[] screens = Screen.AllScreens;
52 Screen screen = screens[0];//获取屏幕变量
53 this.Location = new Point(screen.WorkingArea.Width - widthMax - this.Width, screen.WorkingArea.Height - this.Height);
54 //WorkingArea为Windows桌面的工作区
55 this.timer2.Interval = StayTime;
56 }
57
58 public void ScrollShow()
59 {
60 this.Height = 0;
61 this.Show();
62 this.timer1.Enabled = true;
63 }
64 private void ScrollUp()
65 {
66 if (Height < heightMax)
67 {
68 this.Height += 3;
69 this.Location = new Point(this.Location.X, this.Location.Y - 3);
70 }
71 else
72 {
73 this.timer1.Enabled = false;
74 this.timer2.Enabled = true;
75 }
76 }
77
78 private void ScrollDown()
79 {
80 if (Height > 3)
81 {
82 this.Height -= 3;
83 this.Location = new Point(this.Location.X, this.Location.Y + 3);
84 }
85 else
86 {
87 this.timer3.Enabled = false;
88 this.Close();
89 }
90 }
91
92 private void timer3_Tick(object sender, EventArgs e)//设置由上向下自动消失窗体
93 {
94 ScrollDown();
95 }
96
97 private void timer2_Tick(object sender, EventArgs e)//
98 {
99 timer2.Enabled = false;
100 timer3.Enabled = true;
101 }
102
103 private void timer1_Tick(object sender, EventArgs e)//此处设置由下向上弹出我们制造的窗体
104 {
105 ScrollUp();
106 }
107 }
108 }
上述Code中的timer1,timer2,timer3均来自Timer组件,该组件用来控制时间显示,停留的时间问题。