Sign in to follow this  
experiment

Windows API - force focus on window

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
I have a resource file (.rc)

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


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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this