private void button2_Click(object sender, EventArgs e) { Form1 frmShowWarning = new Form1();//Form1为要弹出的窗体(提示框), Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width-frmShowWarning.Width, Screen.PrimaryScreen.WorkingArea.Height); frmShowWarning.PointToScreen(p); frmShowWarning.Location = p; frmShowWarning.Show(); for (int i = 0; i <= frmShowWarning.Height; i++) { frmShowWarning.Location = new Point(p.X, p.Y - i); Thread.Sleep(10);//将线程沉睡时间调的越小升起的越快 } }
若是要让提示窗过一段时间慢慢的消失,则能够在Form1中放一个timer计时器,设定他执行的频率为2000,Form1的load事件中timer1.Enable=true,当此弹出框load过2秒后timer1开始工做,在timer1事件中代码:this
private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = false; for (int i = 0; i <= this.Height; i++) { Point p = new Point(this.Location.X, this.Location.Y + i);//弹出框向下移动消失 this.PointToScreen(p);//即时转换成屏幕坐标 this.Location = p;// new Point(this.Location.X, this.Location.Y + i); System.Threading.Thread.Sleep(10);//线程睡眠时间调的越小向下消失的速度越快。 } this.Close();//记得关闭此弹出框哦。OK }
若是想让弹出窗,过了半分钟开始渐渐透明一直到关闭,又想在透明阶段鼠标移上去弹出窗显示不透明则操做以下:线程
一、在弹出窗上添加两个timer控件。执行代码以下:code
timer2的事件中:orm
/// <summary> /// /// 判断鼠标是否是还在弹出框上,若是不是则timer1又能够开始工做了 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer2_Tick(object sender, EventArgs e) { timer1.Enabled = false;//timer1中止工做 this.Opacity = 1;//弹出窗透明度设置为1,彻底不透明 if (System.Windows.Forms.Control.MousePosition.X < this.Location.X && System.Windows.Forms.Control.MousePosition.Y < this.Location.Y)//以下 { timer1.Enabled = true; timer2.Enabled = false; } }
timer1的事件中代码:事件
private void timer1_Tick(object sender, EventArgs e) { timer2.Enabled = false;//中止timer2计时器, if (this.Opacity > 0 && this.Opacity <= 1)//开始执行弹出窗渐渐透明 { this.Opacity = this.Opacity - 0.05;//透明频度0.05 } if (System.Windows.Forms.Control.MousePosition.X >= this.Location.X && System.Windows.Forms.Control.MousePosition.Y >= this.Location.Y)//每次都判断鼠标是不是在弹出窗上,使用鼠标在屏幕上的坐标跟弹出窗体的屏幕坐标作比较。 { timer2.Enabled = true;//若是鼠标在弹出窗上的时候,timer2开始工做 timer1.Enabled = false;//timer1中止工做。 } if (this.Opacity == 0)//当透明度==0的时候,关闭弹出窗以释放资源。 { this.Close(); } }