C # MessageBox. Show is automatically disabled every 3 seconds

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.