Set MFC dialog focus?

Posted on 2009-05-16
Last Modified: 2013-12-21
If I have an MFC dialog and dynamically create controls on it e.g a tab control.  How do I set focus on the main parent dialog afterwards?
Question by:Wanting2LearnMan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Accepted Solution

lucky_james earned 500 total points
ID: 24401888
you can use WM_NEXTDLGCTL message

check out:


Expert Comment

ID: 24401890
alternatively, you can use CWnd::SetForegroundWindow() to move the dialog up in z-order


Author Comment

ID: 24401902
OK Thanks,

Can you tell me how to use this in my OnInitFunction??  InMy Oninit I create a tab control like so:
dlgTab1Dlg = new CTab1(this);
     dlgTab1Dlg->Create(IDD_TAB1_DIALOG, this);
     dlgTab1Dlg->SetWindowPos(NULL, rTab.left,, rTab.Width(), rTab.Height(), SWP_NOZORDER);

     dlgTab2Dlg = new CTab2(this);
     dlgTab2Dlg->Create(IDD_TAB2_DIALOG, this);
     dlgTab2Dlg->SetWindowPos(NULL, rTab.left,, rTab.Width(), rTab.Height(), SWP_NOZORDER);

anf after this I want focus to go to my main dialog.

Expert Comment

ID: 24401958
is the tab control the part of your main dialog.
if not, fetch the handle to the main dialog window.
then use
PostMessage with required LParam and WParam:

for example, check out:

for details of message, check out:

Do let me know if you have any queries.


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Thin secure Windows 10 5 116
Path to  STL Map header file 1 91
compass android app not working on moto 4g android phone 5 104
Is there a Google Voice API? 9 70
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator ( This article presumes a wor…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question