概述
Messagebox自定义计时关闭
新建Winform项目WindowsFormsAppTESTMessageBoxAutoClose
主窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsAppTESTMessageBoxAutoClose
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
MessageBoxTimeOut mb = new MessageBoxTimeOut();
mb.Show("123", "提示:");
}
private void Button2_Click(object sender, EventArgs e)
{
FunCord = 1;
StartTimer(1000);
t = 10;
timer1.Enabled = true;
if (MessageBox.Show("系统提示:确认关闭请点击【确定】, 继续使用请点击【取消】", t + "秒后关闭", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)== DialogResult.OK)
{
MessageBox.Show("哈哈哈,你选择了ok");
}
//MessageBox.Show("系统提示:确认关闭请点击【确定】, 继续使用请点击【取消】", t + "秒后关闭", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
//MessageBox.Show("直接关闭");
}
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SetWindowText(IntPtr hWnd, string text);
[DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
/// <summary>
[DllImport("user32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
const int WM_CLOSE = 0x10;
const int BM_CLICK = 0xF5;
int FunCord;
IntPtr hwnd;
int t;
System.Windows.Forms.Timer timer1;
private void StartTimer(int interval)
{
timer1 = new Timer();
timer1.Interval = interval;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (FunCord == 1)
{
hwnd = FindWindow(null, t.ToString() + "秒后关闭");
t = t - 1;
SetWindowText(hwnd, t.ToString() + "秒后关闭");
if (t == 0)
{
timer1.Enabled = false;
SendMessage(hwnd, WM_CLOSE, 0, 0);
}
}
else if (FunCord == 2)
{
hwnd = FindWindow(null, "关闭提示");
IntPtr a = FindWindowEx(hwnd, (IntPtr)null, null,t.ToString() + "秒后关闭");
t = t - 1;
SetWindowText(a, t.ToString() + "秒后关闭");
if (t == 0)
{
timer1.Enabled = false;
SendMessage(hwnd, WM_CLOSE, 0, 0);
}
}
else if (FunCord == 3)
{
hwnd = FindWindow(null, t.ToString() + "秒后关闭");
t = t - 1;
SetWindowText(hwnd, t.ToString() + "秒后关闭");
if (t == 0)
{
IntPtr OKHwnd = FindWindowEx(hwnd, IntPtr.Zero, null, "确定");
SendMessage(OKHwnd, BM_CLICK, 0, 0);
timer1.Enabled = false;
}
}
else if (FunCord == 4)
{
hwnd = FindWindow(null, t.ToString() + "秒后关闭");
t = t - 1;
SetWindowText(hwnd, t.ToString() + "秒后关闭");
if (t == 0)
{
IntPtr OKHwnd = FindWindowEx(hwnd, IntPtr.Zero, null, "取消");
SendMessage(OKHwnd, BM_CLICK, 0, 0);
timer1.Enabled = false;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
FunCord = 2;
t = 5;
timer1.Enabled = true;
MessageBox.Show(t + "秒后关闭", "关闭提示");
timer1.Enabled = false;
}
}
}
扩展类MessageBoxTimeOut:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsAppTESTMessageBoxAutoClose
{
public class MessageBoxTimeOut
{
private string _caption;
public void Show(string text, string caption)
{
Show(3000, text, caption);
}
public void Show(int timeout, string text, string caption)
{
Show(timeout, text, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
public void Show(int timeout, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
this._caption = caption;
StartTimer(timeout);
MessageBox.Show(text, caption, buttons, icon);
}
private void StartTimer(int interval)
{
Timer timer = new Timer();
timer.Interval = interval;
timer.Tick += new EventHandler(Timer_Tick);
timer.Enabled = true;
}
private void Timer_Tick(object sender, EventArgs e)
{
KillMessageBox();
//停止计时器
((Timer)sender).Enabled = false;
}
[DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
public const int WM_CLOSE = 0x10;
private void KillMessageBox()
{
//查找MessageBox的弹出窗口,注意对应标题
IntPtr ptr = FindWindow(null, this._caption);
if (ptr != IntPtr.Zero)
{
//查找到窗口则关闭
PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
}
}
最后
以上就是明亮盼望为你收集整理的Messagebox自定义计时关闭的全部内容,希望文章能够帮你解决Messagebox自定义计时关闭所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复