前两天实现某个功能需要做一个提示框 并且能够自动关闭的,就从网上搜了一个能够自动关闭的提示框 ,但由于我需要的场景是不确定计时时间的,所以并没有使用到该窗体,但是我觉得可以留存备用 ,后边也把我
这种倒计时的提示框用处还是很多的,用于自动弹窗 自动关闭 ,虽然在我的项目中没有
其核心方法在timer(TimerCallBack,Object,int32,int32) TimerCallBack 是一个委托 ,代表要执行的方法,其用途可以用在各个定时去调用方法的场景,而且可以设置窗体的FormBorderStyle的属性为None,设置窗体边框和标题栏外观不显示.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceNewuView.Mix
{
public partial classAutoCloseMessageBox : Form
{
publicAutoCloseMessageBox()
{
InitializeComponent();
}
public void getMassage(string text)//显示的提示信息
{
label1.Text =text;
}
public void GetText(string caption)//显示的文本标题(左上角的窗体标题提示)
{
this.Text =caption;
}
System.Threading.Timer _timeoutTimer;
string_caption;
//参数含义:text: 提示信息 、 caption:提示框窗体左上角标题文本,注意:当两个或多个提示框标题相同且同时出现时,会优先关闭浮在最上层提示框,但下层不会自动关闭,
//会卡住不动,所以当存在这两个或多个自动关闭提示框同时弹出时文本标题必须不同才可 、timeout:多长时间后提示框窗体自动关闭
AutoCloseMessageBox(string text, string caption, inttimeout)
{
_caption =caption;
_timeoutTimer = newSystem.Threading.Timer(OnTimerElapsed,
null, timeout, System.Threading.Timeout.Infinite);
//新建一个自动关闭弹出提示框对象 AutoCloseMassageBox为自动关闭提示框窗体名称
AutoCloseMessageBox m_MassageBox = newAutoCloseMessageBox();
m_MassageBox.getMassage(text);//m_MassageBox 对象调用自身的getMassage函数获取外部传入需要提示的信息
m_MassageBox.GetText(caption);//m_MassageBox 对象调用自身的GetText函数获取外部传入需要提示的文本标题
m_MassageBox.ShowDialog();//调用ShowDialog()函数 showDialog()是一个对话框窗口界面```执行结果以新窗口界面出现```不允许进行后台运行```就是你想编辑什么的时候```非得先关闭showDialog()窗口界面才可以进行其他操作```
}
public static void Show(string text, string caption, inttimeout)
{
newAutoCloseMessageBox(text, caption, timeout);
}
void OnTimerElapsed(objectstate)
{
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, stringlpWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet =System.Runtime.InteropServices.CharSet.Auto)]
static externIntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}
}
调用时直接使用类名.show(text,captiom,timeout) 直接调用即可
下边是当时的项目使用场景的解决办法
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceNewuView.Mix
{
public partial classErrorForm : Form
{
publicErrorForm()
{
InitializeComponent();
}
private void BarcodeErrorForm_Load(objectsender, EventArgs e)
{
this.ShowInTaskbar = false;
}
public voidClear()
{
if (this.InvokeRequired)
{
this.BeginInvoke(newMethodInvoker(Clear));
}
else{
this.richTextBox1.Clear();
}
}
public void SetMsg(stringmsg)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new Action(SetMsg), msg);
}
else{
this.richTextBox1.AppendText(msg +Environment.NewLine);
}
}
public Point Point1 { get; set; }
public voidShowForm()
{
if (this.InvokeRequired)
{
this.Invoke(newMethodInvoker(ShowForm));
}
else{
this.Location =Point1;
this.BringToFront();
this.Visible = true;
}
}
public voidHideForm()
{
if (this.InvokeRequired)
{
this.Invoke(newMethodInvoker(HideForm));
}
else{
this.richTextBox1.Clear();
this.Visible = false;
}
}
}
}
该窗体可以用于实时监控某一个状态时 而弹出的提示框 并根据状态改变而隐藏
使用时,new一个该errorForm
在该窗体有一个RichTextBox,用来显示提示信息,使用SetMsg,设置要显示的信息
需要弹出时,实例调用Show()方法 实际就是讲该窗体的visible属性置为true,让窗体显示,并且调用Clear方法,清除提示信息
需要隐藏时,实例调用HideForm()方法,将窗体visible属性设置为false,调用clear方法,清除提示信息