Search code examples
winapidialogfocusrequirez-order

Win32 - Make dialog require focus/action


In Win32 programming the MessageBox function launches a dialog that requires action, you cannot give focus to another window in the application until you press 'OK'. Well I have a dialog box that I designed in the resource editor of Visual Studio 2008 and the problem is that I can click the menu-item that launches it, and while that dialog is still being showed I can click it again and show another one of them. Also I can use the rest of my application while the dialog is open.

I would like to make it so that the user must press 'OK' or 'Cancel' on my custom dialog to do anything else at all in the application, as if it were a MessageBox. How would I accomplish this? Is there a specific windows style or ex-style that I can set on my Dialog resource? I have tried WS_TOPMOST but I got no effect, I'm not sure what else to try.


Solution

  • Your dialog needs to be launched with DialogBox()., DialogBoxParam(), or any of the direct dialog modal launchers. Further, you need to be sure and provide the top-level window of your application as the parent window when you you call it. Doing this should make your dialog modal, rather than modeless. It is important that you also use the WS_POPUP window style for your dialog template in the resource script, but it is usually set by default, so that is likely not your problem.