首页 > 开发 > 综合 > 正文

用VisualC#编写仿MSNMessager的滚动提示窗口

2020-02-03 13:44:41
字体:
来源:转载
供稿:网友

目录
引言
实现方法
作者简介

--------------------------------------------------------------------------------

引言
大家一定都用过msn messager了吧?每当有新邮件或者是新消息到来的时候,msn messager便会从右下角升起一个小窗口提醒您,然后又降下去。当你在聚精会神的在电脑上做一件事的时候,一定不会喜欢突然被"咚"一下出现在屏幕中心的对话框打扰,它的这种设计不但非常体贴用户,而且效果还很酷。如果您写了一个程序驻留在后台并要求在需要的时候会提醒用户,并且希望也能实现这种效果,那么请跟我一步一步来做下图所示的这个仿msn messager的滚动提示窗口。

--------------------------------------------------------------------------------

实现方法
效果示例图


第一步,建立一个windows application,然后在主form中放置一个button,如下图所示:


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


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

public int heightmax

{

 set

 {

  heightmax = value; 

 }

 get

 {

  return heightmax;

 }

}

 

public int widthmax

{

 set

 {

  widthmax = value;

 }

 get

 {

  return widthmax;

 }

}

添加一个scrollshow的公共方法:
public void scrollshow()

{

 this.width = widthmax;

 this.height = 0;

 this.show();

 this.timer1.enabled = true;

}

添加一个staytime属性设置窗体停留时间(默认为5秒):
public int staytime = 5000;

添加scrollup和scrolldown方法来编写窗体如何滚出和滚入:
private void scrollup()

{

 if(height < heightmax)

 {

  this.height += 3;

  this.location = new point(this.location.x, this.location.y - 3);

 }

 else

 {

  this.timer1.enabled = false;

  this.timer2.enabled = true;

 }

}

 

private void scrolldown()

{

 if(height > 3)

 {

  this.height -= 3;

  this.location = new point(this.location.x, this.location.y + 3);

 }

 else

 {

  this.timer3.enabled = false;

  this.close();

 }

}

 

在三个timer的tick方法中分别写入:
private void timer1_tick(object sender, system.eventargs e)

{

 scrollup();

}

 

private void timer2_tick(object sender, system.eventargs e)

{

 timer2.enabled = false;

 timer3.enabled = true;

 

private void timer3_tick(object sender, system.eventargs e)

{

 scrolldown();

}

在form2的load事件中初始化窗体变量:
private void form2_load(object sender, system.eventargs e)

{

 screen[] screens = screen.allscreens;

 screen screen = screens[0];//获取屏幕变量

 this.location = new point(screen.workingarea.width - widthmax - 20, screen.workingarea.height - 34);//workingarea为windows桌面的工作区

 this.timer2.interval = staytime;

}

 

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

{

 form2 form = new form2();

 form.heightmax = 120;//窗体滚动的高度

 form.widthmax = 148;//窗体滚动的宽度

 form.scrollshow();

}

编译并运行程序,点击按纽,怎么样?是不是跟msn messager的效果一样,很酷吧?:)

--------------------------------------------------------------------------------

作者简介
作者:卢彦
passport: [email protected]

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表