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

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


### 回答1: 可以使用 System.Windows.Forms 命名空间中的 MessageBox.Show() 方法来弹出一个 MessageBox 对话。示例代码如下:MessageBox.Show("这是一个消息提示!", "标题", MessageBoxButtons.OK, MessageBoxIcon.Information); ### 回答2: 在WinForm中实现右下角弹出消息提示的程序可以通过以下步骤进行: 1. 创建WinForm应用程序 首先,我们需要创建一个WinForm应用程序。可以使用Visual Studio等集成开发环境来创建一个新的WinForm项目。 2. 设计主界面 在设计主界面时,我们可以使用一个隐藏的Panel作为消息提示的容器。这个Panel的初始位置应该设置在窗体右下角,并且默认状态下是不可见的。 3. 弹出提示 当需要显示提示时,可以在Panel中添加一个Label控件,用于显示消息内容。设置Label的字体、大小、颜色等样式,使其看起来像一个通知。 4. 动态显示提示 在显示提示时,我们需要将Panel从默认不可见状态切换到可见状态,并适当调整Panel的位置,使其显示在窗体右下角。 可以通过以下代码实现Panel的动态显示: ```csharp panel.Visible = true; panel.BringToFront(); panel.Left = this.Width - panel.Width - 20; // 距离窗体右边缘20个像素 panel.Top = this.Height - panel.Height - 20; // 距离窗体底部20个像素 ``` 5. 设置自动隐藏 为了实现消息提示自动隐藏,我们可以使用定时器控件来延时一段时间后隐藏Panel。 在Panel显示后,启动一个定时器,并在定时器的Tick事件中设置Panel的可见性为false。 可以通过以下代码实现定时器的使用: ```csharp Timer timer = new Timer(); timer.Interval = 3000; // 设置延时时间,单位为毫秒 timer.Tick += (sender, args) => { panel.Visible = false; timer.Stop(); }; timer.Start(); ``` 通过以上步骤,我们就可以在WinForm中实现一个右下角弹出消息提示的程序了。当需要显示消息时,只需向Panel中添加消息内容,并进行显示和定时隐藏即可。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值