Download presentation
Presentation is loading. Please wait.
1
Chapter 5 The MFC Collection Classes
2
MFC Container Class CArray CList CVector CMap
3
Linked List Good: Easy to add or delete data
Bad: Only sequential access to the data is possible
4
CList Linked List Template Class by MFC Create a variable:
CList < datatype > a; Add Data: CList::AddTail(..) CList::AddHead(..) Delete Data: CList::RemoveTail(); CList::RemoveHeat(); CList::RemoveAt(..) Example: CList < int > a; Add data: CList::AddTail(3) CList::AddHead(4) Delete Data: CList::RemoveTail(); CList::RemoveHeat(); CList::RemoveAt(..) iterator: POSITION
5
Iterator Access to an item in the Container (== pointer)
iterator datatype in MFC : POSITION CList <int> a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); // Return the first data while(pos != NULL) // position { int value = a.GetNext(pos); // Return data at pos // pos indicate the next }
6
Insertion // Add at front CList::AddHead( value ) // Add at back
CList::AddTail( value ) // Add at the given position CList::InsertAfter (POSITION, value)
7
Retrieval/Modification
// Get value value = CList::GetAt( POSITION ) // Get reference value & = CList::GetAt( POSITION ) CList <int> a; a.AddHead(10); a.AddHead(20); a.AddHead(30); POSITION pos; pos = a.GetHeadPosition(); int b = a.GetAt(pos); // value int &c = a.GetAt(pos); // reference
8
Removal // Delete the frist CList::RemoveHead( ) // Delete the end
CList::RemoveTail( ) // Delete at the position CList::RemoveAt(POSITION) // Delete all CList::RemoveAll( )
9
Coding Practice Draw points with mouse left dragging.
Delete points with mouse right dragging Draw Points with mouse left dragging Set a rectangle By mouse right dragging Delete points inside the rectangle
10
Coding Practice 2 Draw curves with mouse left dragging.
Each curve should be separated
11
Back to MENU – Chapter 4
12
Chapter 4-3: Menu Magic 2 Ways for creating a menu:
Use the Resource view Use CMenu class and create it manually
13
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
14
Procedure Design a popup menu Attach it to the main menu
Create a new PopupMenu: 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()
15
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)”); }
16
Summary CMenu class: store the menu properties
AppendMenu() member function Flag: MF_STRING (when adding 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 )
17
Context menu Context menu
18
Context menu message handler
1. When click the mouse right button or key 2. Windows sends WM_CONTEXTMENU message 3. Add the message handler
19
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) ;
20
Coding Practice Add the WM_CONTEXTMENU handler
AfxMessageBox(_T(“Context Menu”));
21
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) ;
22
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) ;
23
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()); }
24
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()); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.