5,276,406 members and growing! (16,272 online)
Email Password   helpLost your password?
Desktop Development » Dialogs and Windows » General     Intermediate

Dynamic Dialog Class

By Marcel Scherpenisse

Create dialogs dynamically, and easily, without the need for a dialog resource script.
VC6, VC7, C++Windows, NT4, Win2K, WinXP, MFC, VS6, VS, Dev

Posted: 14 Feb 2000
Updated: 19 Jun 2002
Views: 126,913
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
31 votes for this Article.
Popularity: 6.45 Rating: 4.32 out of 5
1 vote, 12.5%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
3 votes, 37.5%
4
4 votes, 50.0%
5

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

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

Tip!

Possible extensions:

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

License

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



Occupation: Web Developer
Location: Netherlands Netherlands

Other popular Dialogs and Windows articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 45 (Total in Forum: 45) (Refresh)FirstPrevNext
Subject  Author Date 
Generalanother approach to dynamic dialogs [modified]memberMikeDunlavey3:42 24 Nov '07  
QuestionAdding a button to an existing dialog class dynamicallymemberodette77615:43 29 Nov '06  
GeneralCrear con otra vntana padrememberucsm16:03 14 Oct '06  
GeneralRe: Crear con otra vntana padrememberlexugax23:38 22 Mar '07  
GeneralRe: Crear con otra vntana padrememberKeia21:37 26 May '08  
GeneralIs possible to add a control to an existing dialogmemberdtgarry4:25 15 Jun '06  
AnswerRe: Is possible to add a control to an existing dialogmemberMarcel Scherpenisse6:21 15 Jun '06  
Questionto add new control to dynamic dialog box at runtimememberwavesss23:50 5 Apr '06  
AnswerRe: to add new control to dynamic dialog box at runtimememberMarcel Scherpenisse23:58 5 Apr '06  
GeneralSpinner message solution !membersaviorman4:39 28 Feb '06  
GeneralInspirationsupporterRedZenBird9:33 17 Sep '05  
GeneralDynamic Modeless dialog bugmemberMNN Murthy9:38 6 May '04  
GeneralRe: Dynamic Modeless dialog bugsussAnonymous1:30 7 Mar '05  
GeneralHOw to Add the Contents to Combo BoxmemberDELIVER THE PROMISE20:35 16 Dec '03  
GeneralRe: HOw to Add the Contents to Combo BoxmemberPaul Wolfensberger13:01 4 Aug '04  
GeneralRe: HOw to Add the Contents to Combo BoxmemberMarcel Scherpenisse3:46 5 Aug '04  
GeneralRe: How to Add the Contents to Combo BoxmemberJordan Walters23:56 4 Jan '05  
GeneralRe: How to Add the Contents to Combo Boxmemberrudywang20:39 20 Sep '06  
GeneralGood workmemberVitali Halershtein6:01 30 Sep '03  
GeneralAWTmemberGeMe_Hendrix2:21 23 Jul '03  
Generalbuttons with bitmapsmemberFreespirit3:48 13 Feb '03  
GeneralHow to add Scrollingmembermarigapudi6:02 6 Jan '03  
GeneralTab Control Usemembermcordon0:49 26 Dec '02  
QuestionRe: Tab Control Usememberkdobek14:08 3 Jul '08  
GeneralSupport for controls not standartmemberamauta10:27 20 Nov '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Jun 2002
Editor: Chris Maunder
Copyright 2000 by Marcel Scherpenisse
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project