c# messagebox 隔时关闭
来源:互联网 发布:模型软件 编辑:程序博客网 时间:2024/04/28 00:25
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBoxTimeout.Show("It will be closed automatically in 5 seconds", "MessageBox With Timeout", 5000);
}
}
public class MessageBoxTimeout
{
[DllImport("kernel32.dll")]
private static extern uint GetCurrentThreadId();
private delegate int EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
private static extern int GetClassName(IntPtr hWnd, [Out] StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")]
private static extern bool IsWindowEnabled(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
[return : MarshalAs(UnmanagedType.Bool)]
private static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
private class TimerThread
{
private readonly DateTime timeoutTime;
private readonly uint currentThreadId;
private bool terminateFlag;
private readonly Thread thread;
public TimerThread(int timeoutMillisec)
{
timeoutTime = DateTime.Now.AddMilliseconds(timeoutMillisec);
currentThreadId = GetCurrentThreadId();
terminateFlag = false;
thread = new Thread(ThreadProc);
thread.Start();
}
private void ThreadProc()
{
while (!terminateFlag)
{
Thread.Sleep(100);
if (DateTime.Now > timeoutTime)
{
EnumWindows(EnumWindowsProc, new IntPtr(0));
return;
}
}
}
private int EnumWindowsProc(IntPtr hWnd, IntPtr lParam)
{
uint processId;
uint threadId;
threadId = GetWindowThreadProcessId(hWnd, out processId);
if (threadId == currentThreadId)
{
StringBuilder className = new StringBuilder("", 256);
GetClassName(hWnd, className, 256);
if (className.ToString() == "#32770" && IsWindowEnabled(hWnd))
{
const int WM_COMMAND = 0x111;
PostMessage(hWnd, WM_COMMAND, new IntPtr(2), new IntPtr(0));
return 0;
}
}
return 1;
}
public void Terminate()
{
terminateFlag = true;
thread.Join();
}
}
public static DialogResult Show(string text, int timeoutMillsec)
{
TimerThread tt = new TimerThread(timeoutMillsec);
try
{
return MessageBox.Show(text);
}
finally
{
tt.Terminate();
}
}
public static DialogResult Show(string text, string caption, int timeoutMillsec)
{
TimerThread tt = new TimerThread(timeoutMillsec);
try
{
return MessageBox.Show(text, caption);
}
finally
{
tt.Terminate();
}
}
}
}
- c# messagebox 隔时关闭
- c# 关闭MessageBox 或者自动关闭MessageBox
- c# 关闭messageBox
- c# 自动关闭messagebox
- C# 自动关闭的MessageBox
- c# winform 自动关闭messagebox 模拟回车
- c# winform 自动关闭messagebox 模拟回车
- C# MessageBox
- 定时关闭的MessageBox!
- 自动关闭MessageBox
- 自动关闭MessageBox()对话框
- 自动关闭MessageBox()对话框
- 自动关闭MessageBox对话框
- 使MessageBox框自动关闭
- 使MessageBox框自动关闭
- [NOTE]延迟关闭的MessageBox
- 自动关闭MessageBox的实例
- 自动的关闭对话框 MessageBox
- INCEPTION,Analysis and Design Overview
- Architectural Analysis (Brief Chapter)
- c# 播放音乐
- [翻译]jQuery 简介
- Struts2实现简单上传
- c# messagebox 隔时关闭
- javascript实用代码大全
- Design Model - GRASP: Designing Objects With Responsibilities
- javascript函数
- 上网的十招
- 一个计算机高手的成长
- UDP内网穿透讲解
- 实例分析CSS属性Display与Visibility不同
- 计算机必懂的53个英文单词和缩写