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


Comments

  • another way to do this

    Posted by MikeDunlavey on 05/17/2007 01:15pm

    (Copy of same comment on 5053) There is a kind of dynamic dialog that is like this one except that it allows the dialog to be incrementally restructured without any additional programming, and all of its callbacks and event handlers are buried in the class and do not have to be modified. Basically, the idea is you have a method called "Contents" that paints the controls, like "Initialize" above. The difference is that the class has an "Update" method that can be called at any time that is guaranteed to leave the controls exactly as if they had just been painted for the first time. It does this by means of a control structure called differential execution. A simplified animation is shown here: http://en.wikipedia.org/wiki/Differential_Execution#Dynamic_Dialog

    Reply
  • Updating controls properties after creation.

    Posted by Legacy on 01/29/2004 12:00am

    Originally posted by: Keio Kalamies

    How can I update controls properties after I have created it with CreateEx? I would like to change the controls rect.
    For example: make a button larger as the dialog becomes larger.

    Reply
  • radiobutton on XP

    Posted by Legacy on 12/01/2003 12:00am

    Originally posted by: ucs

    When I create a dialog with radiobutton on XP, the button will be a black square, on win2k it is good.
    What can I do?

    Reply
  • How to read the contents of EditBox added Dynamically

    Posted by Legacy on 07/29/2003 12:00am

    Originally posted by: shiva

    Hi I want to know once the Edit Box is dynamically added how to get the contents of the Edit Box

    Reply
  • A way to add control data for later updates

    Posted by Legacy on 01/21/2003 12:00am

    Originally posted by: Ask Ari

    Hi, 
    
    Just wondering if there is a better way of remebering and
    updating data. The way I do it right now, is in addition
    to AddDlgControl, I have a AddControlData method that is
    overloaded for various controls. For example, in the case
    of Edit control - setLimitText & in the case of combo
    control - the display items, setting initial selection
    etc. I don't pass the data directly in the AddDlgControl
    method.

    Have a question regarding Combo Boxes - how do I set the
    dropdown height - Can't get it to work. Any tips would
    be great.

    Reply
  • How to implement dynamic dialog in ScrollView?

    Posted by Legacy on 12/10/2002 12:00am

    Originally posted by: Gerry Leddy

    Hello,
    I have found this example very useful. I need to put a dynamic dialog very similiar to this in a scrollview. The scrollview will be part of a three way splitter. Any suggestions are welcomed.

    Thanks,
    Gerry

    Reply
  • How to trap messages for a particular control

    Posted by Legacy on 12/06/2002 12:00am

    Originally posted by: shashi

    How do i trap messages for a particular control. i am traping the messages in oncommad function of the class derived from CDynDialogEx. my problem is if i am traping EN_KILLFOCUS, it is executing for all the edit controls i have on dialog box. But iwant to trap for particular edit box only. how do i get this. I have created these controls dynamically and application generated id. here is the code.
    
    


    BOOL CDlg::OnCommand(WPARAM wParam, LPARAM lParam)
    {
    //wParam
    //The low-order word of wParam identifies the command ID of the menu item, control, or accelerator.
    //The high-order word of wParam specifies the notification message if the message is from a control.
    //If the message is from an accelerator, the high-order word is 1.
    //If the message is from a menu, the high-order word is 0.

    //lParam
    //Identifies the control that sends the message if the message is from a control. Otherwise, lParam is 0.

    WORD wControlID = LOWORD(wParam);
    int wMessageID = HIWORD(wParam);
    HWND hWndCtrl = (HWND)Param;
    //Control handle
    char chVal[256];
    CString strVal;
    if (wControlID != 0) {
    switch (wMessageID) {
    case EN_KILLFOCUS:
    GetDlgItemText(wControlID,chVal,256);
    strVal=chVal;
    AfxMessageBox(strVal);
    break;
    }
    }

    return CDialog::OnCommand(wParam, lParam);
    }

    Reply
  • How to hold DLGTEMPLATE structure at runtime

    Posted by Legacy on 11/15/2002 12:00am

    Originally posted by: narasimham

    How do i get hold of DLGTEMPLATE structure at runtime...after changing the positions of the controls opn a dialog at runtime...Help needed

    Reply
  • How to add more controls than client screen can hold?

    Posted by Legacy on 12/24/2001 12:00am

    Originally posted by: Krishna Kadali

    Hi,

    I am trying to use this Dynamic Dialog demo to create a number of controls in one of our dialogs. Dialog controls are getting created correctly as long as the co-ordinates are within the client screen area. I am trying to integrate this sample with a Scrolling Dialog sample that I saw else where on this site. Scrolling Dialog allows me to have more controls on the dialog than my screen area will allow. But this dynamic dialog sample fails to create the control if the co-ordinates are beyond the client screen area. How can I get this to work? Any ideas would be greatly appreciated.

    You can see this behaviour in this sample by trying to add more controls than a screen can hold.

    Thanks,
    Krishna.

    Reply
  • Different font for each control??

    Posted by Legacy on 10/01/2001 12:00am

    Originally posted by: Doug Sisco

    Is there any way to initialize each dynamic control with its own font?

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this webcast that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base application that will jumpstart you into building your own more complex app …

  • On-demand Event Event Date: September 17, 2014 Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this webcast and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the future state of your servers, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds