//按钮触发事件
private void button25_Click(object sender, EventArgs e)
{
ShowMessage("测试弹框!!!");
}
//调用弹窗方法
private void ShowMessage(string sMsg)
{
// 创建一个线程来执行倒计时操作
Thread thread = new Thread(() =>
{
// 倒计时几秒关闭,我这里设置的5秒自动关闭
Thread.Sleep(5000);
// 关闭MessageBox
if (InvokeRequired)
{
Invoke(new System.Action(() => { CloseMessageBox(); }));
}
else
{
CloseMessageBox();
}
});
// 启动线程
thread.Start();
// 弹出MessageBox提示框,注意:这里的标题必须与下方查找关闭MessageBox里的标题一致。
MessageBox.Show(sMsg, "完成提示");
}
//关闭弹窗方法
// 查找窗口
[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;
///关闭弹窗方法
private void CloseMessageBox()
{
// 查找并关闭MessageBox窗口
IntPtr hwnd = FindWindow(null, "完成提示");//一致
if (hwnd != IntPtr.Zero)
{
SendMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}