Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog...

33
Chapter 8 Dialog Boxes and Property Sheet

Transcript of Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog...

Page 1: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

Chapter 8Dialog Boxes

and Property Sheet

Page 2: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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.

Page 3: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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

Page 4: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

4

A dialog box template

• Using a resource view to design a new dialog box

Page 5: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

5

A dialog box template

• Add or delete controls– Using Toolbox menu

Page 6: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

6

A dialog box template

• Align templates– Use dialog editor tool bar

– Or use Format menu

Page 7: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

7

A dialog box Template

• Tab order– Determine the order of the focus changing when

tab key is pressed– Use [Format]->[Tab Order] menu

Page 8: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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.

Page 9: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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

Page 10: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

10

Modal Dialog box

• MFC Class Heirarchy

Page 11: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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 ( );

Page 12: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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 ( );

Page 13: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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);}

Page 14: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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; ...}

Page 15: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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

Page 16: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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();}

Page 17: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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?

Page 18: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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

Page 19: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

19

DDX/DDV (6/8)

• What is CWnd::UpdateData() function?

BOOL CWnd::UpdateData(BOOL bSaveAndValidate){ ... CDataExchange dx(this, bSaveAndValidate); DoDataExchange(&dx); ...}

Page 20: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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}

Page 21: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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}

Page 22: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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

Page 23: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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.

Page 24: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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

Page 25: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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

Page 26: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

Common Dialog Boxes

Page 27: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

27

Common Dialog Box

• MFC Class Hierarchy

Page 28: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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

Page 29: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

29

CColorDialog

CColorDialog dlg;dlg.DoModal();COLORREF color = dlg.GetColor();

CColorDialog dlg(RGB(255, 0, 0), CC_FULLOPEN);dlg.DoModal();COLORREF color = dlg.GetColor();

Page 30: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

30

CFileDialog

CFileDialog dlg(TRUE);if(dlg.DoModal() == IDOK) MessageBox(dlg.GetPathName());

CFileDialog dlg(FALSE);if(dlg.DoModal() == IDOK) MessageBox(dlg.GetPathName());

Page 31: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

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"));}

Page 32: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

32

CPageSetupDialog

CPageSetupDialog dlg;dlg.DoModal();

Page 33: Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.

33

CPrintDialog

CPrintDialog dlg(TRUE);dlg.DoModal();

CPrintDialog dlg(FALSE);dlg.DoModal();