Implement an automatically closed MessageBox in winform

Source: Internet
Author: User

In winform, we can call MessageBox. Show to display a message dialog box, prompting users to confirm and perform other operations. In some applications, we needProgramTo automatically close the message dialog box instead of clicking OK. However,. NET Framework does not provide us with the method to automatically disable MessageBox. To implement this function, we need to use windowAPI.

First, we need to find the window handle of the message dialog box. A simple method is to use the findwindow API to find the corresponding form handle.

    1. [Dllimport ("user32.dll", setlasterror = true)]
    2. Static extern intptr findwindow (string lpclassname, string lpwindowname );

CopyCode

You can call this API to find the window handle by the class name of the window or the name of the window title.

Next, we need to find an API to close the dialog box. Here I use enddialog

    1. [Dllimport ("user32.dll")]
    2. Static extern bool enddialog (intptr hdlg, out intptr nresult );

Copy code

With these two API functions, we can close the message dialog box. The idea is to start a background work before calling MessageBox. Show.ThreadThe worker thread waits for a certain period of time to start searching for the window handle of the message dialog box. Then, it calls the enddialog API function to close the message dialog box. However, there is a problem with this method, that is, if there are multiple message dialogs with the same name at the same time (which may not necessarily belong to this application), this may close the error window and solve this problem, I have not come up with a better solution. If you have a better solution to this problem, we may discuss it together.

Based on this idea, I wrote a function for closing the message dialog box with latency.

    1. Public void showmessageboxtimeout (string text, string caption,
    2. Messageboxbuttons buttons, int timeout)
    3. {
    4. Threadpool. queueuserworkitem (New waitcallback (closemessagebox ),
    5. New closestate (Caption, timeout ));
    6. MessageBox. Show (text, caption, buttons );
    7. }

Copy code

The unit of timeout parameter in this function is millisecond. Other parameters have the same meaning as MessageBox. Show parameters, which are not described here.

This function uses the thread pool to call a working thread closemessagebox, and transmits the title and delay of the dialog box to the closemessagebox function through the closestate class.

Closestate is defined as follows:

  1. Private class closestate
  2. {
  3. Private int _ timeout;
  4. /** // <Summary>
  5. /// In millisecond
  6. /// </Summary>
  7. Public int timeout
  8. {
  9. Get
  10. {
  11. Return _ timeout;
  12. }
  13. }
  14. Private string _ Caption;
  15. /** // <Summary>
  16. /// Caption of Dialog
  17. /// </Summary>
  18. Public String Caption
  19. {
  20. Get
  21. {
  22. Return _ Caption;
  23. }
  24. }
  25. Public closestate (string caption, int timeout)
  26. {
  27. _ Timeout = timeout;
  28. _ Caption = Caption;
  29. }
  30. }

Copy code

The last step is the closemessagebox function. Check the Code directly.

    1. private void closemessagebox (object state)
    2. {
    3. closestate = state as closestate;
    4. thread. Sleep (closestate. Timeout);
    5. intptr DLG = findwindow (null, closestate. Caption);
    6. If (DLG! = Intptr. Zero)
    7. {
    8. intptr result;
    9. enddialog (DLG, out result);
    10. }
    11. }

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.