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

本文介绍如何使用C#实现窗体的上升动画、下降动画及渐变透明效果,并通过定时器控制窗体的自动消失与透明度变化,同时加入鼠标悬停交互功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的事件中:

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


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值