VC++ Programming @ UESTC
1
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
What is a modeless dialog?
What’s the difference between modal and modeless dialogs?
What’s the relationship between the dialog and the view
window?
Why do we need to use user-defined message in modeless
dialog?
What are the common dialogs and how to use them?
VC++ Programming @ UESTC
2
Modeless dialog allows user to work elsewhere in the
application while the dialog is active.
Modal and modeless dialogs are derived from CDialog.
Modal dialog can be constructed on the stack and so it is
easy to be cleaned up.
Modeless dialog is more complicated.
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
3
Difference between creating a modal dialog and a modeless
dialog
Modal Modeless
Constructor used Constructor with
resource ID
param
Default
constructor (no
param)
Function used to
create window
DoModal Create with
resource ID
param
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
4
Create() takes the resource ID as parameter and
returns immediately with the dialog window still on
the screen.
Programmer must determine the all actions for the
modeless dialog such as creation,destroy and
processing of user-entered data.
C++ compiler distinguish modal and modeless
dialog by looking at the CEx07aDialog(CView*)
or CEx07aDialog(CWnd*)
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
5
The view needs to know when to destroy the modeless
dialog window.
Usually a user-defined message is sent to the view after the
user click the OK button on the modeless dialog.
Then the view destroy the dialog window but the dialog c++
class may still be there.
User-defined message
#define WM_GOODBYE WM_USER+5
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
6
In CEx07aDialog.h
#define WM_GOODBYE WM_USER+5
In ICEx07aDialog.cpp
void CEx07aDialog::OnCancel() // not really a message handler{
if (m_pView != NULL) {// modeless case
m_pView->PostMessage(WM_GOODBYE,IDCANCEL);}
else {CDialog::OnCancel(); // modal}
}
void CEx07aDialog::OnOK() // not really a message handler{
if (m_pView != NULL) {// modeless case
UpdateData(TRUE);
m_pView->PostMessage(WM_GOODBYE,IDOK);}
else {CDialog::OnOK(); // modal case}
}
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
7
The m_pView points to the view object of this application,
which really contains the user-defined message hanlder.
In CEx07aView.h
afx_msg LRESULT OnGoodbye(WPARAM wParam,LPARAM lParam);
In CEx07aView.cpp
LRESULT CEx07aView::OnGoodbye(WPARAM wParam,LPARAM
lParam){
// message received in response to modeless dialog OK and Cancel
buttons
m_pDlg->DestroyWindow();
return 0L;}
BEGIN_MESSAGE_MAP(CEx07aView,CView)
ON_MESSAGE(WM_GOODBYE,OnGoodbye)
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
8
Let think it over again.
The used-defined message inside dialog class and sends
out the message to the view.
The view handle the message,do necessary jobs,and
finally destroy the modeless dialog window.
The view and modeless dialog have a pointer pointing to
each other,
Inside view,create the modeless dialog window
if (m_pDlg->GetSafeHwnd() == 0) {
m_pDlg->Create(); // displays the dialog window}
Inside view destructor delete m_pDlg;
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
9
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
10
Windows Common Dialogs
They gather user information but do nothing with it.
COMCTL32.DLL contains the windows common control
like treeview;
COMDLG32.DLL contains the various common dialogs.
CColorDialog
CFileDialog
……
CPrintDialog
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
11
We’’ll use CFileDialog to find some,obj files and
delete them.
Win32 use nested dialogs to handle dialog chains.
1,Create the the dialog resource template with style
property set to,Child”
2,Add a,hole”,a group box control with the specific
child window ID stc32(=0x045f),which will hold the
FileOpen dialog.
3,Create C++ class from CDialog,then changed the base
class to CFileDialog by hand in case not to decouple it
from the special template we created.
4,Add message handlers
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
12
Create a modal CFileDialog
int nModal = dlgFile.DoModal();
if ((nModal == IDCANCEL) && (dlgFile.m_bDeleteAll))
{
strMessage.Format(
"Are you sure you want to delete all %s files?",
dlgFile.m_strFilename);
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
13
BOOL CSpecialFileDialog::OnInitDialog()
{
BOOL bRet = CFileDialog::OnInitDialog();
if (bRet == TRUE) {
GetParent()->GetDlgItem(IDOK)->SetWindowText("Delete");
}
return bRet;
}
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
14
When user choose to delete all obj files,the message handler
1,sets the m_bDeleteAll = TRUE
2,GetParent()->GetDlgItem(0x480)->
GetWindowText(m_strFilename);
// 0x480 is the child window ID of the File Name edit control
3,GetParent()->SendMessage(WM_COMMAND,IDCANCEL);
// tell the view the DoModal() returns,CANCEL”
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
15
Give the user another chance before deletion.
If answer Yes
if (AfxMessageBox(strMessage,MB_YESNO) == IDYES) {
HANDLE h;
WIN32_FIND_DATA fData;
while((h =,:FindFirstFile(dlgFile.m_strFilename,&fData))
!= (HANDLE) 0xFFFFFFFF) { // no MFC equivalent
if (::DeleteFile(fData.cFileName) == FALSE) {
strMessage.Format("Unable to delete file %s\n",
fData.cFileName);
AfxMessageBox(strMessage);
break;
}}}
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
16
If just want to delete one file
else if (nModal == IDOK) {
CString strSingleFilename = dlgFile.GetPathName();
strMessage.Format(
"Are you sure you want to delete %s?",
strSingleFilename);
if (AfxMessageBox(strMessage,MB_YESNO) == IDYES)
{ CFile::Remove(strSingleFilename);}
}
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
17
typedef struct _WIN32_FIND_DATA {
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwOID;
TCHAR cFileName[MAX_PATH];
} WIN32_FIND_DATA;
Chapter 7 The Modeless Dialog
and Windows Common Dialogs
VC++ Programming @ UESTC
18
VC++ Programming @ UESTC
19
VC++ Programming @ UESTC
20