C#自动关闭弹出的对话框,自动按下确定按钮
//测试代码
private void button3_Click(object sender, EventArgs e)
{
AutoCloseDlg.ShowMessageBoxTimeout("hello,www.vjsdn.com!", "hello window",
MessageBoxButtons.YesNoCancel, 2000);
}
{
AutoCloseDlg.ShowMessageBoxTimeout("hello,www.vjsdn.com!", "hello window",
MessageBoxButtons.YesNoCancel, 2000);
}
AutoCloseDlg.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
namespace TestProject
{
public class AutoCloseDlg
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool EndDialog(IntPtr hDlg, out IntPtr nResult);
//外部调用的方法
//参数:timeout定义多少毫秒关闭对话框
public static void ShowMessageBoxTimeout(string text, string caption,
MessageBoxButtons buttons, int timeout)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(CloseMessageBox),
new CloseState(caption, timeout));
MessageBox.Show(text, caption, buttons);
}
/// <summary>
/// 由WaitCallback调用的关闭窗体方法
/// </summary>
private static void CloseMessageBox(object state)
{
CloseState closeState = state as CloseState;
Thread.Sleep(closeState.Timeout);
IntPtr dlg = FindWindow(null, closeState.Caption);
if (dlg != IntPtr.Zero)
{
IntPtr result;
EndDialog(dlg, out result);
}
}
}
/// <summary>
/// 作为ThreadPool.QueueUserWorkItem方法的State参数
/// </summary>
public class CloseState
{
private int _Timeout;
/// <summary>
/// In millisecond
/// </summary>
public int Timeout
{
get
{
return _Timeout;
}
}
private string _Caption;
/// <summary>
/// Caption of dialog
/// </summary>
public string Caption
{
get
{
return _Caption;
}
}
public CloseState(string caption, int timeout)
{
_Timeout = timeout;
_Caption = caption;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
namespace TestProject
{
public class AutoCloseDlg
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool EndDialog(IntPtr hDlg, out IntPtr nResult);
//外部调用的方法
//参数:timeout定义多少毫秒关闭对话框
public static void ShowMessageBoxTimeout(string text, string caption,
MessageBoxButtons buttons, int timeout)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(CloseMessageBox),
new CloseState(caption, timeout));
MessageBox.Show(text, caption, buttons);
}
/// <summary>
/// 由WaitCallback调用的关闭窗体方法
/// </summary>
private static void CloseMessageBox(object state)
{
CloseState closeState = state as CloseState;
Thread.Sleep(closeState.Timeout);
IntPtr dlg = FindWindow(null, closeState.Caption);
if (dlg != IntPtr.Zero)
{
IntPtr result;
EndDialog(dlg, out result);
}
}
}
/// <summary>
/// 作为ThreadPool.QueueUserWorkItem方法的State参数
/// </summary>
public class CloseState
{
private int _Timeout;
/// <summary>
/// In millisecond
/// </summary>
public int Timeout
{
get
{
return _Timeout;
}
}
private string _Caption;
/// <summary>
/// Caption of dialog
/// </summary>
public string Caption
{
get
{
return _Caption;
}
}
public CloseState(string caption, int timeout)
{
_Timeout = timeout;
_Caption = caption;
}
}
}
源代码出处/博客园
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 |
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 |
在线客服
