简体   繁体   中英

SendMessage and WM_CLOSE

I am creating a simple C# app to monitor vended application on the server and if any message boxes come up with a particular window name to close the window and continue with the process. Vendor has informational message boxes come up randomly which hangs the application until X or OK buttons are clicked. There are two types of message boxes that come up and one gets closed with no problem using my application but the other one does not. They both look very similar its just that one has embedded sql text as part of the message. Both of the Message Boxes are found by FindWindow its just when the SendMessage gets run it does not close the second type of the message box even though it sees it. I have also tried PostMessage and I get the same issue.

This is what I have:

    private const int WM_CLOSE = 0x10;

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    IntPtr ErrorPopUp = FindWindow(null, WindowToClose.Trim());
                        if (ErrorPopUp != IntPtr.Zero) {
                            try {
                                Thread.Sleep(200);
                                SendMessage(ErrorPopUp, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
                                _WMExceptionDal.LogErrorMsg(_WMException);

                            } catch (Exception ex) {
                                _WMException.txt_iferror = "Unable to close the popup window";
                                _WMException.txt_sqlerrtext = ex.ToString();
                                _WMExceptionDal.LogErrorMsg(_WMException);
                            }
                        }

Any suggestions of other approach or if I am doing something wrong of why some message boxes would get closed by WM_CLOSE and why some wouldn't I would really appreciate it.

The answer is simpler than you think: the popups you get for errors have an 'OK' button, and no 'Close' button in the title bar area of the window.

They are very, very primitive: the window proc's message-handling is limited to the functions behind the dialog buttons:

SendMessage(ErrorPopUp, WM_COMMAND, IDOK, IntPtr.Zero);

If you send it WM_CLOSE there's nobody home: there is nothing in the message-handler for that window that will respond to that message. Yes, there was a time when I thought that all windows responded to WM_CLOSE messages, too.

There are also restrictions on dialog windows responding to 'Close' messages coming in from other threads, but I don't think that's what's happening here. (However, if the dialog box does have an 'x' button to close it in the title bar, and it's not responding to your WM_CLOSE messages, this is the most likely explanation).

Either way, you're sending it the wrong message: the dialog was created to respond to the user inputs listed on the buttons, and those are the messages you send - WM_COMMAND with a message identifier:

private const int WM_COMMAND = 0x111;
private const int dlgOK = 0x1; private const int dlgCANCEL = 0x2; private const int dlgABORT = 0x3; private const int dlgRETRY = 0x4; private const int dlgIGNORE = 0x5; private const int dlgYES = 0x6; private const int dlgNO = 0x7;

The question 'Which Identifier?' is usually irrelevant - you know it's 'Ok' here - but the User32 function GetDlgItem() will check which dialog box functions are present if you need to ask.

Also: watch out for dialogs with a 'Cancel' button - there are implementations of the Cancel function in a dialog that enable the window menu and give you a close 'x' button in the title bar, and some of these implementations respond to the WM_CLOSE message (they ought to respond to WM_SYSCOMMAND, SC_CLOSE too); but you would be unwise to rely on anything other than WM_COMMAND, dlgCANCEL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM