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 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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read