330 likes | 549 Views
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.
E N D
Coding Practice • Draw curves with mouse left dragging. • Each curve should be separated
Chapter 4-3: Menu Magic • 2 Ways for creating a menu: • Use the Resourceview • Use CMenu class and create it manually
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
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()
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)”); }
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 )
Context menu Context menu
Context menu message handler • 1. When click the mouse right button or key • 2. Windows sends WM_CONTEXTMENU message • 3. Add the message handler
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) ;
Coding Practice • Add the WM_CONTEXTMENU handler AfxMessageBox(_T(“Context Menu”));
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) ;
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) ;
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()); }
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()); }
Chapter 6:FILE I/O and Serialize CFile class
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
Introduction(2/2) • MFC Class Hierarchy • Basic File I/O • Derived Classes for specific purposes
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).
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 );
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”));
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();
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(); }
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).
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.
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(); }
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) ;
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));
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));
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);
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
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