Environment: VC6, win2000
It is not uncommon to need a dialog box that can change its appearance based on selections made within the dialog box. (Hence the need for tab and property sheets.) Often, tab controls or property sheets can be used to accomplish such dynamic behavior. Sometimes, however, there are so many possible selections and combinations of selections, that tab or property sheets are impractical.
This technique shows a simple example of how to dynamically change a dialog box based on the selection of a list box.
The CDynamicDlgDlg::Initialize() method shown below is called from the CDynamicDlgDlg::OnInitDialog() method. Each of the CDialogs in the CDialogStructure shown below is a dialog class with CHILD and THIN_FRAME properties. This could also be done in an array of CDialogs, but then functions and attributes specific to the “new” dialog classes would not be available.
typedef struct
{
CButtonDlg buttonDlg;
CRadioDlg radioDlg;
CStaticTextDlg staticTextDlg;
}CDialogStructure;typedef enum
{
BUTTON_DLG,
RADIO_DLG,
STATIC_TEXT_DLG,
NUM_DLGS
}eDlgType;void CDynamicDlgDlg::Initialize()
{
CDialog* dlg;
numDlgs = NUM_DLGS;
currentDlg = BUTTON_DLG;
CRect currentDlgRect, parentDlgRect;dialogStructure.buttonDlg.Create(IDD_BUTTON_DLG, this);
dialogStructure.radioDlg.Create(IDD_RADIO_DLG, this);
dialogStructure.staticTextDlg.Create(IDD_STATIC_TEXT_DLG, this);GetClientRect(parentDlgRect); //parentDlgRect = GetMyRectangle();
for(int i = 0; i < NUM_DLGS; i++)
{
dlg = GetDlgAt(currentDlg);
dlg->GetClientRect(currentDlgRect);
dlg->SetWindowPos((const CWnd*)&wndTop,
(parentDlgRect.right-currentDlgRect.right)/2, \
(parentDlgRect.bottom – currentDlgRect.bottom)/2, \
currentDlgRect.right,
currentDlgRect.bottom,
SWP_SHOWWINDOW);
dlg->ShowWindow((i==BUTTON_DLG)?SW_SHOW:SW_HIDE);
currentDlg = currentDlg + 1;
}
currentDlg = BUTTON_DLG;
}CDialog* CDynamicDlgDlg::GetDlgAt(int dlgType)
{
switch(dlgType)
{
case BUTTON_DLG:
return &dialogStructure.buttonDlg;
case RADIO_DLG:
return &dialogStructure.radioDlg;
case STATIC_TEXT_DLG:
return &dialogStructure.staticTextDlg;
default:
return NULL;
}
}void CDynamicDlgDlg::OnSelchangeDialogDropList()
{
CDialog* dlg;
UpdateData(true);
dlg = GetDlgAt(currentDlg);
dlg->ShowWindow(SW_HIDE);
dlg = GetDlgAt(dialogType);
dlg->ShowWindow(SW_SHOW);
currentDlg = dialogType;
}