自動關閉彈出提示框(用一個小窗體顯示提示信息):
例如在一個form窗體中彈出自動關閉的提示框
1、首先創建一個彈出提示信息的窗體 AutoCloseMassageBox,在里面拖一個lable控件,去掉默認文字,設置為透明,專門用來顯示提示信息
在這個窗體中加入外部傳入需要提示的信息和文本標題獲取函數
public void getMassage(string text)//顯示的提示信息
{
label.Text = text;
}
public void GetText(string caption)//顯示的文本標題(左上角的窗體標題提示)
{
this.Text = caption;
}
2、然后在form窗體中聲明和加入這個彈出提示框函數
public AutoCloseMassageBox m_MassageBox;
//自動關閉彈出提示框
public class AutoClosingMessageBox
{
System.Threading.Timer _timeoutTimer;
string _caption;
//參數含義:text: 提示信息 、 caption:提示框窗體左上角標題文本,注意:當兩個或多個提示框標題相同且同時出現時,會優先關閉浮在最上層提示框,但下層不會自動關閉,
會卡住不動,所以當存在這兩個或多個自動關閉提示框同時彈出時文本標題必須不同才可 、timeout:多長時間后提示框窗體自動關閉
AutoClosingMessageBox(string text, string caption, int timeout)
{
_caption = caption;
_timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
null, timeout, System.Threading.Timeout.Infinite);
//新建一個自動關閉彈出提示框對象 AutoCloseMassageBox為自動關閉提示框窗體名稱
AutoCloseMassageBox m_MassageBox = new AutoCloseMassageBox();
m_MassageBox.getMassage(text);//m_MassageBox 對象調用自身的getMassage函數獲取外部傳入需要提示的信息
m_MassageBox.GetText(caption);//m_MassageBox 對象調用自身的GetText函數獲取外部傳入需要提示的文本標題
m_MassageBox.ShowDialog();//調用ShowDialog()函數 showDialog()是一個對話框窗口界面```執行結果以新窗口界面出現```不允許進行后台運行```就是你想編輯什么的時候```非得先關閉showDialog()窗口界面才可以進行其他操作```
}
public static void Show(string text, string caption, int timeout)
{
new AutoClosingMessageBox(text, caption, timeout);
}
void OnTimerElapsed(object state)
{
IntPtr mbWnd = FindWindow(null, _caption);
if (mbWnd != IntPtr.Zero)
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
_timeoutTimer.Dispose();
}
const int WM_CLOSE = 0x0010;
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}
3、最后在想要顯示自動關閉彈出提示框的地方調用該函數即可
AutoClosingMessageBox.Show("系統初始化中,請稍后 ... ", "自動關閉提示框", 1500);