[C#]定時自動關閉MessageBox視窗小技巧
2014-05-06 10:20
246 查看
[C#]定時自動關閉MessageBox視窗小技巧
執行結果:
參考網址:
http://topic.csdn.net/u/20070801/11/90043e30-2319-4b47-af34-dd3200430336.html
http://www.codeproject.com/KB/dialog/AutoCloseMessageBox.aspx
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); } } } }
執行結果:
參考網址:
http://topic.csdn.net/u/20070801/11/90043e30-2319-4b47-af34-dd3200430336.html
http://www.codeproject.com/KB/dialog/AutoCloseMessageBox.aspx
相关文章推荐
- C# 常用小技巧
- C#实战小技巧(一):静态类和静态成员
- Asp.net &C#开发中的一些注意事项及小技巧
- VS2015使用小技巧 将VS2015设置成C#开发环境
- [C#开发小技巧]解决WinForm控件TabControl闪烁问题
- C#进行平台调用时利用C++头文件的小技巧
- 代码对齐 分类: C#小技巧 2014-04-17 14:45 166人阅读 评论(0) 收藏
- 前台获取后台c#代码的小技巧
- 工作中经常用到的C#编程小技巧
- C#解决Linq OrderBy() 失效的小技巧
- .NET C# 【小技巧】控制台程序,运行是否弹出窗口选择!
- C#:小技巧
- C#开发小技巧--对Text控件增量赋值的代码优化
- C#小技巧系列之四:获取本地网络信息
- C#小技巧(一)循环的Remove方法
- C#中使用反射遍历一个对象属性及值的小技巧
- 转:C# WinForm清空界面控件值的小技巧
- [C#开发小技巧]解决WinForm控件TabControl闪烁问题
- c#中枚举类型使用的一个小技巧
- 七个C#编程的小技巧