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.
- [Dllimport ("user32.dll", setlasterror = true)]
- 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
- [Dllimport ("user32.dll")]
- 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.
- Public void showmessageboxtimeout (string text, string caption,
- Messageboxbuttons buttons, int timeout)
- {
- Threadpool. queueuserworkitem (New waitcallback (closemessagebox ),
- New closestate (Caption, timeout ));
- MessageBox. Show (text, caption, buttons );
- }
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:
- Private class closestate
-
- {
-
- Private int _ timeout;
-
-
- /** // <Summary>
-
- /// In millisecond
-
- /// </Summary>
-
- Public int timeout
-
- {
-
- Get
-
- {
-
- Return _ timeout;
-
- }
- }
-
-
- Private string _ Caption;
-
-
- /** // <Summary>
-
- /// Caption of Dialog
-
- /// </Summary>
-
- Public String Caption
-
- {
-
- Get
-
- {
-
- Return _ Caption;
-
- }
- }
-
-
- Public closestate (string caption, int timeout)
-
- {
-
- _ Timeout = timeout;
-
- _ Caption = Caption;
-
- }
-
- }
Copy code
The last step is the closemessagebox function. Check the Code directly.
- private void closemessagebox (object state)
- {
- closestate = state as closestate;
-
- thread. Sleep (closestate. Timeout);
- intptr DLG = findwindow (null, closestate. Caption);
-
- If (DLG! = Intptr. Zero)
- {
- intptr result;
- enddialog (DLG, out result);
- }
- }