Click here to Skip to main content
13,140,231 members (49,584 online)
Click here to Skip to main content
Add your own
alternative version


84 bookmarked
Posted 14 Feb 2000

Dynamic Dialog Class

, 19 Jun 2002
Rate this:
Please Sign up or sign in to vote.
Create dialogs dynamically, and easily, without the need for a dialog resource script.

Sample Image Testappliction - TestDynDialog.gif Sample Image Dynamic dialog - DynamicDialog.gif

Sample Image Dynamic dialog with List control - DynDialogList.gif Sample Image Dynamic dialog with ActiveX control - DynDialogActiveX.gif

Dynamic Dialog class

These classes are being used for displaying a modal/modelles 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:

  • class is derived of CWnd
  • 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);
    CDynDialogEx dlg(this);     //create the dynamic dialog, using this as parent window
    dlg.SetWindowTitle(_T("Dynamic Dialog : WindowTitle....."));

    //Add a button control at the given position
    dlg.AddDlgControl(_T("BUTTON"),    // Type of control OR classname of the control
            _T("Press me!"),    // Caption of control
            STYLE_BUTTON,    // dwStyle of control
            EXSTYLE_BUTTON,    // dwStyleEx of control
            &rect,    // Position of control on dialog in dialog units, default = NULL
            NULL,    // void pointer to variable use for DDX, default = NULL
            IDC_DYN_BUTTON);    // ID of the contol, 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

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() for modal dialogs or CDialog::CreateIndirect() for modless dialogs
  • CDynDialogEx::OnCreate() function creates all the controls on the dialog
  • CDynDialogItemEx::CreateEx() does nothing (ActiveX controls) or creates the window
  • CDynDialogEx::OnInitDialog() function repositions all the controls on the dialog or creates the ActiveX controls
  • CDynDialogItemEx::SetWindowPos() function converts from dialog units to screen units and creates ActiveX control

Update 18-06-2002

  • Class CDynDialogItemEx now derived from CWnd instead of heaving a CWnd member
  • Added modless gialog possibility as supposed by CodeBuddy. (CDynDialogEx::SetUseModeless())
  • Added subclassed controls, because I needed them. After adding the control you can use SubclassDlgItem with the Control_ID returned by calling CDynDialogEx::AddSubclassedDlgControl()
  • Improved DDX_Control support for dialog items
  • Added partial ActiveX controls support. Partial because the controls are created, but there is no support for the EVENT_SINK! But there is code for creating licensed controls dynamicly.
  • Added examples for modeless dialog, adding and filling a CListbox, an ActiveX control on a dialog


