Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. runtime. interopservices;
Namespace windowsformsapplication1
{
Public partial class form1: Form
{
System. Windows. Forms. Timer timer = NULL;
String wm_caption = string. empty;
Public form1 ()
{
Initializecomponent ();
}
[Dllimport ("user32.dll", entrypoint = "findwindow", charset = charset. Auto)]
Private extern static intptr findwindow (string lpclassname, string lpwindowname );
[Dllimport ("user32.dll", charset = charset. Auto)]
Public static extern int postmessage (intptr hwnd, int MSG, intptr wparam, intptr lparam );
Public const int wm_close = 0x10;
Private void button#click (Object sender, eventargs E)
{
Showmsg ("test", "title ");
}
Void showmsg (string MSG, string Caption)
{
Wm_caption = Caption;
If (timer! = NULL & timer. Enabled = false)
{
Timer. Enabled = true;
}
Else
{
Timer = new timer (); // declare a timer object named settimer
Timer. interval = (3*1000); // starts after X seconds
Timer. Tick + = new eventhandler (settimer_tick); // create a trigger tick event
Timer. Start (); // start timer (that is, start settimer)
}
MessageBox. Show (MSG, caption, messageboxbuttons. okcancel );
}
Void settimer_tick (Object sender, eventargs E)
{
Killmessagebox (); // remove the MessageBox (that is, remove the MessageBox)
(Timer) sender). Enabled = false; // stop time. Remember to transform to Timer
}
/// <Summary>
/// Kill the window
/// </Summary>
Private void killmessagebox ()
{
// Follow the MessageBox title to find the MessageBox window
Intptr PTR = findwindow (null, wm_caption );
If (PTR! = Intptr. Zero) // indicates that when PTR is not null
{
// Close the MessageBox dialog box
Postmessage (PTR, wm_close, intptr. Zero, intptr. Zero );
}
}
}
}
C # MessageBox. Show is automatically disabled every 3 seconds