C#實(shí)現(xiàn)倒計(jì)時(shí)關(guān)閉提示框功能
前兩天實(shí)現(xiàn)某個(gè)功能需要做一個(gè)提示框 并且能夠自動(dòng)關(guān)閉的,就從網(wǎng)上搜了一個(gè)能夠自動(dòng)關(guān)閉的提示框 ,但由于我需要的場景是不確定計(jì)時(shí)時(shí)間的,所以并沒有使用到該窗體,但是我覺得可以留存?zhèn)溆?,后邊也把我這種倒計(jì)時(shí)的提示框用處還是很多的,用于自動(dòng)彈窗 自動(dòng)關(guān)閉 ,雖然在我的項(xiàng)目中沒有
其核心方法在 timer(TimerCallBack,Object,int32,int32) TimerCallBack
是一個(gè)委托 ,代表要執(zhí)行的方法,其用途可以用在各個(gè)定時(shí)去調(diào)用方法的場景,而且可以設(shè)置窗體的FormBorderStyle
的屬性為None,設(shè)置窗體邊框和標(biāo)題欄外觀不顯示.
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 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); } }
調(diào)用時(shí)直接使用類名.show(text,captiom,timeout)
直接調(diào)用即可
下邊是當(dāng)時(shí)的項(xiàng)目使用場景的解決辦法
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; } } } }
該窗體可以用于實(shí)時(shí)監(jiān)控某一個(gè)狀態(tài)時(shí) 而彈出的提示框 并根據(jù)狀態(tài)改變而隱藏
使用時(shí),new一個(gè)該errorForm
在該窗體有一個(gè)RichTextBox,用來顯示提示信息,使用SetMsg,設(shè)置要顯示的信息
需要彈出時(shí),實(shí)例調(diào)用Show()方法 實(shí)際就是講該窗體的visible屬性置為true,讓窗體顯示,并且調(diào)用Clear方法,清除提示信息
需要隱藏時(shí),實(shí)例調(diào)用HideForm()方法,將窗體visible屬性設(shè)置為false,調(diào)用clear方法,清除提示信息
總結(jié)
以上所述是小編給大家介紹的C#實(shí)現(xiàn)倒計(jì)時(shí)關(guān)閉提示框功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
- c# 設(shè)置TeeChart控件的提示文本
- C#瀏覽器提示跨域問題解決方案
- C#實(shí)現(xiàn)狀態(tài)欄提示信息功能的示例
- C# winForm實(shí)現(xiàn)的氣泡提示窗口功能示例
- 解決C#調(diào)用dll提示
- C#實(shí)現(xiàn)簡單的loading提示控件實(shí)例代碼
- c#消息提示框messagebox的詳解及使用
- C#中TextBox實(shí)現(xiàn)輸入提示功能的方法
- C#程序提示“正由另一進(jìn)程使用,因此該進(jìn)程無法訪問該文件”的解決辦法
- C#提示:“在證書存儲(chǔ)區(qū)中找不到清單簽名證書”的解決方法
- c# 關(guān)閉窗體時(shí)提示的小例子
- 給 c# 程序員的十個(gè)重要提示
相關(guān)文章
C#實(shí)現(xiàn)字母與ASCII碼互相轉(zhuǎn)換
ASCII是基于拉丁字母的編碼系統(tǒng),也是現(xiàn)今最通用的單字節(jié)編碼系統(tǒng),本文主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)字母與ASCII碼互轉(zhuǎn),需要的可以參考下2024-01-01C#反射實(shí)現(xiàn)插件式開發(fā)的過程詳解
插件式架構(gòu),一種全新的、開放性的、高擴(kuò)展性的架構(gòu)體系,插件式架構(gòu)設(shè)計(jì)好處很多,把擴(kuò)展功能從框架中剝離出來,降低了框架的復(fù)雜度,讓框架更容易實(shí)現(xiàn),這篇文章主要介紹了C#反射實(shí)現(xiàn)插件式開發(fā),需要的朋友可以參考下2023-09-09Unity實(shí)現(xiàn)透視滑動(dòng)列表
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)透視滑動(dòng)列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07使用Deflate算法對文件進(jìn)行壓縮與解壓縮的方法詳解
本篇文章是對使用Deflate算法對文件進(jìn)行壓縮和解壓縮的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06C#定時(shí)每天00點(diǎn)00分00秒自動(dòng)重啟軟件
這篇文章主要為大家詳細(xì)介紹了C#定時(shí)每天00點(diǎn)00分00秒自動(dòng)重啟軟件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08C#實(shí)現(xiàn)Array,List,Dictionary相互轉(zhuǎn)換
這篇文章介紹了C#實(shí)現(xiàn)Array,List,Dictionary互相轉(zhuǎn)換的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04