溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#如何實(shí)現(xiàn)倒計(jì)時(shí)關(guān)閉提示框功能

發(fā)布時(shí)間:2021-05-17 09:42:23 來源:億速云 閱讀:287 作者:小新 欄目:編程語言

這篇文章主要介紹了C#如何實(shí)現(xiàn)倒計(jì)時(shí)關(guān)閉提示框功能,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

前兩天實(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)題欄外觀不顯示.

C#如何實(shí)現(xiàn)倒計(jì)時(shí)關(guān)閉提示框功能

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方法,清除提示信息

C#是什么

C#是一個(gè)簡單、通用、面向?qū)ο蟮木幊陶Z言,它由微軟Microsoft開發(fā),繼承了C和C++強(qiáng)大功能,并且去掉了一些它們的復(fù)雜特性,C#綜合了VB簡單的可視化操作和C++的高運(yùn)行效率,以其強(qiáng)大的操作能力、優(yōu)雅的語法風(fēng)格、創(chuàng)新的語言特性和便捷的面向組件編程從而成為.NET開發(fā)的首選語言,但它不適用于編寫時(shí)間急迫或性能非常高的代碼,因?yàn)镃#缺乏性能極高的應(yīng)用程序所需要的關(guān)鍵功能。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#如何實(shí)現(xiàn)倒計(jì)時(shí)關(guān)閉提示框功能”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI