1 / 45

Chapter 8 Dialog Boxes and Property Sheet

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.

Télécharger la présentation

Chapter 8 Dialog Boxes and Property Sheet

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. Modal Dialog box • MFC Class Heirarchy

  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

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

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

  7. Dialog box Dialog variables Parent variables IDC_STR IDC_COLOR m_str m_color m_str m_color When showing dialog box ② ① Dialog box Dialog variables Parent variables IDC_STR IDC_COLOR m_str m_color m_str m_color When pressingOK button ③ ④ DDX/DDV • What you have to:

  8. Automation? Dialog box Dialog variables Parent variables IDC_STR IDC_COLOR m_str m_color m_str m_color ② ① Dialog box Dialog variables Parent variables IDC_STR IDC_COLOR m_str m_color m_str m_color ③ ④ DDX/DDV • An automatic way: • DDX(Dialog Data eXchange)

  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 }

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

  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 }

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

  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.

  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

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

  16. Common Dialog Boxes

  17. Common Dialog Box • MFC Class Hierarchy

  18. Common Dialog Dlasses

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

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

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

  22. CPageSetupDialog CPageSetupDialog dlg; dlg.DoModal();

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

  24. Part IIDocument/View Architecture

  25. Chapter 9Documents, Views, and the Single Document Interface

  26. Document/View Fundamentals • Single document/view architecture

  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

  28. SDI and MDI • Single Document Interface vs.Multiple Document Interface • Different in the number of documents to open at once

  29. SDI MDI 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

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

  31. Single Document Interface Example(1/4) • New Project: • Single Document+ Document/View Support

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

  33. Single Document Interface Example (3/4) • Result

  34. Single Document Interface Example (4/4) • 실행 결과 (cont'd)

  35. SDI Application • A Simple Structure CFrameWnd CWinApp CSingleDocTemplate CDocument CView

  36. CFrameWnd CWinApp CView 1 CSingleDocTemplate CDocument CView 2 SDI Application • A structure with two views

  37. GetFirstViewPosition & GetNextView Document Object View Object GetDocument GetActiveDocument GetActiveView GetParentFrame GetDocTemplate Frame Window Object AfxGetMainWnd GetFirstDocPosition & GetNextDoc m_pMainWnd Document Template Application Object AfxGetApp GetFirstDocTemplatePosition & GetNextDocTemplate SDI Application • How to refer each other

  38. View #1 View #2 View #3 Document Object m_viewList NULL 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)

  39. Document/View in detail • Create a new application as a SDI • Edit the document template string

  40. Document/View in detail • Document template string ExSDI\n\nExSDI\nExSDI 파일 (*.sdi)\n.sdi\nExSDI.Document ① ② ③ ④ ⑤ ⑥ \nExSDI Document ⑦

  41. Document Class • Key CDocument operations

  42. Document Class • Key CDocument Overrides

  43. Document class • What happens when: • [FILE]->[New] • [FILE]->[Open...] • [FILE]->[Save] or[File]->[Save as...] DeleteContents()  OnNewDocument() DeleteContents()  Serialize()  OnOpenDocument() Serialize()

  44. View Class • Key CView Overrides

  45. Coding practice • Using Document/View architecture, make an application which draws circles and save/load it Things to change CView:: OnDraw() CDocument:: OnNewDocument() Serialize()

More Related