简要咨询咨询QQ网站导航网站搜索手机站点联系我们设为首页加入收藏 

C#实现winform自动关闭MessageBox对话框的方法

来源:易贤网   阅读:8815 次  日期:2015-04-28 14:16:05

温馨提示:易贤网小编为您整理了“C#实现winform自动关闭MessageBox对话框的方法”,方便广大网友查阅!

C#实现winform自动关闭MessageBox对话框的方法

这篇文章主要介绍了C#实现winform自动关闭MessageBox对话框的方法,实例分析了C#中MessageBox对话框的相关操作技巧,需要的朋友可以参考下

本文实例讲述了C#实现winform自动关闭MessageBox对话框的方法。分享给大家供大家参考。具体实现方法如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace WindowsApplication1

{

public partial class AutoDeleteMessageBox : Form

{

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

public AutoDeleteMessageBox()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

StartKiller();

MessageBox.Show("3秒钟后自动关闭MessageBox窗口", "MessageBox");

}

private void StartKiller()

{

Timer timer = new Timer();

timer.Interval = 3000; //3秒启动

timer.Tick += new EventHandler(Timer_Tick);

timer.Start();

}

private void Timer_Tick(object sender, EventArgs e)

{

KillMessageBox();

//停止Timer

((Timer)sender).Stop();

}

private void KillMessageBox()

{

//按照MessageBox的标题,找到MessageBox的窗口

IntPtr ptr = FindWindow(null, "MessageBox");

if (ptr != IntPtr.Zero)

{

//找到则关闭MessageBox窗口

PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);

}

}

}

}

希望本文所述对大家的C#程序设计有所帮助

更多信息请查看IT技术专栏

更多信息请查看网络编程
点此处就本文及相关问题在本站进行非正式的简要咨询(便捷快速)】     【点此处查询各地各类考试咨询QQ号码及交流群
上一篇:在Python中使用mongoengine操作MongoDB教程
下一篇:利用Python的装饰器解决Bottle框架中用户验证问题
易贤网手机网站地址:C#实现winform自动关闭MessageBox对话框的方法
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!