C#倒計時關閉提示框


前兩天實現某個功能需要做一個提示框 並且能夠自動關閉的,就從網上搜了一個能夠自動關閉的提示框 ,但由於我需要的場景是不確定計時時間的,所以並沒有使用到該窗體,但是我覺得可以留存備用 ,后邊也把我

這種倒計時的提示框用處還是很多的,用於自動彈窗 自動關閉 ,雖然在我的項目中沒有

其核心方法在 timer(TimerCallBack,Object,int32,int32) TimerCallBack 是一個委托 ,代表要執行的方法,其用途可以用在各個定時去調用方法的場景,而且可以設置窗體的FormBorderStyle的屬性為None,設置窗體邊框和標題欄外觀不顯示.

 

 以下為網上找的一段代碼實現的定時關閉窗體的功能  ,但由於是之前找的,已經找不到原作者,在此表示十分抱歉

以下代碼非本人編寫:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Demo { public partial class AutoCloseMessageBox : Form { public AutoCloseMessageBox() { InitializeComponent(); } public void getMassage(string text)  { label1.Text = text; } public void GetText(string caption)  { this.Text = caption; } System.Threading.Timer _timeoutTimer; string _caption;  AutoCloseMessageBox(string text, string caption, int timeout) { _caption = caption; _timeoutTimer = new System.Threading.Timer(OnTimerElapsed, null, timeout, System.Threading.Timeout.Infinite);  AutoCloseMessageBox m_MassageBox = new AutoCloseMessageBox();
m_MassageBox.getMassage(text); m_MassageBox.GetText(caption); m_MassageBox.ShowDialog(); public static void Show(string text, string caption, int timeout) { new AutoCloseMessageBox(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); } }

調用時直接使用類名.show(text,captiom,timeout) 直接調用即可

 

下邊是當時的項目使用場景的解決辦法

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace NewuView.Mix { public partial class ErrorForm : Form { public ErrorForm() { InitializeComponent(); } private void BarcodeErrorForm_Load(object sender, EventArgs e) { this.ShowInTaskbar = false; } public void Clear() { if (this.InvokeRequired) { this.BeginInvoke(new MethodInvoker(Clear)); } else { this.richTextBox1.Clear(); } } public void SetMsg(string msg) { if (this.InvokeRequired) { this.BeginInvoke(new Action<string>(SetMsg), msg); } else { this.richTextBox1.AppendText(msg + Environment.NewLine); } } public Point Point1 { get; set; } public void ShowForm() { if (this.InvokeRequired) { this.Invoke(new MethodInvoker(ShowForm)); } else { this.Location = Point1; this.BringToFront(); this.Visible = true; } } public void HideForm() { if (this.InvokeRequired) { this.Invoke(new MethodInvoker(HideForm)); } else { this.richTextBox1.Clear(); this.Visible = false; } } } }

該窗體可以用於實時監控某一個狀態時 而彈出的提示框 並根據狀態改變而隱藏 

使用時,new一個該errorForm

在該窗體有一個RichTextBox,用來顯示提示信息,使用SetMsg,設置要顯示的信息

需要彈出時,實例調用Show()方法  實際就是講該窗體的visible屬性置為true,讓窗體顯示,並且調用Clear方法,清除提示信息

需要隱藏時,實例調用HideForm()方法,將窗體visible屬性設置為false,調用clear方法,清除提示信息

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM