C # Countdown close the prompt box

Two days ago a feature needs to be done to achieve a prompt box and can automatically shut down, just from the Internet search can automatically close a prompt box, but the scene is uncertain timing I needed time, and there is no use to the form, but I think we can save for reuse, behind me, too,

This countdown is still a lot of useful tips box for automatic pop automatically shut down, though not in my project

The method in which the core Timer (TimerCallback, Object, Int32, Int32) TimerCallback delegate is a process to be executed on behalf of, their use may be used in various scenarios to call the timing process, but may be provided in the form attribute FormBorderStyle to None, set the form's border and title bar appearance does not appear.

 

 

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) // text header display (top left corner of the form title tips) { This.Text = Caption;} the System.Threading.Timer _timeoutTimer; String _caption; // Definition: text: message, caption: prompt box top left corner of the title text form, Note: when two or more of the same title and the same time prompt box appears, will give priority to close the prompt box floating on the top, but the underlying does not automatically turn off, // get stuck does not move, so that when there are two or more simultaneously closed automatically pop-up text message box title must be different before, timeout: How long after balloon form automatically closed AutoCloseMessageBox ( String text, String Caption, int timeout) { = _caption Caption; _timeoutTimer = new new the System.Threading.Timer (OnTimerElapsed, null , timeout, System.Threading.Timeout.Infinite); //Create a new object is automatically close pop-up boxes to automatically turn off the prompt box AutoCloseMassageBox form name = m_MassageBox AutoCloseMessageBox new new AutoCloseMessageBox (); m_MassageBox.getMassage (text); // m_MassageBox calls itself the object of getMassage function to get incoming external information needs prompt m_MassageBox .getText (caption); // m_MassageBox object calls itself GetText function to get an external incoming require prompt text title m_MassageBox.ShowDialog (); // call ShowDialog () function showDialog () is a dialog window interface to perform `` ` the results appear in a new window interface `` `` `does not allow running in the background that you want to edit` What time `` `have to turn off the showDialog () window interface can perform other operations` `` } public static void Show ( String text, String Caption, int timeout) { new 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); } }

Directly using the class name .show (text, captiom, timeout) when calling a direct call to

 

Below is the scene at the time of the project using the solution

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; } } } }

The form can be used for real-time monitoring of a state of a message box pops up from time to time and depending on the status change and hide 

When using, new a the errorForm

In this form there is a the RichTextBox, to display the message, using SetMsg, setting information to be displayed

Pop up when needed, examples of calling Show () method is actually talking about the form's visible property is set to true, so that the form is displayed, and call the Clear method to clear message

To hide, examples of calling HideForm () method, the form visible property is set to false, call the clear method, clear message

 

Guess you like

Origin www.cnblogs.com/pandefu/p/11204134.html