zoukankan      html  css  js  c++  java
  • Visual C++ 菜单

    2.1 如何动态添加和删除菜单项

    1. AppendMenu
    2. InsertMenu
    3. DeleteMenu
    /////////////////////////////////////////////////////////////////////////////
    // CMainFrame message handlers
    void CMainFrame::OnAppendMenu() 
    {
        //获得主菜单
        CMenu* pMenu = GetMenu();
        //获得子菜单
        CMenu* pSubMenu = pMenu->GetSubMenu(4);
    
        if (pSubMenu->GetMenuItemCount() == 4)
        {
            //在菜单末端添加菜单项
            pSubMenu->AppendMenu(MF_STRING, ID_TEST_MENU, _T("新菜单项"));
    
            //重画菜单
            DrawMenuBar();
        }
    }
    
    void CMainFrame::OnInsertMenu() 
    {
        //获得主菜单
        CMenu* pMenu = GetMenu();
        //获得子菜单
        CMenu* pSubMenu = pMenu->GetSubMenu(4);
    
        if (pSubMenu->GetMenuItemCount() == 4)
        {
            //在菜单指定位置添加菜单项
            pSubMenu->InsertMenu(4, MF_BYPOSITION, ID_TEST_MENU, _T("新菜单项"));
    
            //重画菜单
            DrawMenuBar();
        }
    }
    
    void CMainFrame::OnRemoveMenu() 
    {
        //获得主菜单
        CMenu* pMenu = GetMenu();
        //获得子菜单
        CMenu* pSubMenu = pMenu->GetSubMenu(4);
    
        if (pSubMenu->GetMenuItemCount() == 5)
        {
             //删除菜单项
             pSubMenu->RemoveMenu(4, MF_BYPOSITION);
    
            //重画菜单
            DrawMenuBar();
        }
    }
    
    void CMainFrame::OnDeleteMenu() 
    {
        //获得主菜单
        CMenu* pMenu = GetMenu();
        //获得子菜单
        CMenu* pSubMenu = pMenu->GetSubMenu(4);
    
        if (pSubMenu->GetMenuItemCount() == 5)
        {
             //删除菜单项
             pSubMenu->DeleteMenu(4, MF_BYPOSITION);
    
            //重画菜单
            DrawMenuBar();
        }
    }
    
    void CMainFrame::OnTestMenu() 
    {
        AfxMessageBox(_T("测试菜单项命令"));    
    }
    

    2.2 如何在系统菜单中添加菜单项

    1. GetSystemMenu
    //获得系统菜单
    CMenu* pMenu = GetSystemMenu(FALSE);
    
    //删除系统菜单项
    pMenu->RemoveMenu(SC_MOVE, MF_STRING);
    
    //添加菜单项
    pMenu->InsertMenu(0, MF_BYPOSITION, ID_TEST_MENU, _T("测试菜单项"));
    
    //重画菜单
    DrawMenuBar();
    

    2.3 如何禁用关闭按钮

    1. EnableMenuItem
    //获得系统菜单
    CMenu* pMenu = GetSystemMenu(FALSE);
    
    //禁用关闭按钮
    pMenu->EnableMenuItem(SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
    

    2.4 如何启用和禁用菜单项

    CCmdUI的Enable方法

    void CMainFrame::OnTestMenu1() 
    {
        m_bEnable1 = FALSE;
        m_bEnable2 = TRUE;
    }
    
    void CMainFrame::OnUpdateTestMenu1(CCmdUI* pCmdUI) 
    {
        //启用或禁用菜单1
        pCmdUI->Enable(m_bEnable1);    
    }
    
    void CMainFrame::OnTestMenu2() 
    {
        m_bEnable1 = TRUE;
        m_bEnable2 = FALSE;    
    }
    
    void CMainFrame::OnUpdateTestMenu2(CCmdUI* pCmdUI) 
    {
        //启用或禁用菜单2
        pCmdUI->Enable(m_bEnable2);
    }
    

    2.5 如何设置菜单项的检查状态

    SetCheck方法

    void CMainFrame::OnUpdateTestMenu(CCmdUI* pCmdUI) 
    {
        //设置菜单项检查状态
        pCmdUI->SetCheck(m_nCheck);    
    }
    

    image

    2.6 如何实现快捷菜单

    即右键菜单ContextMenu

    void CDemoView::OnContextMenu(CWnd* pWnd, CPoint point) 
    {
        CMenu menu;
    
        //加载菜单
        if (!menu.LoadMenu(IDR_MENU))
        {
            return;
        }
    
        //获得子菜单
        CMenu* pPopupMenu = menu.GetSubMenu(0);
    
        //弹出菜单
        pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
            point.x, point.y, pWnd);  
    }
    
    void CDemoView::OnTestMenu1() 
    {
        AfxMessageBox(_T("快捷菜单项1命令"));
    }
    
    void CDemoView::OnTestMenu2() 
    {
        AfxMessageBox(_T("快捷菜单项2命令"));
    }
    
  • 相关阅读:
    更新整理本人所属博客文章的示例代码和工具组件(Java 和 C++)
    【端午呈献】通用高性能 Windows Socket 组件 HPSocket v2.2.1 悄然发布
    REST手记(一):对URI隧道技术以及CRUD操作的理解
    Jquery+JSON消费REST WCF4.0 服务(带源码)
    对REST架构 风格下WCF的一点补充
    REST与SOA两种架构下WCF的异同比较(含源码)
    REST笔记(三):一种标准的超媒体格式:Atom
    REST WCF 使用Stream进行Server与Client交互
    REST笔记(六)通过缓存架构可伸缩性与容错性的Service
    深入理解WCF系统体系(之二:WCF客户端如何构建?(上))
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/2005816.html
Copyright © 2011-2022 走看看