C#完成winform主動封閉MessageBox對話框的辦法。本站提示廣大學習愛好者:(C#完成winform主動封閉MessageBox對話框的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成winform主動封閉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#法式設計有所贊助。