当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现倒计时关闭提示框功能

C#实现倒计时关闭提示框功能

2019年09月08日  | 移动技术网IT编程  | 我要评论

前两天实现某个功能需要做一个提示框 并且能够自动关闭的,就从网上搜了一个能够自动关闭的提示框 ,但由于我需要的场景是不确定计时时间的,所以并没有使用到该窗体,但是我觉得可以留存备用 ,后边也把我这种倒计时的提示框用处还是很多的,用于自动弹窗 自动关闭 ,虽然在我的项目中没有

其核心方法在 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 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);
  }
}

调用时直接使用类名.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方法,清除提示信息

总结

以上所述是小编给大家介绍的c#实现倒计时关闭提示框功能,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网