第一步,建立一个Windows Application,然后在主form中放置一个Button,如下图所示:
第二步,给这个Application添加一个窗体(Form2),把窗体的FormBorderStyle属性设置为None(无边框模式),然后把TopMost属性(总在最上方)属性设置为True,把ShowInTaskbar属性(是否在 Windows 任务栏中显示窗体)设置为False,并在窗体上加上你打算要显示的文字(实际应用中一般是在程序中动态加载),将窗体的背景设置为你想要的图片和合适的大小。最后再放上三个Timer控件,其中,timer1控制窗体滚出的动画,timer2控制窗体停留时间,timer3控制窗体的滚入动画,将它们的Interval属性设置为10。参见下图
#p#第四步,编写代码,在Form2中添加两个属性用来设置窗体的显示大小:
private int heightMax, widthMax; public int HeightMax { set { heightMax = value; } get { return heightMax; } } public int WidthMax { set { widthMax = value; } get { return widthMax; } }
public void ScrollShow() { this.Width = widthMax; this.Height = 0; this.Show(); this.timer1.Enabled = true; }
public int StayTime = 5000;
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(); } }
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(); }
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; }
private void button1_Click(object sender, System.EventArgs e) { Form2 form = new Form2(); form.HeightMax = 120;//窗体滚动的高度 form.WidthMax = 148;//窗体滚动的宽度 form.ScrollShow(); }