Focus problem in 2 modeless dialog boxes

ef33 used Ask the Experts™

I have some general modeless custom Dialog box, which may contain 2 different custom Dialog boxes (they appear exclusively – the first or the second, but not the both).

For the simplicity, assume that:

General dialog box contains only the "Close" button
First child dialog contains only the "Show" button
Second child dialog contains only some static text (no buttons)

When my general dialog box contains the first child dialog I need to set the focus on "Show" child dialog's button, whereas when the general dialog box contains the second child dialog I need to set the focus on "Close" general dialog's button.

The only possible solution, which I found is to check conditionally in OnSetFocus() of General dialog box, who is its child and to call SetFocus appropriately.  However, it's not too good-looking solution especially in my case, since the general dialog and the child dialogs reside in different MSVC projects.

Thanks a lot !
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

  So ...

  General + First Focus = First Dialog 'Show' Button
  General + Second Focus = General Dialog 'Close' Button

  I assume General by itself 'Close' Has Focus!

  Try This...

  In the Properties tab of the General 'Close' button click the DefaultButton checkBox. Do the Same in the First Dialog, check the DefaultButton checkbox for 'Show'.

  When the First Dialog Launches Show will have the Focus
  When You launch the Second Dialog immediately Set the Focus Back to the General Dialog

 secondDlg->ShowWindow ();
 secondDlg->MoveWindow (...); // move window away from General Dialog
 this->SetFocus ();  // Set focus to General

 Let me know if this is the solution you are looking for.


Hi williamcampbell !

I don't know wheather your solution works or not, but the problem is that in real application I have only one dialog of type "First Child", but dozens of dialogs of type "Second Child". So it's not too practical to start overriding all these OnFocus of all theese numereous dialogs.

Alternatively, I found another very dirty way to solve the problem by overriding OnPaint of the "First Dialog". The possible solution would be to find another message, which received when my dialog appears...
 Possible solution at end of this post

  RE: Multiple Dialogs

 For the Second child Dialogs You should create a baseclass


 And derive subsequent Dialogs from that

 That way you only have to write the function once

 CBaseSecondaryDialog : public CDialog
            CBaseSecondaryDialog ( CWnd *pFirst, IDD, ....
                   pFirst->BringWindowToTop ();
                   pFirst->SetFocus ();
 COtherDialog : public CBaseSecondaryDialog ...

 A better solution might be Register a new message and BroadCast it

 UINT uDialogActiveMsg;  
uDialogActiveMsg = RegisterWindowMessage("ActiveDialog");

 When a Second Dialog Starts do this ..

 SendMessage ( HWND_BROADCAST ,uDialogActiveMsg, 0, 0L);

 In the First Dialog Listen for this Message by Setting up a MessageHandler

 When you get the message make youself the focus window


I very appreciate your help.
I made something similar to what you advised: the base class of the dialogs, which don’t need the focus, send WM_SETFOCUS message back to the general dialog, whereas in order not to enter infinite loop in OnSetFocus of the general dialog, this time I pass special value in LPARAM

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial