Windows API - force focus on window

Started by
7 comments, last by Evil Steve 14 years, 10 months ago
How can I force focus on certain dialog window? So when the user opens this dialog window, it doesn't allow him to change focus on any other window in the application until closed.. I'm thinking there's a function that takes the handle to the window as argument or something like that. using C++ and win32. [Edited by - ApochPiQ on June 2, 2009 9:24:54 PM]
Advertisement
What you're referring to is known as a modal dialog. You typically create one using an API call like DialogBox or one of the related functions.

If you can provide some additional details on your code (how you create the window, etc.) we can give you some more specific tips [smile]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

What about SetFocus()?
Quote:Original post by Vortez
What about SetFocus()?


SetFocus does not change the modality of a window. The OP wants the designated window to be modal, i.e. always in focus, not simply to activate its focus.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I have a resource file (.rc)

IDD_FILE_IMPORT DIALOG DISCARDABLE  0, 0, 200, 300STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENUCAPTION "Import"FONT 8, "MS Sans Serif"BEGIN... blah blahEND


And in main source, when clicking File->Import
case WM_COMMAND:switch(LOWORD(wParam)){case ID_FILE_IMPORT:	hwndFileImport = CreateDialog(hiFileImport, MAKEINTRESOURCE(IDD_FILE_IMPORT), hwnd, (DLGPROC)FileImportDlgProc);			ShowWindow(hwndFileImport, SW_SHOW); 			PostMessage(hwnd, WM_PAINT, 0, 0);			break;
Replace CreateDialog with DialogBox, delete the ShowWindow() and if you must repaint the parent window use InvalidateRect() rather than posting WM_PAINT. Don't forget to add a call to EndDialog somewhere in FileImportDlgProc or you'll never be able to get rid of it.

You might want to post the declaration of the FileImportDialogProc function too, the cast on the function pointer shouldn't be necessary.
adeyblue: works! exactly what i needed.
Thanks everyone for fast responses.

Placed EndDialog on both WM_CLOSE and own CANCEL button.

Quote:You might want to post the declaration of the FileImportDialogProc function too

I had to do that, cause the FileImportDialogProc is defined later.. code readability reasons.

Quote:the cast on the function pointer shouldn't be necessary.

cannot convert from 'INT_PTR' to 'HWND' is this what you were thinking about?
Actually, I can't think of a reason to hold that pointer to the function, so it isn't so important to me.. maybe just for future references.
For future reference, please don't marked threads as "solved" in the General Programming forum. Thanks! [smile]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This doesn't do what you think it does:
PostMessage(hwnd, WM_PAINT, 0, 0);
It sends a WM_PAINT message to your window, but doesn't correctly handle updating the window. In the best case it'll not work. In the worst case, it'll appear to work but break on a future version of windows or on other peoples PCs.

You probably want InvalidateRect(hwnd, NULL, FALSE);.

This topic is closed to new replies.

Advertisement