chapter 8 dialog boxes and property sheet

45
Chapter 8 Dialog Boxes and Property Sheet

Upload: audra-chambers

Post on 31-Dec-2015

63 views

Category:

Documents


7 download

DESCRIPTION

Chapter 8 Dialog Boxes and Property Sheet. 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 8 Dialog Boxes  and Property Sheet

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

3

Modal Dialog box

• MFC Class Heirarchy

Page 4: Chapter 8 Dialog Boxes  and Property Sheet

4

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 5: Chapter 8 Dialog Boxes  and Property Sheet

5

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 6: Chapter 8 Dialog Boxes  and Property Sheet

6

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 7: Chapter 8 Dialog Boxes  and Property Sheet

7

DDX/DDV

• 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 8: Chapter 8 Dialog Boxes  and Property Sheet

8

DDX/DDV

• An automatic way:– DDX(Dialog Data eXchange)

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

Automation?

Page 9: Chapter 8 Dialog Boxes  and Property Sheet

9

DDX/DDV

• 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 10: Chapter 8 Dialog Boxes  and Property Sheet

10

DDX/DDV

• 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 11: Chapter 8 Dialog Boxes  and Property Sheet

11

DDX/DDV

• 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 12: Chapter 8 Dialog Boxes  and Property Sheet

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 13: Chapter 8 Dialog Boxes  and Property Sheet

13

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 14: Chapter 8 Dialog Boxes  and Property Sheet

14

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 15: Chapter 8 Dialog Boxes  and Property Sheet

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 16: Chapter 8 Dialog Boxes  and Property Sheet

Common Dialog Boxes

Page 17: Chapter 8 Dialog Boxes  and Property Sheet

17

Common Dialog Box

• MFC Class Hierarchy

Page 18: Chapter 8 Dialog Boxes  and Property Sheet

18

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 19: Chapter 8 Dialog Boxes  and Property Sheet

19

CColorDialog

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

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

Page 20: Chapter 8 Dialog Boxes  and Property Sheet

20

CFileDialog

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

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

Page 21: Chapter 8 Dialog Boxes  and Property Sheet

21

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 22: Chapter 8 Dialog Boxes  and Property Sheet

22

CPageSetupDialog

CPageSetupDialog dlg;dlg.DoModal();

Page 23: Chapter 8 Dialog Boxes  and Property Sheet

23

CPrintDialog

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

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

Page 24: Chapter 8 Dialog Boxes  and Property Sheet

Part IIDocument/View

Architecture

Page 25: Chapter 8 Dialog Boxes  and Property Sheet

Chapter 9Documents, Views, and

the Single Document Interface

Page 26: Chapter 8 Dialog Boxes  and Property Sheet

26

Document/View Fundamentals

• Single document/view architecture

Page 27: Chapter 8 Dialog Boxes  and Property Sheet

27

Document/View Fundamentals

• Application object:– Provides message loop and send messages to the

frame window and the view

• Frame Window object:– Shows menu, toolbar and status bar

• View object:– Displays(renders) data– Translates mouse and keyboard input into

commands to operates the data

• Document object:– Stores data– Manipulates data

Page 28: Chapter 8 Dialog Boxes  and Property Sheet

28

SDI and MDI

• Single Document Interface vs.Multiple Document Interface– Different in the number of documents to open at

once

Page 29: Chapter 8 Dialog Boxes  and Property Sheet

29

Document Template

• Document Template– Identifies the document class, view class and

frame window class. – Stores the resource ID for menu, tool bar and

other resources for the frame window

• MFC Class Hierarchy

SDIMDI

Page 30: Chapter 8 Dialog Boxes  and Property Sheet

30

Document Template

• Initial Setting for an application: InitInstance()

BOOL CExFileApp::InitInstance(){ ... CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CExFileDoc), RUNTIME_CLASS(CMainFrame), RUNTIME_CLASS(CExFileView)); AddDocTemplate(pDocTemplate); ...}

Page 31: Chapter 8 Dialog Boxes  and Property Sheet

31

Single Document Interface Example (1/4)

• New Project:– Single Document + Document/View Support

Page 32: Chapter 8 Dialog Boxes  and Property Sheet

32

Single Document Interface Example (2/4)

• Change OnDraw function of View Class

void CExFileView::OnDraw(CDC* pDC){ CExFileDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pDC->SetMapMode(MM_LOMETRIC); pDC->Ellipse(100, -100, 600, -600);}

Page 33: Chapter 8 Dialog Boxes  and Property Sheet

33

Single Document Interface Example (3/4)

• Result

Page 34: Chapter 8 Dialog Boxes  and Property Sheet

34

Single Document Interface Example (4/4)

