C#實現(xiàn)winform自動關(guān)閉MessageBox對話框的方法
更新時間:2015年04月24日 15:32:51 作者:令狐不聰
這篇文章主要介紹了C#實現(xiàn)winform自動關(guān)閉MessageBox對話框的方法,實例分析了C#中MessageBox對話框的相關(guān)操作技巧,需要的朋友可以參考下
本文實例講述了C#實現(xiàn)winform自動關(guān)閉MessageBox對話框的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
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秒鐘后自動關(guān)閉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的標(biāo)題,找到MessageBox的窗口
IntPtr ptr = FindWindow(null, "MessageBox");
if (ptr != IntPtr.Zero)
{
//找到則關(guān)閉MessageBox窗口
PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
}
}
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
Unity UGUI的GridLayoutGroup網(wǎng)格布局組件使用詳解
這篇文章主要介紹了Unity UGUI的GridLayoutGroup網(wǎng)格布局組件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
C#讀取QQ純真IP數(shù)據(jù)庫QQWry.Dat的代碼
QQ純真IP庫算是IP地址收集較為全的一個IP庫,對于IP查詢來說這個是不錯的選擇。下面是如何查詢純真IP庫的一個類,使用C#代碼。2007-03-03
C#讀取與寫入txt文件內(nèi)容的實現(xiàn)方法
在 C# 中讀取和寫入文本文件內(nèi)容是一個常見的任務(wù),本文主要介紹了使用幾種不同方法讀取和寫入文本文件的示例,并通過代碼示例介紹的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2024-08-08

