源码: 菜单项 void CMainFrame::OnAppendMenu() { // TODO: Add your command handler code here //获取主菜单 CMenu* pMenu=GetMenu(); //获取子菜单 CMenu *pSubMenu=pMenu->GetSubMenu(4); if (pSubMenu->GetMenuItemCount()==4) { //在菜单末端添加菜单项 pSubMenu->AppendMenu(MF_STRING,ID_TEST_MENU,_T("新菜单")); //重画菜单 DrawMenuBar(); } } void CMainFrame::OnDeleteMenu() { // TODO: Add your command handler code here CMenu* pMenu=GetMenu(); CMenu* pSubMenu=pMenu->GetSubMenu(4); if (pSubMenu->GetMenuItemCount()==5) { pSubMenu->DeleteMenu(4,MF_BYPOSITION); DrawMenuBar(); } } void CMainFrame::OnRemoveMenu() { // TODO: Add your command handler code here CMenu* pMenu=GetMenu(); CMenu* pSubMenu=pMenu->GetSubMenu(4); if (pSubMenu->GetMenuItemCount()==5) { pSubMenu->RemoveMenu(4,MF_BYPOSITION); DrawMenuBar(); } } void CMainFrame::OnInsertMenu() { // TODO: Add your command handler code here CMenu* pMenu=GetMenu(); CMenu* pSubMenu=pMenu->GetSubMenu(4); if (pSubMenu->GetMenuItemCount()==4) { //在指定的位置添加菜单项 pSubMenu->InsertMenu(2,MF_BYPOSITION,ID_TEST_MENU,_T("Insert菜单项")); DrawMenuBar(); } } void CMainFrame::OnMenuDlg() { dlg.DoModal(); } void CMainFrame::OnMenuTest1() { m_bEnable1=FALSE; m_bEnable2=TRUE; } void CMainFrame::OnMenuTest2() { m_bEnable1=TRUE; m_bEnable2=FALSE; } void CMainFrame::OnUpdateMenuTest1(CCmdUI* pCmdUI) { //启用或者禁用菜单 pCmdUI->Enable(m_bEnable1); } void CMainFrame::OnUpdateMenuTest2(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bEnable2); } 光标: void CMenuView::OnDraw(CDC* pDC) { /* CMenuDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here*/ // /* //获取客户光标坐标 CRect rect; GetClientRect(&rect); //绘制十字光标 pDC->MoveTo(0,m_point.y); pDC->LineTo(rect.Width(),m_point.y); pDC->MoveTo(m_point.x,0); pDC->LineTo(m_point.x,rect.Height()); //输出光标坐标 CString strText=_T(""); strText.Format(_T("%d,%d"),m_point.x,m_point.y); pDC->SetBkMode(TRANSPARENT); pDC->SetTextAlign(TA_RIGHT|TA_BOTTOM); pDC->TextOut(m_point.x,m_point.y,strText); // */ CRect rect; GetClientRect(rect); CRect rect2; rect2.left=rect.left+rect.Width()/4; rect2.right=rect.right-rect.Width()/4; rect2.top=rect.top+rect.Height()/4; rect2.bottom=rect.bottom-rect.Height()/4; //绘制光标的移动范围 pDC->Rectangle(rect2); } / // CMenuView printing BOOL CMenuView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMenuView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMenuView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } / // CMenuView diagnostics #ifdef _DEBUG void CMenuView::AssertValid() const { CView::AssertValid(); } void CMenuView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMenuDoc* CMenuView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMenuDoc))); return (CMenuDoc*)m_pDocument; } #endif //_DEBUG / // CMenuView message handlers //void CMenuView::OnMove(int x, int y) //{ //CView::OnMove(x, y); // TODO: Add your message handler code here //} void CMenuView::OnMouseMove(UINT nFlags, CPoint point) { //保存光标坐标 m_point=point; //刷新客户区 Invalidate(); CView::OnMouseMove(nFlags, point); } void CMenuView::OnLButtonDown(UINT nFlags, CPoint point) { //光标的自由移动 ClipCursor(NULL); //加载光标 m_hCursor=AfxGetApp()->LoadCursor(IDC_CURSOR); //设置光标 CRect rect; GetClientRect(rect); CRect rect2; rect2.left=rect.left+rect.Width()/4; rect2.right=rect.right-rect.Width()/4; rect2.top=rect.top+rect.Height()/4; rect2.bottom=rect.bottom-rect.Height()/4; //映射屏幕坐标 ClientToScreen(rect2); //限制光标的移动 ClipCursor(&rect2); CView::OnLButtonDown(nFlags, point); } void CMenuView::OnLButtonUp(UINT nFlags, CPoint point) { //光标的自由移动 ClipCursor(NULL); //加载光标 m_hCursor=AfxGetApp()->LoadCursor(IDC_CURSOR); //设置光标 ::SetCursor(m_hCursor); CView::OnLButtonUp(nFlags, point); } void CMenuView::OnInitialUpdate() { CView::OnInitialUpdate(); //加载光标 m_hCursor=AfxGetApp()->LoadCursor(IDC_CURSOR); } BOOL CMenuView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { //设置光标 ::SetCursor(m_hCursor); return TRUE; //return CView::OnSetCursor(pWnd, nHitTest, message); }