Embed 'child' dialogs into your main dialog | CodeGuru

Embed ‘child’ dialogs into your main dialog

Environment: VC6, VC5 One of the challenges I was recently set was to produce some code that would allow ‘child’ dialogs to be added to a ‘parent’ dialog at run time so that all the controls operated as if they were part of the original dialog. To this end I came up with some code […]

Written By
CodeGuru Staff
CodeGuru Staff
Jun 22, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: VC6, VC5

One of the challenges I was recently set was to produce some
code that would allow ‘child’ dialogs to be added to a ‘parent’ dialog at run time
so that all the controls operated as if they were part of the original dialog.

To this end I came up with some code that reads the dialog template resource file
of the child dialogs and embeds the controls onto the parent dialog.

Using the code is simple. Derive your parent dialog from the CMultiDialog class and call
the AddDialog function to embed your child dialog. The first parameter is the ID of the child
dialog template and the second parameter is the ID of a marker control (usually a static control) that
marks the position of the child control. This marker control will be hidden after the child controls have
been embedded.

Place follow lines into the OnInitDialog function of the parent dialog.
// Embed the dialog
AddDialog( IDD_DIALOG1, IDC_MARKER );

Access controls on the dialog as per a normal dialog control. eg.:-

   m_pListBox = (CListBox*) GetDlgItem( IDC_LIST1 );
   if ( m_pListBox ) {
      m_pListBox->AddString( "First" );
      m_pListBox->AddString( "Second" );
      m_pListBox->AddString( "Third" );
   }

I’ve not extensively tested this code – so don’t shoot me if it breaks! :o)

Downloads

Download demo project – 33 Kb

Download source – 3 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.