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 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#实现倒计时关闭提示框功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

(0)

相关推荐

  • c#编写的番茄钟倒计时器代码

    恩  主要大家可以看下思路吧  图形界面里 除了图标和音乐两个资源 别的都是代码. 时间没有用timer组件 是自创的Time类在一个线程中进行的倒计时.  对于导出记录 创建了一个Record类  别的就没什么了  .... Program.cs 复制代码 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace 番茄钟 {   

  • C#结合JavaScript实现秒杀倒计时的方法

    本文实例讲述了C#结合JavaScript实现秒杀倒计时的方法.分享给大家供大家参考.具体如下: 最近做个秒杀活动,要用到倒计时.要求每周三上午10:00开始倒计时 private string Dtime() { byte tempB = (byte)DateTime.Now.DayOfWeek; byte dayByte = (byte)DayOfWeek.Wednesday; DateTime wednesdayNow = DateTime.Now.AddDays(dayByte - te

  • C#线程倒计时器源码分享

    本文实例为大家分享了C#线程倒计时器源码,供大家参考,具体内容如下 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace ListZZBG { class TimeHeleper { Thread thread; private TimeSpan time;

  • c#消息提示框messagebox的详解及使用

    C#消息提示框messagebox的详解及使用 消息对话框是用messagebox对象的show方法显示的.MessageBox对象是命名空间System.Windows.Forms的一部分,Show是一个静态方法,意思是说,不需要基于MessageBox类的对象创建实例,就可以使用该方法.而且该方法是可以重载的,即方法可以有不同的参数列表形式. 返回结果:DialogResult dr1=MessageBox.Show(text,caption,buttons,icon,defaultbutt

  • C#基于TimeSpan实现倒计时效果的方法

    本文实例展示了C#基于TimeSpan实现倒计时效果的方法,比较实用的功能,对于初学者来说有一定的学习参考价值.具体实现方法如下: 示例代码如下: using System; using System.Threading; namespace ConsoleApplication29 { class Program { static void Main(string[] args) { try { DateTime _timeEnd = DateTime.Now.AddSeconds(62);

  • C#使用Shader实现夜幕降临倒计时的效果

    最近火爆全球的PC游戏Battlerite(战争仪式)在倒计时的会生成一种类似夜幕降临的效果,会以战场中心为圆心,某个长度为半径的范围外是暗的,而这个半径会逐渐缩小,而圆之外的阴暗部分是附着地形的,本文就尝试使用屏幕后处理的手段来实现这种效果. (暂时缺少Battlerite的截图,稍后会补上) 首先看效果图: 注:本文参考了Tasharen Fog of War插件 创建一个C#脚本,命名为NightFall.cs,为NightFall类创建一些公共变量(nightColor,center和r

  • C#实现windows form倒计时的方法

    本文实例讲述了C#实现windows form倒计时的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace date { public partial cl

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

    前两天实现某个功能需要做一个提示框 并且能够自动关闭的,就从网上搜了一个能够自动关闭的提示框 ,但由于我需要的场景是不确定计时时间的,所以并没有使用到该窗体,但是我觉得可以留存备用 ,后边也把我这种倒计时的提示框用处还是很多的,用于自动弹窗 自动关闭 ,虽然在我的项目中没有 其核心方法在 timer(TimerCallBack,Object,int32,int32) TimerCallBack 是一个委托 ,代表要执行的方法,其用途可以用在各个定时去调用方法的场景,而且可以设置窗体的FormBo

  • JavaScript实现短暂提示框功能

    业务场景:当鼠标移入某元素时,显示提示框进行介绍.当鼠标移除时,会自动消失.引入ToolTip.js和ToolTip.css 主方法:ToolTip.show(需要提示的元素id, 随意不重复即可, 要提示的html文本, 宽(可不指定), 高(可不指定)); ToolTip.show(obj, id, html, width, height); 效果如下: 1.显示文本: 2:显示图片 3:显示网站 js代码:F:\Html5\Plugins\ToolTip\js\ToolTip.js (fu

  • javascript实现倒计时提示框

    本文实例为大家分享了javascript实现倒计时提示框的具体代码,供大家参考,具体内容如下 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">

  • 基于BootStrap Metronic开发框架经验小结【六】对话框及提示框的处理和优化

    在各种Web开发过程中,对话框和提示框的处理是很常见的一种界面处理技术,用得好,可以给用户很好的页面体验,Bootstrap开发也一样,我们往往在页面新增.编辑.查看详细等界面使用弹出对话框层的方式进行显示数据,删除则可能使用一个提示确认框,如果操作成功,我们可以使用更丰富的提示框来处理,本篇主要对比说明在Bootstrap开发中用到的这些技术要点. 1.Bootstrap对话框的使用 常规的Bootstrap有几种尺寸的对话框,包括默认状态的小对话框,中等宽度的对话框,和全尺寸的对话框几种,B

  • js提示框替代系统alert,自动关闭alert对话框的实现方法

    自己写了个alert提示框.因为系统alert在苹果手机微信中,提示时,顶部会显示网站地址. 同时其他后续操作需要在js中继续填写.因此简单用div写了一个alert提示框,并自动关闭. 效果图 css样式 /*弹出消息对话框样式*/ .show_alert_box{ width:100%; height:100%; position:fixed; top:0px; left:0px; background-color:rgba(0,0,0,0.6); display:none; z-index

  • 给WordPress的编辑后台添加提示框的代码实例分享

    WordPress 3.5 新添加了一个提示框功能,可以创建一个提示框,然后指向任何元素,比如下边的例子: 本文就来教你怎么创建一个这样的提示框. 首先需要添加提示框的脚本,这样才能使用提示框的 JS 方法. //挂载提示框脚本 function Bing_admin_pointer_enqueue_scripts(){ wp_enqueue_style( 'wp-pointer' ); wp_enqueue_script( 'wp-pointer' ); } add_action( 'admi

  • JS延时提示框实现方法详解

    本文实例讲述了JS延时提示框实现方法.分享给大家供大家参考,具体如下: 提示框功能:当鼠标指向头像时,弹出一个信息框,鼠标可移动到信息框,当鼠标离开头像时信息框消失,当鼠标离开信息框时信息框消失. 实现功能思路: 1.获取元素. 2.当鼠标指向Div1时,Div2显示. 3.当鼠标离开Div1时,使Div2延迟0.5秒消失,这样以便有时间把鼠标移到Div2. 4.当鼠标指向Div2时,Div2显示.因为第3步设置setTimeout使Div2消失,所以把用clearTimeout()把setTi

  • js网页右下角提示框实例

    本文实例讲述了js网页右下角提示框的实现方法,分享给大家供大家参考.具体方法如下: html代码部分如下: 复制代码 代码如下: <style type="text/css"> .messageTip{border-right: #455690 1px solid; border-top: #a6b4cf 1px solid; border-left: #a6b4cf 1px solid; border-bottom: #455690 1px solid; z-index:

  • layer提示框添加多个按钮选择的实例

    如下所示: function jumpChoose(argu) { //询问框 var index = layer.confirm('下载文件还是在线预览呢?', { btn: ['在线预览','下载',"关闭"], //按钮 shade: false //不显示遮罩 }, function(){ //关闭提示框 layer.close(index); }, function(){ //关闭提示框 layer.close(index); }, function(){ //关闭提示框 l

  • unity自定义弹出框功能

    本文实例为大家分享了unity自定义弹出框的具体方法,供大家参考,具体内容如下 一.弹出框的搭建 布局如图:Message为整个父物体,并且添加UiMessage代码.panel为遮罩. MessageBox为整个提示框,Panel为标题,ok为确定按钮,cancel为取消按钮,retry为重试按钮,Text为提示框的文字. 注意大小写,后面代码会根据名称进行获取对应组建. 效果如下: 二.MessageBox代码 要说明的都在代码中注释了.仿照Windows的提示框功能,如果功能不足可自行添加

随机推荐