背景
在Winform應用程序開發中,很多時候我們需要彈出一個消息提示(模式的),又希望不用自己手動去關閉,在這種場景下我們可以開發一個超時消息提示框類,然後進行調用。
代碼實現
具體代碼封裝如下:
MessageBoxTimeout.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Wongoing.Basic
{
/// <summary>
/// 自動超時消息提示框
/// </summary>
public class MessageBoxTimeOut
{
/// <summary>
/// 標題
/// </summary>
private static string _caption;
/// <summary>
/// 顯示消息框
/// </summary>
/// <param name="text">消息內容</param>
/// <param name="caption">標題</param>
/// <param name="timeout">超時時間,單位:毫秒</param>
public static void Show(string text, string caption, int timeout)
{
_caption = caption;
StartTimer(timeout);
MessageBox.Show(text, caption);
}
/// <summary>
/// 顯示消息框
/// </summary>
/// <param name="text">消息內容</param>
/// <param name="caption">標題</param>
/// <param name="timeout">超時時間,單位:毫秒</param>
/// <param name="buttons">消息框上的按鈕</param>
public static void Show(string text, string caption, int timeout, MessageBoxButtons buttons)
{
_caption = caption;
StartTimer(timeout);
MessageBox.Show(text, caption, buttons);
}
/// <summary>
/// 顯示消息框
/// </summary>
/// <param name="text">消息內容</param>
/// <param name="caption">標題</param>
/// <param name="timeout">超時時間,單位:毫秒</param>
/// <param name="buttons">消息框上的按鈕</param>
/// <param name="icon">消息框上的圖標</param>
public static void Show(string text, string caption, int timeout, MessageBoxButtons buttons, MessageBoxIcon icon)
{
_caption = caption;
StartTimer(timeout);
MessageBox.Show(text, caption, buttons, icon);
}
/// <summary>
/// 顯示消息框
/// </summary>
/// <param name="owner">消息框所有者</param>
/// <param name="text">消息內容</param>
/// <param name="caption">標題</param>
/// <param name="timeout">超時時間,單位:毫秒</param>
public static void Show(IWin32Window owner, string text, string caption, int timeout)
{
_caption = caption;
StartTimer(timeout);
MessageBox.Show(owner, text, caption);
}
/// <summary>
/// 顯示消息框
/// </summary>
/// <param name="owner">消息框所有者</param>
/// <param name="text">消息內容</param>
/// <param name="caption">標題</param>
/// <param name="timeout">超時時間,單位:毫秒</param>
/// <param name="buttons">消息框上的按鈕</param>
public static void Show(IWin32Window owner, string text, string caption, int timeout, MessageBoxButtons buttons)
{
_caption = caption;
StartTimer(timeout);
MessageBox.Show(owner, text, caption, buttons);
}
/// <summary>
/// 顯示消息框
/// </summary>
/// <param name="owner">消息框所有者</param>
/// <param name="text">消息內容</param>
/// <param name="caption">標題</param>
/// <param name="timeout">超時時間,單位:毫秒</param>
/// <param name="buttons">消息框上的按鈕</param>
/// <param name="icon">消息框上的圖標</param>
public static void Show(IWin32Window owner, string text, string caption, int timeout, MessageBoxButtons buttons, MessageBoxIcon icon)
{
_caption = caption;
StartTimer(timeout);
MessageBox.Show(owner, text, caption, buttons, icon);
}
private static void StartTimer(int interval)
{
Timer timer = new Timer();
timer.Interval = interval;
timer.Tick += new EventHandler(Timer_Tick);
timer.Enabled = true;
}
private static void Timer_Tick(object sender, EventArgs e)
{
KillMessageBox();
//停止計時器
((Timer)sender).Enabled = false;
}
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private extern static int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
private const int WM_CLOSE = 0x10;
private static void KillMessageBox()
{
//查找MessageBox的彈出窗口,注意對應標題
IntPtr ptr = FindWindow(null, _caption);
if (ptr != IntPtr.Zero)
{
//查找到窗口則關閉
PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
}
}
調用示例
簡單的調用示例如下:
MessageBoxTimeOut.Show("你好,我是超時消息框,3秒後自動關閉!","提示", 3000);