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

①弹出信息框后慢慢下降消失

在主窗体中新增按钮重命名为btnShowAndDisappearMessages,在click事件中写如下代码:

private void btnShowAndDisappearMessages_Click(object sender, EventArgs e)
      {

            Messages ms = new Messages();//要弹出的消息框
            Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - ms.Width, Screen.PrimaryScreen.WorkingArea.Height);
            ms.PointToClient(p);
            ms.Location = p;
            ms.Show();
            for (int i = 0; i < ms.Height; i++)
            {
                ms.Location = new Point(p.X, p.Y - i);
                System.Threading.Thread.Sleep(10);
            }

      }

在Messages窗体中新增timer计时器,设定Interval为2000,并将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 + 1);

                System.Threading.Thread.Sleep(10);//线程睡眠时间调的越小向下消失的速度越快。
               
            }

            this.Close();

    this.Dispose();
        } 

 

②弹出信息框后渐渐变淡消失,鼠标移上去后再显示

 在主窗体新增按钮并重命名为btnMouseShowMessages写如下代码:

 

private void btnMouseShowMessages_Click(object sender, EventArgs e)
        {
            MouseShowMessages mm = new MouseShowMessages();//要弹出的消息框

            Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - mm.Width, Screen.PrimaryScreen.WorkingArea.Height);
            mm.PointToClient(p);
            mm.Location = p;
            mm.Show();
            for (int i = 0; i < mm.Height; i++)
            {
                mm.Location = new Point(p.X, p.Y - i);
                System.Threading.Thread.Sleep(10);
            }
        }

在MouseShowMessages窗体上新增两个timer控件。

其中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.1;//透明频度
            }
            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();

      this.Dispose();

            }

 

        }

timer2的事件代码如下:(控制检测鼠标位置)

 

 

         /// <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;
            }
        }

 

处理速度可以根据timer控件的Interval属性设置。

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

上一篇:C# 系统应用之窗体最小化至任务栏及常用操作


下一篇:学习游戏基础编程2:Win32分割窗口