VC++ Programming @ UESTC
1
Chapter 6 The Modal Dialog
and Windows Common Controls
What does modal dialog means?
How to use control palette to add controls to your
dialog window?
Understand the DoModal() and DoDataExchange()
How to add dialog control at runtime?
What are the windows common control and how to
program with them.
VC++ Programming @ UESTC
2
Almost every Windows-based program uses a dialog to
interact with the user.
There’re two kinds of dialogs,Modal and Modeless.
With a modal dialog,the user cannot work elsewhere in the
same application (more correctly the same user interface
thread) until the dialog is closed.
With modeless dialog,the user can work in another window
while the dialog is on the screen.
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
3
A dialog window almost always tied to a Windows resource
that identifies the dialog’s elements and specifies their
layout.
The elements on the dialog are called dialog controls,which
include edit control,buttons,list boxes,combo boxes,labels,
tree views,progress indicators,sliders and so forth.
Windows manages these controls using special grouping
and tabbing logic.
The dialog controls can be referenced by the CWnd pointer
or by an index number.
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
4
Steps to add a modal dialog to an existing porject:
1,Use the dialog editor to create a dialog resource
that contains various controls.
2,Use ClassWizard to create a dialog class that is
derived from Cdialog and attached to the resource
created in step1.
3,Use ClassWizard to add data member,exchange
functions and validation functions to the dialog
class.
4,Use ClassWizard to add message handler.
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
5
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
6
5,Write codes for special control initialization (in
OnInitDialog) and for the message handler,Check the
OnOK function.
6,Write codes in your view class to activate the dialog,Call
constructor first,then call DoModal() dialog member
function.
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
7
Ex06A visited.
Use property page to setup parameters for different controls.
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
8
The Group control serves only to group two radio buttons.
Be sure that both radio buttons have the auto property set on
the style tab and the Hourly button has the Group property
set.
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
9
Control palette
If it is not visible,right click anytoolbar and choose
controls from the list.
Setting the tabbing order on the dialog
1,click the dialog and you’ll see a layout menu occur;
2,Choose Tab Order;
3,Use the mouse to set the right order;
4,Holding down the Alt key and pressing the underlined
letter on the label,enables the user to jump to the next
control in tabbing order.
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
10
Combo box
1,Simple,User can see all choices.
2,Drop-down,User can type in any word and choose
from the drop-down list.
3,Drop List,Use can only select from the attached List
Box
4,Use,Ctrl-Enter” to add new line on the,Data” tab
section for three types of combo box.
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
11
Choose the combo type from,style” tab section
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
12
List Box
You may choose two items at one time,But there’s no
initial choice for the list,So do initialization at runtime.
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
13
BOOL CEx06aDialog::OnInitDialog() {
CListBox* pLB = (CListBox*) GetDlgItem(IDC_DEPT);
pLB->InsertString(-1,"Documentation");
…..
pLB->InsertString(-1,"Security");
CScrollBar* pSB = (CScrollBar*)
GetDlgItem(IDC_LOYAL);
pSB->SetScrollRange(nMin,nMax);
pSB = (CScrollBar*) GetDlgItem(IDC_RELY);
pSB->SetScrollRange(nMin,nMax);
// Call after initialization
return CDialog::OnInitDialog();
}
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
14
Use ClassWizard to add a dialog class for the dialog
resource.
1,Choose CDialog as base class
2,Add member variables
3,Add message handlers for the special controls
4,Do necessary initialization work
5,Connect the dialog to the view class
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
15
The DoDataExchange() will connect the control IDs with the
dialog member variables and set the right values at runtime.
void CEx06aDialog::DoDataExchange(CDataExchange* pDX){
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEx06aDialog)
DDX_Text(pDX,IDC_BIO,m_strBio);
DDX_Radio(pDX,IDC_CAT,m_nCat);
……
DDX_CBString(pDX,IDC_SKILL,m_strSkill);
DDX_Text(pDX,IDC_SSN,m_nSsn);
DDV_MinMaxInt(pDX,m_nSsn,0,999999999);
//}}AFX_DATA_MAP
}
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
16
The CWnd::UpdateData(FALSE) read data from member
variables to controls;
The CWnd::UpdateData(TRUE) write data to the member
variables from the controls.
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
17
Ex06B Windows common controls
Treeview,listview,Progress Indicator,Trackbar,Spin
Buttton.
Focus on the treeview and listview.
There’s an image list for both of above.
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
18
The steps to make an image list:
1,Create Icons in the resource editor for images
2,Give each icon an unique ID
3,Add a private CImageList data member
4,In the dialog initial function OnInitDialog(),fill up the
image list with icons like following:
HICON hIcon[8]; int n;
m_imageList.Create(16,16,0,8,8);
// 32,32 for large icons
hIcon[0] = AfxGetApp()->LoadIcon(IDI_WHITE);
……..
hIcon[7] = AfxGetApp()->LoadIcon(IDI_GREEN);
for (n = 0; n < 8; n++)
{ m_imageList.Add(hIcon[n]);}
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
19
Program with List control
Initialization
static char* color[] = {"white","black","red",
"blue","yellow","cyan",
"purple","green"};
CListCtrl* pList =
(CListCtrl*) GetDlgItem(IDC_LISTVIEW1);
pList->SetImageList(&m_imageList,LVSIL_SMALL);
for (n = 0; n < 8; n++) {
pList->InsertItem(n,color[n],n);
}
pList->SetBkColor(RGB(0,255,255)); // UGLY!
pList->SetTextBkColor(RGB(0,255,255));
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
20
Program with List control
Message LVN_ITEMCHANGED
void CEx06bDialog::OnItemchangedListview1(NMHDR* pNMHDR,
LRESULT* pResult) {
NM_LISTVIEW* pNMListView =(NM_LISTVIEW*)pNMHDR;
CListCtrl* pList =(CListCtrl*) GetDlgItem(IDC_LISTVIEW1);
int nSelected = pNMListView->iItem;
if (nSelected >= 0) {
CString strItem = pList->GetItemText(nSelected,0);
SetDlgItemText(IDC_STATIC_LISTVIEW1,strItem);}
*pResult = 0;
}
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
21
Program the tree control
Initialization
CTreeCtrl* pTree =(CTreeCtrl*)
GetDlgItem(IDC_TREEVIEW1);
pTree->SetImageList(&m_imageList,TVSIL_NORMAL);
// tree structure common values
TV_INSERTSTRUCT tvinsert;
tvinsert.hParent = NULL;
……
tvinsert.item.cchTextMax = 6;
tvinsert.item.iSelectedImage = 1;
tvinsert.item.cChildren = 0;
tvinsert.item.lParam = 0;
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
22
Insert item to Treeview,Top level
tvinsert.hParent = NULL;
…..
tvinsert.item.pszText = "Homer";
tvinsert.item.iImage = 2;
HTREEITEM hDad = pTree ->InsertItem(&tvinsert);
tvinsert.item.pszText = "Marge";
HTREEITEM hMom = pTree->InsertItem(&tvinsert);
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
23
Program Treeview,second level
tvinsert.hParent = hDad;
tvinsert.item.pszText = "Bart";
tvinsert.item.iImage = 3;
pTree->InsertItem(&tvinsert);
tvinsert.item.pszText = "Lisa";
pTree->InsertItem(&tvinsert);
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
24
Message TVN_SELCHANGED
void CEx06bDialog::OnSelchangedTreeview1(NMHDR* pNMHDR,LRESULT*
pResult) {
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
CTreeCtrl* pTree =(CTreeCtrl*) GetDlgItem(IDC_TREEVIEW1);
HTREEITEM hSelected = pNMTreeView->itemNew.hItem;
if (hSelected != NULL) {
char text[31];
TV_ITEM item;
item.mask = TVIF_HANDLE | TVIF_TEXT;
item.hItem = hSelected;
item.pszText = text;
item.cchTextMax = 30;
VERIFY(pTree->GetItem(&item));
SetDlgItemText(IDC_STATIC_TREEVIEW1,text);}
*pResult = 0;
}
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
25
WM_NOTIFY message
For some common control,the 32bit WM_COMMAND message
are nor sufficient for holding the information that needs to be
sent to its parent.
So MS defines a new WM_NOTIFY which has two elements,
wParam is the control ID;
lParam is point to NMHDR;
typedef struct tagNMHDR{
HWND hwndFrom; // handle to control sending the message
UINT idFrom; // ID of control sending the message
UINT code; // control-specific nitification code
} NMHDR;
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
26
Many control send WM_NOTIFY message with pointers to
structures larger than NMHDR,Those structures contain the
three member above plus appended control specific members.
Treeview control notifications,pass a pointer to a
NM_TREEVIEW structure that contains TV_ITEM structure,
a drag point and so forth.
typedef struct _NM_TREEVIEW {
NMHDR hdr;
UINT action;
TV_ITEM itemOld;
TV_ITEM itemNew;
POINT ptDrag;
} NM_TREEVIEW;
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
27
typedef struct _TV_ITEM {
UINT mask;
HTREEITEM hItem;
UINT state;
UINT stateMask;
LPSTR pszText;
int cchTextMax;
int iImage;
int iSelectedImage;
int cChildren;
LPARAM lParam;
} TV_ITEM,FAR *LPTV_ITEM;
hItem Handle to the item to which this structure refers,
Chapter 6 The Modal Dialog
and Windows Common Controls
VC++ Programming @ UESTC
28
typedef struct _TV_INSERTSTRUCT {
HTREEITEM hParent;
HTREEITEM hInsertAfter;
TV_ITEM item;
} TV_INSERTSTRUCT,FAR *LPTV_INSERTSTRUCT
Chapter 6 The Modal Dialog
and Windows Common Controls