C++ – How to close a MessageBox window by its handle with C++

cmessageboxwinapiwindows

I have a multi-threaded application that may display a MessageBox for a user's interaction. The message box itself is displayed from a worker thread, after a user picks a context menu command from the app's system tray icon, so the user can technically continue using the app while the message box is displayed. This works great until a user issues "Exit" command, at which point I need to find a way to close any open message boxes.

I did my homework and I was able to obtain HWND handle for the main (dialog) window of the message box (using this method.) I checked the HWND to be correct using Spy++, so HWND itself is not the issue. What happens is that when I do PostMessage(hMsgBoxWnd, WM_CLOSE, 0, 0); from another thread to the message box, it simply ignores this message and doesn't close.

Any idea how to close the message-box by its window handle?

Best Answer

MessageBox() simply does not process WM_CLOSE in all sitations:

SendMessage/PostMessage WM_CLOSE to MessageBox window does not always work

Related Topic