Winforms实现类似QQ消息框一样的右下角消息提示窗口

实现方法   效果示例图

Winforms实现类似QQ消息框一样的右下角消息提示窗口1

 

 

第一步,建立一个Windows Application,然后在主form中放置一个Button

 

第 二步,给这个Application添加一个窗体(Form2),把窗体的FormBorderStyle属性设置为None(无边框模式),然后把 TopMost属性(总在最上方)属性设置为True,把ShowInTaskbar属性(是否在 Windows 任务栏中显示窗体)设置为False,并在窗体上加上你打算要显示的文字(实际应用中一般是在程序中动态加载),将窗体的背景设置为你想要的图片和合适的 大小。最后再放上三个Timer控件,其中,timer1控制窗体滚出的动画,timer2控制窗体停留时间,timer3控制窗体的滚入动画,将它们的 Interval属性设置为10。参见下图

 

 

Winforms实现类似QQ消息框一样的右下角消息提示窗口2

 

 

第四步,编写代码,在Form2中添加两个属性用来设置窗体的显示大小:

 

 

  1. private int heightMax, widthMax;  
  2. public int WidthMax  
  3. {  
  4.     get { return widthMax; }  
  5.     set { widthMax = value; }  
  6. }  
  7.   
  8. public int HeightMax  
  9. {  
  10.     get { return heightMax; }  
  11.     set { heightMax = value; }  
  12. }  



 

添加一个ScrollShow的公共方法:

 

 

  1.  public void ScrollShow()  
  2. {  
  3.     this.Width = widthMax;  
  4.     this.Height = 0;  
  5.     this.Show();  
  6.     this.timer1.Enabled = true;  
  7. }  



 

添加一个StayTime属性设置窗体停留时间(默认为5秒):

 

 

  1. public int StayTime = 5000;  



 

添加ScrollUp和ScrollDown方法来编写窗体如何滚出和滚入:

 

 

  1. private void ScrollUp()  
  2. {  
  3.     if (Height < heightMax)  
  4.     {  
  5.         this.Height += 3;  
  6.         this.Location = new Point(this.Location.X, this.Location.Y - 3);  
  7.     }  
  8.     else  
  9.     {  
  10.         this.timer1.Enabled = false;  
  11.         this.timer2.Enabled = true;  
  12.     }  
  13. }  
  14.   
  15. private void ScrollDwon()  
  16. {  
  17.     if (Height > 3)  
  18.     {  
  19.         this.Height -= 3;  
  20.         this.Location = new Point(this.Location.X, this.Location.Y + 3);  
  21.     }  
  22.     else  
  23.     {  
  24.         this.timer3.Enabled = false;  
  25.         this.Close();  
  26.     }  
  27. }  



 

在三个Timer的Tick方法中分别写入:

 

 

  1. private void timer1_Tick(object sender, EventArgs e)  
  2. {  
  3.     ScrollUp();  
  4. }  
  5.   
  6. private void timer2_Tick(object sender, EventArgs e)  
  7. {  
  8.     timer2.Enabled = false;  
  9.     timer3.Enabled = true;  
  10. }  
  11.   
  12. private void timer3_Tick(object sender, EventArgs e)  
  13. {  
  14.     ScrollDwon();  
  15. }  



 

在Form2的Load事件中初始化窗体变量:

 

 

  1.  private void frmMessage_Load(object sender, EventArgs e)  
  2. {  
  3.     Screen[] screens = Screen.AllScreens;  
  4.     Screen screen = screens[0]; //获取屏幕变量  
  5.     this.Location = new Point(screen.WorkingArea.Width - widthMax - 20, screen.WorkingArea.Height - 34); //WorkingArea为Windows桌面的工作区  
  6.     this.timer2.Interval = StayTime;  
  7. }  



 

好 了,滚动窗体的代码编写到这里就完成了,当然,它本身只实现了一个比较简单的窗体滚动滚出效果,具体如何去应用还应该配合你的程序来完成。当然,你还可以 为它添加更多的功能,比如从窗体的任意位置显示(这里只是从右下角显示),淡入淡出效果,加上声音等等。最常用的就是写一个托盘程序,然后采用这种提醒效 果。如何用C#编写托盘程序请参见:用Visual C#做托盘程序http://www.yesky.com/20020110/213425.shtml   最后,我们再回到Form1,在Button的Click事件中写如下代码来测试一下效果:

 

 

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     frmMessage message = new frmMessage();  
  4.     message.HeightMax = 120;  
  5.     message.WidthMax = 148;  
  6.     message.ScrollShow();  
  7. }  



 

编译并运行程序,点击按纽,跟MSN Messager的效果一样。

posted @ 2012-12-19 23:29  Net-Spider  阅读(471)  评论(0)    收藏  举报