• 실행 결과 (cont'd)

Page 35: Chapter 8 Dialog Boxes  and Property Sheet

35

SDI Application

• A Simple Structure

CWinApp

CSingleDocTemplate CDocument CView

CFrameWnd

Page 36: Chapter 8 Dialog Boxes  and Property Sheet

36

SDI Application

• A structure with two views

CWinApp

CSingleDocTemplate CDocument

CView 1

CFrameWnd

CView 2

Page 37: Chapter 8 Dialog Boxes  and Property Sheet

SDI Application

• How to refer each other

DocumentObject

ViewObject

Frame WindowObject

GetFirstViewPosition& GetNextView

GetDocument

GetActiveDocumentGetActiveView

ApplicationObject

AfxGetMainWnd

AfxGetApp

m_pMainWnd

GetParentFrame

Document Template

GetFirstDocTemplatePosition& GetNextDocTemplate

GetDocTemplate

GetFirstDocPosition& GetNextDoc

Page 38: Chapter 8 Dialog Boxes  and Property Sheet

38

SDI Application

• Key Functions– CWinApp* AfxGetApp ( );

• Return the pointer to the application object

– CWnd* AfxGetMainWnd ( );• Return the pointer to the frame window object

– CView* CFrameWnd::GetActiveView ( ); • Return the pointer to the view object

– CDocument* CView::GetDocument ( ); • Return the pointer to the document object

– POSITION CDocument::GetFirstViewPosition ( );CView* CDocument::GetNextView (POSITION& rPosition);• Access to the view object(s)

Document Objectm_viewList

View #1 View #2 View #3

NULL

Page 39: Chapter 8 Dialog Boxes  and Property Sheet

Document/View in detail

• Create a new application as a SDI– Edit the document template string

Page 40: Chapter 8 Dialog Boxes  and Property Sheet

40

Document/View in detail

• Document template string

ExSDI\n\nExSDI\nExSDI 파일 (*.sdi)\n.sdi\nExSDI.Document

① ② ③ ④ ⑤ ⑥\nExSDI Document ⑦

No.

Description

1 Title shown on the title bar

2 A initial name of the document. If skipped, “untitled” will be given

3 Brief name of the document

4 Brief name of the document in file dialog box

5 Default extension name

6 Document ID used in the windows registry

7 Document name used in the windows registry

Page 41: Chapter 8 Dialog Boxes  and Property Sheet

Document Class

• Key CDocument operationsFunction Description

GetFirstViewPosition

Returns a POSITION value that can be passed to GetNextView to begin enumerating the views associated with this document

GetNextView Returns a CView pointer to the next view in the list of views associated with this document

GetPathName Retrieves the document's file name and path—for example, "C:\Documents\Personal\MyFile.doc"; returns an empty string if the document hasn't been named

GetTitle Retrieves the document's title—for example, "MyFile"; returns an empty string if the document hasn't been named

IsModified Returns a nonzero value if the document contains unsaved data or 0 if it doesn't

SetModifiedFlag Sets or clears the document's modified flag, which indicates whether the document contains unsaved data

UpdateAllViews Updates all views associated with the document by calling each view's OnUpdate function

Page 42: Chapter 8 Dialog Boxes  and Property Sheet

42

Document Class

• Key CDocument OverridesFunction Description

OnNewDocument

Called by the framework when a new document is created. Override to apply specific initializations to the document object each time a new document is created.

OnOpenDocument

Called by the framework when a document is loaded from disk. Override to apply specific initializations to the document object each time a document is loaded.

DeleteContents Called by the framework to delete the document's contents. Override to free memory and other resources allocated to the document before it is closed.

Serialize Called by the framework to serialize the document to or from disk. Override to provide document-specific serialization code so that documents can be loaded and saved.

Page 43: Chapter 8 Dialog Boxes  and Property Sheet

43

Document class

• What happens when:– [FILE]->[New]

– [FILE]->[Open...]

– [FILE]->[Save] or [File]->[Save as...]

DeleteContents() OnNewDocument()

DeleteContents() Serialize() OnOpenDocument()

Serialize()

Page 44: Chapter 8 Dialog Boxes  and Property Sheet

44

View Class

• Key CView Overrides

Function Description

OnDraw Called to draw the document's data. Override to paint views of a document.

OnInitialUpdate

Called when a view is first attached to a document. Override to initialize the view object each time a document is created or loaded.

OnUpdate Called when the document's data has changed and the view needs to be updated. Override to implement "smart" update behavior that redraws only the part of the view that needs redrawing rather than the entire view.

Page 45: Chapter 8 Dialog Boxes  and Property Sheet

Coding practice

• Using Document/View architecture, make an application which draws circles and save/load it

Things to change

CView::OnDraw()

CDocument::OnNewDocument()Serialize()

Things to change

CView::OnDraw()

CDocument::OnNewDocument()Serialize()