C#實(shí)現(xiàn)winform自動(dòng)關(guān)閉MessageBox對(duì)話(huà)框的方法
本文實(shí)例講述了C#實(shí)現(xiàn)winform自動(dòng)關(guān)閉MessageBox對(duì)話(huà)框的方法。分享給大家供大家參考。具體實(shí)現(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秒鐘后自動(dòng)關(guān)閉MessageBox窗口", "MessageBox");
}
private void StartKiller()
{
Timer timer = new Timer();
timer.Interval = 3000; //3秒啟動(dòng)
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);
}
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C# Bitmap圖像處理(含增強(qiáng)對(duì)比度的三種方法)
本文主要介紹了C# Bitmap圖像處理(含增強(qiáng)對(duì)比度的三種方法),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
C#在winform中實(shí)現(xiàn)數(shù)據(jù)增刪改查等功能
本篇文章主要是介紹了C#在winform中操作數(shù)據(jù)庫(kù),實(shí)現(xiàn)數(shù)據(jù)增刪改查,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
基于Unity3D實(shí)現(xiàn)3D迷宮小游戲的示例代碼
迷宮游戲作為經(jīng)典的小游戲,一直深受大家的喜愛(ài)。本文小編將為大家詳細(xì)介紹一下如何用Unity實(shí)現(xiàn)一個(gè)3D版的迷宮小游戲,感興趣的可以動(dòng)手試一試2022-03-03
C# BinaryReader實(shí)現(xiàn)讀取二進(jìn)制文件
在 C# 以二進(jìn)制形式讀取數(shù)據(jù)時(shí)使用的是 BinaryReader 類(lèi)。本文介紹了C# BinaryReader實(shí)現(xiàn)讀取二進(jìn)制文件,感興趣的可以了解一下2021-06-06
解決C#運(yùn)行程序修改數(shù)據(jù)后數(shù)據(jù)表不做更新的問(wèn)題
近日,在使用C#連接數(shù)據(jù)庫(kù)的時(shí)候,對(duì)數(shù)據(jù)庫(kù)中的表做更新后,在當(dāng)前啟動(dòng)項(xiàng)目中去顯示表數(shù)據(jù)時(shí)雖然會(huì)發(fā)生一個(gè)更新,但是在結(jié)束程序運(yùn)行后再去觀察數(shù)據(jù)表中的記錄時(shí)發(fā)現(xiàn)并沒(méi)有發(fā)生一個(gè)變化,所以本文給大家解決一下這個(gè)問(wèn)題,需要的朋友可以參考下2023-08-08

