winform C#屏幕右下角弹出消息框,自动消失

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事件中代码:
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
}
如果想让弹出窗,过了半分钟开始渐渐透明一直到关闭,又想在透明阶段鼠标移上去弹出窗显示不透明则操作如下:
1、在弹出窗上添加两个timer控件。执行代码如下:
timer2的事件中:
///


///
/// 判断鼠标是不是还在弹出框上,如果不是则timer1又可以开始工作了
///

///
///
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.Contr

ol.MousePosition.Y >= this.Location.Y)//每次都判断鼠标是否是在弹出窗上,使用鼠标在屏幕上的坐标跟弹出窗体的屏幕坐标做比较。
{
timer2.Enabled = true;//如果鼠标在弹出窗上的时候,timer2开始工作
timer1.Enabled = false;//timer1停止工作。
}
if (this.Opacity == 0)//当透明度==0的时候,关闭弹出窗以释放资源。
{
this.Close();
}
}

相关主题
相关文档
最新文档