java倒计时自动关闭弹窗_C# WinForm MessageBox弹窗倒计时的自动关闭

这是一个C# WinForm应用程序,通过DllImport调用系统API实现MessageBox弹窗的倒计时自动关闭功能。用户点击按钮后,会显示一个警告弹窗,倒计时从5秒开始,每秒减少1秒,直到0秒时自动关闭弹窗。如果在倒计时过程中用户点击确定或取消,弹窗也会立即关闭。
摘要由CSDN通过智能技术生成

dc840f9fff341d80e87455749cdf9b03.gif

[DllImport("user32.dll", EntryPoint = "FindWindow")]

private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);

[DllImport("User32.dll", CharSet = CharSet.Auto)]

public static extern int SetWindowText(IntPtr hWnd, string text);

[DllImport("user32.dll", EntryPoint = "FindWindowEx")]

private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

///

[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;

private void button1_Click(object sender, EventArgs e)

{

FunCord = 1;

t = 5;

timer1.Enabled = true;

MessageBox.Show("系统关机提示:\n\n 确认关机请点击【确定】\n\n 继续使用请点击【取消】", "系统将于" + t + "秒后关机", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);

MessageBox.Show("直接关闭");

}

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;

}

private void Form1_Load(object sender, EventArgs e)

{

}

private void button3_Click(object sender, EventArgs e)

{

FunCord = 3;

t = 5;

timer1.Enabled = true;

if (MessageBox.Show("系统关机提示:\n\n 确认关机请点击【确定】\n\n 继续使用请点击【取消】", "系统将于" + t + "秒后关机", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.OK)

{

MessageBox.Show("点击了确定");

}

timer1.Enabled = false;

}

private void button4_Click(object sender, EventArgs e)

{

FunCord = 4;

t = 5;

timer1.Enabled = true;

if (MessageBox.Show("系统关机提示:\n\n 确认关机请点击【确定】\n\n 继续使用请点击【取消】", "系统将于" + t + "秒后关机", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Cancel)

{

MessageBox.Show("点击了取消");

}

timer1.Enabled = false;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值