chapter 8 dialog boxes and property sheet. 2 two kinds of dialog boxes dialog boxes –modal dialog...
TRANSCRIPT
Chapter 8Dialog Boxes
and Property Sheet
2
Two kinds of dialog boxes
• Dialog boxes– Modal dialog
• When appear, it takes all ownership of input. • It disables the window until the dialog box is dismissed.
– Modeless dialog• It behaves more like a conventional window. • It activates together with other windows.
3
How to create dialog box
• Design a dialog template by using dialog box edit tool in the resource view
• Edit resource file by your self. – Edit Resource script(*.RC) manually
4
A dialog box template
• Using a resource view to design a new dialog box
5
A dialog box template
• Add or delete controls– Using Toolbox menu
6
A dialog box template
• Align templates– Use dialog editor tool bar
– Or use Format menu
7
A dialog box Template
• Tab order– Determine the order of the focus changing when
tab key is pressed– Use [Format]->[Tab Order] menu
8
Two types of dialog boxes
• Dialog boxes– Modal dialog
• When appear, it takes all ownership of input. • It disables the window until the dialog box is dismissed.
– Modaless dialog• It behaves more like a conventional window. • It activates together with other windows.
9
Modal dialog box
• How to create and show it① Design a dialog box template
Resource View
② Create a CDialog derived class using the template
Use [Project] [add class] menu
③ Call CDialog::DoModal() function to show the dialog box
10
Modal Dialog box
• MFC Class Heirarchy
11
Modal dialog box
• Main Virtual functions of CDialog class
– WM_INITDIALOG message handler– When initializes the dialog box– Good place for initializing other controls
– IDOK message handler (when pressing OK button)
– Good place for updating variables before closing the dialog box,
virtual BOOL CDialog::OnInitDialog ( );
virtual void CDialog::OnOK ( );
12
Modal dialog box
• Main Virtual functions of CDialog class
– IDCANCEL message handler (when pressing cancel button)
– Close the dialog box
virtual void CDialog::OnCancel ( );
13
OnOK() and OnCancel() function
• Call EndDialog function to close the dialog box
void CDialog::OnOK(){ UpdateData(TRUE); // update the variables EndDialog(IDOK);}
void CDialog::OnCancel(){ EndDialog(IDCANCEL);}
DDX/DDV (1/8)
• How to connect your variables in the parent window with a dialog box:– Add the same variables and connect them to the
controls• Ex) create two variables and change them using a dialog box
class CMyDialog : public CDialog{ ... CString m_str; int m_color; ...}
15
DDX/DDV (2/8)
• What you have to:
IDC_STRIDC_COLOR
①②
Dialog box
m_str m_color
Dialog variables
m_str m_color
Parent variables
IDC_STRIDC_COLOR
③ ④
Dialog box
m_str m_color
Dialog variables
m_str m_color
Parent variables
When showing dialog box
When pressingOK button
16
DDX/DDV (3/8)
• How to implement it:
BOOL CMyDialog::OnInitDialog() { CDialog::OnInitDialog(); SetDlgItemText(IDC_STR, m_str); SetDlgItemInt(IDC_COLOR, m_color); return TRUE;}
void CMyDialog::OnOK() { GetDlgItemText(IDC_STR, m_str); m_color = GetDlgItemInt(IDC_COLOR); CDialog::OnOK();}
17
DDX/DDV (4/8)
• An automatic way:– DDX(Dialog Data eXchange)
IDC_STRIDC_COLOR
①②
대화상자
m_str m_color
대화상자 객체
m_str m_color
뷰 객체
IDC_STRIDC_COLOR
③ ④
대화상자
m_str m_color
대화상자 객체
m_str m_color
뷰 객체
Automation?
18
DDX/DDV (5/8)
• OnInitDialog(), OnOK() implementation
BOOL CDialog::OnInitDialog(){ ... UpdateData(FALSE); // Give the values to the
controls ...}
void CDialog::OnOK(){ ... UpdateData(TRUE); // Retrieve the values
// from the controls ...}
19
DDX/DDV (6/8)
• What is CWnd::UpdateData() function?
BOOL CWnd::UpdateData(BOOL bSaveAndValidate){ ... CDataExchange dx(this, bSaveAndValidate); DoDataExchange(&dx); ...}
20
DDX/DDV (7/8)
• Implementation of DDX– Connecting a variable with a control
• Use DDX_* MACRO
void CMyDialog::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMyDialog) DDX_Text(pDX, IDC_STR, m_str); DDX_Text(pDX, IDC_COLOR, m_color); //}}AFX_DATA_MAP}
21
DDX/DDV (8/8)
• DDV(Dialog Data Validation)– Automation of the validation of the data
• Check the range of the data• Use DDV_* MACRO
void CMyDialog::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMyDialog) DDX_Text(pDX, IDC_STR, m_str); DDV_MaxChars(pDX, m_str, 10); DDX_Text(pDX, IDC_COLOR, m_color); DDV_MinMaxInt(pDX, m_color, 0, 255); //}}AFX_DATA_MAP}
Coding Practice
• Create a dialog box as shown below and show it when pressing mouse left button
• Type on the edit control and choose a text color value from the radio buttons
23
Two types of dialog boxes
• Dialog boxes– Modal dialog
• When appear, it takes all ownership of input. • It disables the window until the dialog box is dismissed.
– Modaless dialog• It behaves more like a conventional window. • It activates together with other windows.
24
Modaless Dialog Box
• Same thing:– Create a dialog template and add a CDialog-
derived class.
• Different thing:– Do not use CDialog::DoModal() function– Use CDialog::Create() function for initialization
• Ex: CDialog::Create( Resource_ID, parent_wnd);
– Use CDialog::ShowWindow() function for showing– Use CWnd::DestroyWindow() function to close
Modaless dialog box test
• Create a dialog box as shown below and show it as a modaless dialog box.
• Type on the edit control and choose a text color value from the radio buttons
Common Dialog Boxes
27
Common Dialog Box
• MFC Class Hierarchy
28
Common Dialog Dlasses
Class Dialog Type(s)
CFileDialog Open and Save As dialog boxes
CPrintDialog Print and Print Setup dialog boxes
CPageSetupDialog Page Setup dialog boxes
CFindReplaceDialog Find and Replace dialog boxes
CColorDialog Color dialog boxes
CFontDialog Font dialog boxes
29
CColorDialog
CColorDialog dlg;dlg.DoModal();COLORREF color = dlg.GetColor();
CColorDialog dlg(RGB(255, 0, 0), CC_FULLOPEN);dlg.DoModal();COLORREF color = dlg.GetColor();
30
CFileDialog
CFileDialog dlg(TRUE);if(dlg.DoModal() == IDOK) MessageBox(dlg.GetPathName());
CFileDialog dlg(FALSE);if(dlg.DoModal() == IDOK) MessageBox(dlg.GetPathName());
31
CFontDialog
CFontDialog dlg;
if(dlg.DoModal() == IDOK){ CClientDC dc(this);// Get Color COLORREF color = dlg.GetColor(); dc.SetTextColor(color); // Get Font LOGFONT lf; dlg.GetCurrentFont(&lf); CFont font; font.CreateFontIndirect(&lf); dc.SelectObject(&font); // Show Text dc.TextOut(10, 10, CString(" 한글 & English"));}
32
CPageSetupDialog
CPageSetupDialog dlg;dlg.DoModal();
33
CPrintDialog
CPrintDialog dlg(TRUE);dlg.DoModal();
CPrintDialog dlg(FALSE);dlg.DoModal();