Dynamic Dialog Class | CodeGuru

Dynamic Dialog Class

Environment: Windows 2000 RC2, Windows NT4 SP6, Visual C++ 6 SP3 Dynamic Dialog class These classes are being used for displaying a modal dialog, on which the controls are dynamically added, without the need of having a dialog template as resource. These classes were developed as base classes for use in a script parser, where […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 16, 2000
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: Windows 2000 RC2, Windows NT4 SP6, Visual C++ 6 SP3

Dynamic Dialog class

These classes are being used for displaying a modal dialog,
on which the controls are dynamically added,
without the need of having a dialog template as resource.
These classes were developed as base classes for use in a script parser,
where users can build there own dialogs, using a VB-script alike language.
So basically there can be any number of controls on the dialog,
at any place on the dialog.

Global structure of the CDynDialogEx class:

  • class is derived of CDialog
  • in the class there is an array of CDynDialogItemEx pointers, the dialog controls
  • class includes DoDataExchange() function.
  • add controls to dialog through function AddDlgControl()

Global structure of the CDynDialogItemEx class:

  • holds the data of the control that was added to the dialog, like the
    caption, the rectangle, etc.
  • creates the controls on the dialog

Small piece of sample code on how to use the classes

void CTestDynDialogDlg::OnButton1();
{
 int nRadio1 = 0;

 //Create a rectangle in dialog units, where 
 //the control should be placed
 CRect rect(10,5,60,19);

 //create the dynamic dialog, 
 //using this as parent window
 CDynDialogEx dlg(this);

 dlg.SetWindowTitle(_T("Dynamic Dialog : WindowTitle....."));

 //Add a button control at the given position
 dlg.AddDlgControl(_T("BUTTON"), // Type of control OR control classname
                   _T("Press me!"), // Caption of control
                   STYLE_BUTTON,    // dwStyle of control
                   EXSTYLE_BUTTON,  // dwStyleEx of control
                   &rect,           // Position of control on dlg in dlg units
                   NULL,            // void ptr to DDX var - default=NULL
                   IDC_DYN_BUTTON); // control ID - default=zero

 //Add a group of radio buttons
 //variable nRadio1 is used for DDX
 dlg.AddDlgControl(_T("BUTTON"),
                   _T("Radio1Caption 1"),
                   STYLE_RADIO_GROUP,
                   EXSTYLE_RADIO,
                   NULL,
                   (void*)&nRadio1);

 dlg.AddDlgControl(_T("BUTTON"),
                   _T("Radio1Caption 2"),
                   STYLE_RADIO,
                   EXSTYLE_RADIO);

 //Now show me the dialog
 dlg.DoModal()
}

Working explained

  • CDynDialogEx::AddDlgControl() function creates new object of class CDynDialogItemEx
    and adds it to the array of controls. Function also checks/sets the size of
    the dialog, so the control is seen on the dialog.
  • CDynDialogEx::DoModal() function initializes the DLGTEMPLATE structure
    using the selected font and calls CDialog::InitModalIndirect()
  • CDynDialogEx::OnInitDialog() function creates all the controls on the
    dialog
  • CDynDialogItemEx::CreateEx() function converts from dialog units to
    screen units and creates the control

Possible extensions:

  • adding ActiveX controls dynamically to the dialog
  • adding Menus dynamically to the dialog

Downloads

Download demo project – 30 Kb
Download source – 8 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.