1 / 33

Chapter 5 The MFC Collection Classes

Chapter 5 The MFC Collection Classes. Coding Practice. Draw curves with mouse left dragging. Each curve should be separated. Back to MENU – Chapter 4. Chapter 4-3: Menu Magic. 2 Ways for creating a menu: Use the Resource view Use CMenu class and create it manually.

tass
Download Presentation

Chapter 5 The MFC Collection Classes

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 5The MFC Collection Classes

  2. Coding Practice • Draw curves with mouse left dragging. • Each curve should be separated

  3. Back to MENU – Chapter 4

  4. Chapter 4-3: Menu Magic • 2 Ways for creating a menu: • Use the Resourceview • Use CMenu class and create it manually

  5. Create a menu using CMenu class • Design a popup menu • Add(attach) the popup menu to the main menu 2. Add it to the main menu 1. Design a popup menu

  6. Procedure • Design a popup menu • Create a newPopupMenu: CreatePopupMenu() • Adding menu items to the menu: AppendMenu() • Attach it to the main menu • Get the main menu: GetMenu() • Attach a menu to the existing menu: AppendMenu()

  7. Coding practice • In the CMainFrame::OnCreate function int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { // … ... CMenu Popup; Popup.CreatePopupMenu(); Popup.AppendMenu(MF_STRING, 201, “Red(&R)"); Popup.AppendMenu(MF_STRING, 202, “Green(&G)"); Popup.AppendMenu(MF_STRING, 203, “Blue(&B)"); CMenu * pMenuMain = GetMenu(); pMenuMain->AppendMenu(MF_POPUP, (UINT_PTR) Popup.Detatch(), “Color(&C)”); }

  8. Summary • CMenu class: store the menu properties • AppendMenu() member function • Flag: • MF_STRING (whenadding a normal menu item) • MF_POPUP (when attaching a popup menu) • MF_SEPARATOR • ID: • In case of MF_STRING : Command ID • In case of MF_POPUP : Pointer to the popup menu (use CMenu::Detatch() function) bool CMenu::AppendMenu( Flag, ID, Caption )

  9. Context menu Context menu

  10. Context menu message handler • 1. When click the mouse right button or key • 2. Windows sends WM_CONTEXTMENU message • 3. Add the message handler

  11. Prototype of the handler • WM_CONTEXTMENU message handler • pWnd – window where the mouse cursor is located • pos – the position of the mouse cursor afx_msg void OnContextMenu (CWnd* pWnd, CPoint pos) ;

  12. Coding Practice • Add the WM_CONTEXTMENU handler AfxMessageBox(_T(“Context Menu”));

  13. Show the menu in the handler • Use CMenu::TrackPopupMenu() function • nFlags • TPM_LEFTALIGN, TPM_CENTERALIGN, TPM_RIGHTALIGN • TPM_LEFTBUTTON, TPM_RIGHTBUTTON BOOL TrackPopupMenu (UINT nFlags, int x, int y, CWnd* pWnd, LPCRECT lpRect = 0) ;

  14. Show the menu in the handler • Use CMenu::TrackPopupMenu() function • x, y • Position where the menu will be located(스크린 좌표) • pWnd • Window which will get the WM_COMMAND message • Usually use AfxGetMainWnd() function to get the CMainFrame • lpRect • The rectangle region of the menu. Give 0 usually. BOOL TrackPopupMenu (UINT nFlags, int x, int y, CWnd* pWnd, LPCRECT lpRect = 0) ;

  15. An example to show the context menu • Create a menu and show it: void CChildView::OnContextMenu(CWnd* pWnd, CPoint point) { CMenu menuPopup; menuPopup.CreatePopupMenu(); menuPopup.AppendMenu(MF_STRING, 201, "Red (&R)"); menuPopup.AppendMenu(MF_STRING, 202, "Green (&G)"); menuPopup.AppendMenu(MF_STRING, 203, "Blue (&B)"); menuPopup.TrackPopupMenu( TPM_LEFTALIGN|TPM_LEFTBUTTON, point.x, point.y, AfxGetMainWnd()); }

  16. More example • Using existing menu as a context menu void CChildView::OnContextMenu(CWnd* pWnd, CPoint point) { CMenu menu; menu.LoadMenu(IDR_MAINFRAME); CMenu* pMenu = menu.GetSubMenu(4); pMenu->TrackPopupMenu( TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd()); }

  17. Chapter 6:FILE I/O and Serialize CFile class

  18. Introduction (1/2) • How to read/write a file • Usual way of File I/O • Use CFile class • Use member functions: CFile::Read() and CFile::Write() • Universal use. • Low levelfile I/O • Serialize • Use CArchive class • Use operators: << and >> • Easy to use • Limited functionality

  19. Introduction(2/2) • MFC Class Hierarchy • Basic File I/O • Derived Classes for specific purposes

  20. CFile class • Basic functionality • Open or create a file(Open). • Read data at the file pointer(Read). • Write data at the file pointer(Write). • Change the file pointer(Seek). • Close the file(Close).

  21. CFile: Two ways to Open a file • Use CFile::Open member function • Use a constructor CFile file; file.Open( filename, mode, error ); CFile file ( filename, mode );

  22. CFile class(1/6) • Open and Create: Use CFile::Open(..) CFile file; file.Open( filename, mode, error ); CFile file; if( file.Open("mytest.txt", CFile::modeRead) == false) AfxMessageBox(_T(“Error”));

  23. CFile class(1/6) • Open and Create: Use CFile::Open(..) • Get errors and related information CFile file; file.Open( filename, mode, error ); CFile file; CFileException e; if(!file.Open("mytest.txt", CFile::modeReadWrite, &e)) e.ReportError();

  24. CFile class(1/6) • Open and Create : Use the constructor CFile file ( filename, mode ); try { CFile file("mytest.txt", CFile::modeReadWrite); } catch (CFileException* e) { e->ReportError(); e->Delete(); }

  25. CFile class(2/6) • Access modes of CFile flags meaning CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. CFile::modeRead Requests read access only CFile::modeReadWrite Requests read and write access CFile::modeWrite Requests write access only CFile::shareDenyNone Opens the file nonexclusively CFile::shareDenyRead Denies read access to other parties. CFile::shareDenyWrite Denies write access to other parties. CFile::shareDenyExclusive Denies both read and write access to other parties (default).

  26. CFile class(3/6) • Close a file: Destructor closes it automatically void CExFileView::OnLButtonDblClk(UINT nFlags, CPoint point) { CFile file; CFileException e; if(!file.Open("mytest.txt", CFile::modeReadWrite|CFile::modeCreate, &e)) { e.ReportError(); return; } // skip... } // -> CFile::~CFile() is called and close the file automatically.

  27. CFile class(4/6) • Close a file: CFile::Close member function • When opening a multiple files void CExFileView::OnLButtonDblClk(UINT nFlags, CPoint point) { CFile file; CFileException e; if(!file.Open("mytest.txt“, CFile::modeReadWrite|CFile::modeCreate| CFile::modeNoTruncate, &e)) { e.ReportError(); return; } file.Close(); }

  28. CFile class(5/6) • Read and Write • Change the file pointer UINT CFile::Read (void* lpBuf, UINT nCount) ; void CFile::Write (const void* lpBuf, UINT nCount) ; ULONGLONG CFile::Seek (LONGLONG lOff, UINT nFrom) ;

  29. CFile class: Write Data void CFile::Write (const void* lpBuf, UINT nCount) ; CFile file(_T(“test.txt“), CFile::modeCreate|CFile::modeWrite); int a = 30; int b = 20; file.Write(&a, sizeof(a)); file.Write(&b, sizeof(b));

  30. CFile class: Read Data UINT CFile::Read (void* lpBuf, UINT nCount) ; CFile file; CFileException e; if(!file.Open(_T("test.txt“), CFile::modeRead, &e)) { e.ReportError(); return; }; int a,b; file.Read(&a, sizeof(a)); file.Read(&b, sizeof(b));

  31. CFile class: Read Data UINT CFile::Read (void* lpBuf, UINT nCount) ; CFile file; CFileException e; if(!file.Open(_T("test.txt“), CFile::modeRead, &e)){ e.ReportError(); return; }; int a,b; file.Read(&a, sizeof(a)); file.Read(&b, sizeof(b)); CString str; str.Format(_T("a=%d b=%d"), a, b); AfxMessageBox(str);

  32. Coding Practice • Create variables “a” and “b” • When clicking mouse left button, create “test.txt” file, and store the values of “a” and “b” variables • When clicking mouse right button, read “test.txt” file, and read the values of “a” and “b” variables • Show the data by using AfxMessageBox function

  33. CFile class(6/6) • Miscellaneous functions • CFile::GetLength(), CFile::SetLength() • Get or change the file size. • CFile::GetPosition() • Get the file position. • CFile::LockRange(), CFile::UnlockRange() • Lock or Unlock the file. If lock a file, other process can not access the file. • CFile::GetFilePath(), CFile::GetFileName() • Get full path or file name as a CString

More Related