Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Chapter 8 Dialog Boxes and Property Sheet

2 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 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 4 A dialog box template Using a resource view to design a new dialog box

5 5 A dialog box template Add or delete controls –Using Toolbox menu

6 6 A dialog box template Align templates –Use dialog editor tool bar –Or use Format menu

7 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 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 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 10 Modal Dialog box MFC Class Heirarchy

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

14 14 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 15 DDX/DDV (2/8) What you have to: IDC_STR IDC_COLOR ①② Dialog box m_str m_color Dialog variables m_str m_color Parent variables IDC_STR IDC_COLOR ③ ④ Dialog box m_str m_color Dialog variables m_str m_color Parent variables When showing dialog box When pressing OK button

16 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 17 DDX/DDV (4/8) An automatic way: –DDX(Dialog Data eXchange) IDC_STR IDC_COLOR ①② 대화상자 m_str m_color 대화상자 객체 m_str m_color 뷰 객체 IDC_STR IDC_COLOR ③ ④ 대화상자 m_str m_color 대화상자 객체 m_str m_color 뷰 객체 Automation?

18 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 19 DDX/DDV (6/8) What is CWnd::UpdateData() function? BOOL CWnd::UpdateData(BOOL bSaveAndValidate) {... CDataExchange dx(this, bSaveAndValidate); DoDataExchange(&dx);... }

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

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

25 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

26 Common Dialog Boxes

27 27 Common Dialog Box MFC Class Hierarchy

28 28 Common Dialog Dlasses ClassDialog Type(s) CFileDialogOpen and Save As dialog boxes CPrintDialogPrint and Print Setup dialog boxes CPageSetupDialogPage Setup dialog boxes CFindReplaceDialogFind and Replace dialog boxes CColorDialogColor dialog boxes CFontDialogFont dialog boxes

29 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 30 CFileDialog CFileDialog dlg(TRUE); if(dlg.DoModal() == IDOK) MessageBox(dlg.GetPathName()); CFileDialog dlg(FALSE); if(dlg.DoModal() == IDOK) MessageBox(dlg.GetPathName());

31 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 32 CPageSetupDialog CPageSetupDialog dlg; dlg.DoModal();

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


Download ppt "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."

Similar presentations


Ads by Google