首页 > 代码库 > MFC: 孙鑫教程6笔记

MFC: 孙鑫教程6笔记

这节课的主要内容是讲菜单,分为静态菜单和动态菜单

动态:

CMainFrame::OnCreate

 1 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 2 { 3     if (CFrameWnd::OnCreate(lpCreateStruct) == -1) 4         return -1; 5      6     if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP 7         | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || 8         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) 9     {10         TRACE0("未能创建工具栏\n");11         return -1;      // 未能创建12     }13 14     if (!m_wndStatusBar.Create(this) ||15         !m_wndStatusBar.SetIndicators(indicators,16           sizeof(indicators)/sizeof(UINT)))17     {18         TRACE0("未能创建状态栏\n");19         return -1;      // 未能创建20     }21 22     // TODO: 如果不需要可停靠工具栏,则删除这三行23     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);24     EnableDocking(CBRS_ALIGN_ANY);25     DockControlBar(&m_wndToolBar);26 27     /*CMenu menu;28     menu.CreatePopupMenu();29 //    GetMenu()->AppendMenuA(MF_POPUP, (UINT)menu.m_hMenu, "Panda");30     GetMenu()->InsertMenuA(2, MF_BYPOSITION | MF_POPUP, (UINT)menu.m_hMenu, "Panda");31     menu.AppendMenuA(MF_STRING, IDM_HELLO, "Hello");32     menu.AppendMenuA(MF_STRING, 112, "Panda");33     menu.AppendMenuA(MF_STRING, 113, "Mybole");34 /*    GetMenu()->GetSubMenu(0)->AppendMenuA(MF_STRING, 114, "welcome");35     GetMenu()->GetSubMenu(0)->InsertMenuA(ID_FILE_OPEN, MF_BYCOMMAND | MF_STRING, 115, "熊猫");36     GetMenu()->DeleteMenu(1, MF_BYPOSITION);37     GetMenu()->GetSubMenu(0)->DeleteMenu(2, MF_BYPOSITION);*/38 39     /*menu.Detach();*/40 41     return 0;42 }
View Code

CMainFrame::OnCommand

 1 BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam) 2 { 3     // TODO: 在此添加专用代码和/或调用基类 4     int MenuCmdId = LOWORD(wParam); 5     //CMenu2View view; 6     CMenu2View *pView = (CMenu2View*)GetActiveView(); 7     if (MenuCmdId >= IDM_PHONE1 && MenuCmdId < IDM_PHONE1+pView->m_strArray.GetSize()) 8     { 9         CClientDC dc(pView);10         dc.TextOutA(0, 0, pView->m_strArray.GetAt(MenuCmdId-IDM_PHONE1));11         //MessageBox("Test");12         return TRUE;13     }14     return CFrameWnd::OnCommand(wParam, lParam);15 }
View Code

CMenu2View::OnChar

 1 void CMenu2View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 2 { 3     // TODO: 在此添加消息处理程序代码和/或调用默认值 4     CClientDC dc(this); 5  6     if (0x0d == nChar) { 7         if (0 == ++m_nIndex) { 8             m_menu.CreatePopupMenu(); 9             GetParent()->GetMenu()->AppendMenuA(MF_POPUP, (UINT)m_menu.m_hMenu, "PhoneBook");10             GetParent()->DrawMenuBar();11         }12         m_menu.AppendMenuA(MF_STRING, IDM_PHONE1+m_nIndex, m_strLine.Left(m_strLine.Find( )));13         m_strArray.Add(m_strLine);14         m_strLine.Empty();15         Invalidate();16     }17     else {18         m_strLine += (wchar_t)nChar;19         dc.TextOutA(0, 0, m_strLine);20     }21 22 23     CView::OnChar(nChar, nRepCnt, nFlags);24 }
View Code

CMenu2View::OnPhone1

 1 void CMenu2View::OnPhone2() 2 { 3     // TODO: 在此添加命令处理程序代码 4     CClientDC dc(this); 5     dc.TextOut(0, 0, m_strArray.GetAt(1)); 6 } 7  8 void CMenu2View::OnPhone1() 9 {10     // TODO: 在此添加命令处理程序代码11     CClientDC dc(this);12     dc.TextOut(0, 0, m_strArray.GetAt(0));13 }14 15 void CMenu2View::OnPhone3()16 {17     // TODO: 在此添加命令处理程序代码18     CClientDC dc(this);19     dc.TextOut(0, 0, m_strArray.GetAt(2));20 }21 22 void CMenu2View::OnPhone4()23 {24     // TODO: 在此添加命令处理程序代码25     CClientDC dc(this);26     dc.TextOut(0, 0, m_strArray.GetAt(3));27 }
View Code

静态:

CMenuView::OnRButtonDown

 1 void CMenuView::OnRButtonDown(UINT nFlags, CPoint point) 2 { 3     // TODO: 在此添加消息处理程序代码和/或调用默认值 4     CMenu menu; 5     menu.LoadMenuA(IDR_MENU1); 6     CMenu *pPopup = menu.GetSubMenu(0); 7     ClientToScreen(&point); 8  9     //pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);10     pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, GetParent());11 12     CView::OnRButtonDown(nFlags, point);13 }
View Code

CMainFrame::OnCreate

 1 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 2 { 3     if (CFrameWnd::OnCreate(lpCreateStruct) == -1) 4         return -1; 5      6     if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP 7         | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || 8         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) 9     {10         TRACE0("未能创建工具栏\n");11         return -1;      // 未能创建12     }13 14     if (!m_wndStatusBar.Create(this) ||15         !m_wndStatusBar.SetIndicators(indicators,16           sizeof(indicators)/sizeof(UINT)))17     {18         TRACE0("未能创建状态栏\n");19         return -1;      // 未能创建20     }21 22     // TODO: 如果不需要可停靠工具栏,则删除这三行23     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);24     EnableDocking(CBRS_ALIGN_ANY);25     DockControlBar(&m_wndToolBar);26 27     //GetMenu()->GetSubMenu(0)->CheckMenuItem(0, MF_BYPOSITION | MF_CHECKED);28     //GetMenu()->GetSubMenu(0)->CheckMenuItem(ID_FILE_NEW, MF_BYCOMMAND | MF_CHECKED);29     //GetMenu()->GetSubMenu(0)->SetDefaultItem(1, TRUE);30     //GetMenu()->GetSubMenu(0)->SetDefaultItem(ID_FILE_OPEN);31     //GetMenu()->GetSubMenu(0)->SetDefaultItem(5, TRUE);32 33     /*CString str;34     str.Format("x = %d, y = %d", GetSystemMetrics(SM_CXMENUCHECK), GetSystemMetrics(SM_CXMENUCHECK));35     MessageBox(str);36     m_bitmap.LoadBitmapA(IDB_BITMAP1);37     GetMenu()->GetSubMenu(0)->SetMenuItemBitmaps(0, MF_BYPOSITION, &m_bitmap, &m_bitmap);*/38 39     //GetMenu()->GetSubMenu(0)->EnableMenuItem(1, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);40 41     SetMenu(NULL);42 43     CMenu menu;44     menu.LoadMenuA(IDR_MAINFRAME);45     SetMenu(&menu);46     menu.Detach();47 48     return 0;49 }
View Code

CMainFrame::OnUpdateEditCut

1 void CMainFrame::OnUpdateEditCut(CCmdUI *pCmdUI)2 {3     // TODO: 在此添加命令更新用户界面处理程序代码4     //if (2 == pCmdUI->m_nIndex)5     if (ID_EDIT_CUT == pCmdUI->m_nID)6         pCmdUI->Enable();7     //pCmdUI->Enable();8 }
View Code

CMainFrame::OnUpdateFileNew

1 void CMainFrame::OnUpdateFileNew(CCmdUI *pCmdUI)2 {3     // TODO: 在此添加命令更新用户界面处理程序代码4 //    if (ID_FILE_NEW == pCmdUI->m_nID)5 //        pCmdUI->Enable(FALSE);6     if (0 == pCmdUI->m_nIndex)7         pCmdUI->Enable(FALSE);8 }
View Code

CMainFrame::OnShow

1 void CMainFrame::OnShow()2 {3     // TODO: 在此添加命令处理程序代码4     MessageBox("MainFrame show!");5 }
View Code

 

MFC: 孙鑫教程6笔记