Winform程序里的MessageBox实现展示3秒后自动关闭

创建了一个新的线程,在该线程中进行倒计时操作。在倒计时3秒后,通过调用CloseMessageBox方法来关闭MessageBox窗口。

由于MessageBox是一个模态对话框,无法直接通过代码关闭。在这里,我们使用了WinAPI中的FindWindowSendMessage方法来查找并关闭MessageBox窗口。

​
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form3 : Form
    {
        // 查找窗口
        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        // 发送消息
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        // 关闭消息
        private const uint WM_CLOSE = 0x0010;

        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ShowMessage("测试测试",2);
        }

        private void ShowMessage(string sMsg, int nSecondCount)
        {
            // 创建一个线程来执行倒计时操作
            Thread thread = new Thread(() =>
            {
                // 倒计时3秒
                Thread.Sleep(nSecondCount * 1000);

                // 关闭MessageBox
                if (InvokeRequired)
                {
                    Invoke(new Action(() => { CloseMessageBox(); }));
                }
                else
                {
                    CloseMessageBox();
                }
            });

            // 启动线程
            thread.Start();

            // 弹出MessageBox提示框,注意:这里的标题必须与下方查找关闭MessageBox里的标题一致。
            MessageBox.Show(sMsg, "完成提示");
        }

        private void CloseMessageBox()
        {
            // 查找并关闭MessageBox窗口
            IntPtr hwnd = FindWindow(null, "完成提示");//一致
            if (hwnd != IntPtr.Zero)
            {
                SendMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
            }
        }

        
    }
}

​

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
WinForm实现彻底关闭后等待2秒自动启动可以通过以下步骤来实现: 1. 创建一个Windows应用程序。 2. 在应用程序的FormClosing事件中,判断窗口是否是被用户手动关闭,如果是,则使用`System.Diagnostics.Process.Start(Application.ExecutablePath)`方法重新启动应用程序并退出当前进程。 3. 在应用程序的Main函数中,判断当前应用程序是否正在运行,如果正在运行,则等待2秒钟后重新启动应用程序。 示例代码如下: ```csharp static bool isRunning = false; static void Main() { // 判断当前应用程序是否正在运行 Mutex mutex = new Mutex(true, "MyApp", out isRunning); if (!isRunning) { // 等待2秒钟后重新启动应用程序 System.Threading.Thread.Sleep(2000); System.Diagnostics.Process.Start(Application.ExecutablePath); return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { // 判断窗口是否是被用户手动关闭 if (e.CloseReason == CloseReason.UserClosing) { // 使用System.Diagnostics.Process.Start(Application.ExecutablePath)方法重新启动应用程序 System.Diagnostics.Process.Start(Application.ExecutablePath); // 退出当前进程 System.Diagnostics.Process.GetCurrentProcess().Kill(); } } ``` 需要注意的是,在使用`System.Diagnostics.Process.Start(Application.ExecutablePath)`方法重新启动应用程序时,需要确保应用程序的可执行文件位于正确的路径下,并且在重新启动之前,需要确保所有的数据已经保存。同时,也需要注意在关闭窗口时,要确保在窗口关闭时释放所有资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值