Implementing an Automatically Closed MessageBox under WinForm
Implementing an Automatically Closed MessageBox under WinForm
Realize an automatic closing MessageBox under WinForm Author: eaglet Under WinForm, we can call MessageBox.Show to display a message dialog box, prompting the user to confirm and other operations. In some applications, we need to automatically close the message dialog box through the program instead of the user clicking the confirmation button to close it. However. Net framework does not provide us with a method to automatically close the MessageBox, to achieve this function, we need to use the Window API to complete. First of all, we need to find the window handle of the message dialog box. A relatively simple method is to use the FindWindow API to find the corresponding window handle. [DllImport( “ user32.dll “ , SetLastError = true )] static extern IntPtr FindWindow( string lpClassName, string lpWindowName); This API call can 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); With these two API functions, we can close the message dialog box. The idea is to start a…