Possible extensions:

  • improving ActiveX controls support, specially EVENT_SINK related
  • adding Menus dynamically to the dialog
  • ...


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Marcel Scherpenisse
Software Developer
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionCompile as DLL? Pin
namezero1111118-Jul-10 10:21
membernamezero1111118-Jul-10 10:21 
AnswerRe: Compile as DLL? Pin
Albert van Peppen12-Sep-10 21:37
memberAlbert van Peppen12-Sep-10 21:37 
Generalanother approach to dynamic dialogs [modified] Pin
MikeDunlavey24-Nov-07 2:42
memberMikeDunlavey24-Nov-07 2:42 
QuestionAdding a button to an existing dialog class dynamically Pin
odette77629-Nov-06 14:43
memberodette77629-Nov-06 14:43 
GeneralCrear con otra vntana padre Pin
ucsm14-Oct-06 15:03
memberucsm14-Oct-06 15:03 
GeneralRe: Crear con otra vntana padre Pin
lexugax22-Mar-07 22:38
memberlexugax22-Mar-07 22:38 
GeneralRe: Crear con otra vntana padre Pin
Keia26-May-08 20:37
memberKeia26-May-08 20:37 
GeneralIs possible to add a control to an existing dialog Pin
dtgarry15-Jun-06 3:25
memberdtgarry15-Jun-06 3:25 
AnswerRe: Is possible to add a control to an existing dialog Pin
Marcel Scherpenisse15-Jun-06 5:21
memberMarcel Scherpenisse15-Jun-06 5:21 
Questionto add new control to dynamic dialog box at runtime Pin
wavesss5-Apr-06 22:50
memberwavesss5-Apr-06 22:50 
AnswerRe: to add new control to dynamic dialog box at runtime Pin
Marcel Scherpenisse5-Apr-06 22:58
memberMarcel Scherpenisse5-Apr-06 22:58 
GeneralSpinner message solution ! Pin
saviorman28-Feb-06 3:39
membersaviorman28-Feb-06 3:39 
GeneralInspiration Pin
RedZenBird17-Sep-05 8:33
memberRedZenBird17-Sep-05 8:33 
GeneralDynamic Modeless dialog bug Pin
MNN Murthy6-May-04 8:38
memberMNN Murthy6-May-04 8:38 
GeneralRe: Dynamic Modeless dialog bug Pin
Anonymous7-Mar-05 0:30
sussAnonymous7-Mar-05 0:30 
QuestionHOw to Add the Contents to Combo Box Pin
memberDELIVER THE PROMISE16-Dec-03 19:35 
AnswerRe: HOw to Add the Contents to Combo Box Pin
Paul Wolfensberger4-Aug-04 12:01
memberPaul Wolfensberger4-Aug-04 12:01 
GeneralRe: HOw to Add the Contents to Combo Box Pin
Marcel Scherpenisse5-Aug-04 2:46
memberMarcel Scherpenisse5-Aug-04 2:46 
GeneralRe: How to Add the Contents to Combo Box Pin
Jordan Walters4-Jan-05 22:56
memberJordan Walters4-Jan-05 22:56 
GeneralRe: How to Add the Contents to Combo Box Pin
rudywang20-Sep-06 19:39
memberrudywang20-Sep-06 19:39 
GeneralRe: How to Add the Contents to Combo Box Pin
LieWei6-Apr-12 14:12
memberLieWei6-Apr-12 14:12 
QuestionRe: How to Add the Contents to Combo Box Pin
Member 927478521-Jul-12 21:48
memberMember 927478521-Jul-12 21:48 
GeneralRe: How to Add the Contents to Combo Box Pin
LIYAFU6-Dec-12 1:30
memberLIYAFU6-Dec-12 1:30 
GeneralGood work Pin
Vitali Halershtein30-Sep-03 5:01
memberVitali Halershtein30-Sep-03 5:01 
GeneralAWT Pin
GeMe_Hendrix23-Jul-03 1:21
memberGeMe_Hendrix23-Jul-03 1:21 
Generalbuttons with bitmaps Pin
Freespirit13-Feb-03 2:48
memberFreespirit13-Feb-03 2:48 
QuestionHow to add Scrolling Pin
marigapudi6-Jan-03 5:02
membermarigapudi6-Jan-03 5:02 
GeneralTab Control Use Pin
mcordon25-Dec-02 23:49
membermcordon25-Dec-02 23:49 
QuestionRe: Tab Control Use Pin
kdobek3-Jul-08 13:08
memberkdobek3-Jul-08 13:08 
GeneralSupport for controls not standart Pin
amauta20-Nov-02 9:27
memberamauta20-Nov-02 9:27 
GeneralRe: Support for controls not standart Pin
ChrisJohnShort6-Mar-06 16:59
memberChrisJohnShort6-Mar-06 16:59 
QuestionWhere's the new version? Pin
naveed20-Jun-02 12:45
membernaveed20-Jun-02 12:45 
AnswerRe: Where's the new version? Pin
Chris Maunder21-Jun-02 14:25
adminChris Maunder21-Jun-02 14:25 
GeneralAdding CListCtrl Pin
Tximist25-Apr-02 0:59
memberTximist25-Apr-02 0:59 
GeneralRe: Adding CListCtrl Pin
Blagodaria25-Apr-02 1:06
memberBlagodaria25-Apr-02 1:06 
GeneralRe: Adding CListCtrl Pin
Greg Niswonger26-Oct-06 9:47
memberGreg Niswonger26-Oct-06 9:47 
GeneralDDX Pin
EVOLUTION MAN1-Oct-01 12:28
memberEVOLUTION MAN1-Oct-01 12:28 
Generalresize dialog Pin
ray4-Apr-01 5:29
memberray4-Apr-01 5:29 
GeneralRe: resize dialog Pin
Chunjying17-Apr-01 22:52
memberChunjying17-Apr-01 22:52 
GeneralAdding entries to a ClistCtrl Pin
Ron King25-Sep-00 3:54
sussRon King25-Sep-00 3:54 
AnswerRe: Adding entries to a ClistCtrl Pin
Greg Niswonger26-Oct-06 9:53
memberGreg Niswonger26-Oct-06 9:53 
GeneralRe: Adding entries to a ClistCtrl Pin
CALIFF219-Oct-09 11:39
memberCALIFF219-Oct-09 11:39 
GeneralRe: Adding entries to a ClistCtrl Pin
Greg Niswonger20-Oct-09 3:48
memberGreg Niswonger20-Oct-09 3:48 
GeneralAdding ActiveX Controls Pin
James Hunter Ross20-Sep-00 7:29
sussJames Hunter Ross20-Sep-00 7:29 
GeneralRe: Adding ActiveX Controls Pin
Marcel Scherpenisse24-Sep-00 22:19
sussMarcel Scherpenisse24-Sep-00 22:19 
QuestionHow are you supposed to adjust the controls? Pin
Jason15-Sep-00 5:06
sussJason15-Sep-00 5:06 
AnswerRe: How are you supposed to adjust the controls? Pin
Marcel Scherpenisse18-Sep-00 22:15
sussMarcel Scherpenisse18-Sep-00 22:15 
GeneralThis is great, but... Pin
Nathan Cook9-Jun-00 8:26
sussNathan Cook9-Jun-00 8:26 
GeneralRe: This is great, but... Pin
*RPM16-Apr-03 3:16
member*RPM16-Apr-03 3:16 
GeneralRe: This is great, but... Pin
KarstenK15-Mar-04 22:23
memberKarstenK15-Mar-04 22:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 20 Jun 2002
Article Copyright 2000 by Marcel Scherpenisse
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid