第9章 菜单、工具栏和状态栏

菜单

  • 系统菜单:单击左上角图标,自动生成、不需要消息映射
  • 程序菜单:普通菜单
  • 快捷菜单:鼠标右键
创建菜单
  • 创建一个基于单文档的应用程序
  • 单击项目工作区的Resource View面板,展开面板里面的内容,单击Menu前面的“+”图标,双击其选项,在代码编辑区弹出向导创建的菜单资源。
  • 右击菜单资源右边的空白项,在弹出的快捷菜单中选择properties命令,弹出Menu Item Properties对话框。在该对话框中可以对菜单资源的样式等进行设计。Separator表示该菜单项是一个分割线,pop up表示该菜单项是一个弹出式菜单,inactive表示该菜单是否被激活,选择则表示不可用,checked表示是否被选中,选中时左侧会有“√”,grayed表示菜单是否被禁用,prompt表示鼠标滑动到菜单项,是否显示提示内容,提示内容在文本框输入。
  • COMMAND菜单消息响应函数:添加类向导,选择一个菜单选项,COMMAND函数,在CMainFrame.cpp中添加消息响应:
void CMainFrame::OnMenu2() 
{
    // TODO: Add your command handler code here
    AfxMessageBox("单击了下拉菜单!");
}
  • 类向导,UPDATE_COMMAND_UI设置是否选择,已经选择会在前面打钩。在CMainFrame类中添加BOOL成员flag,并初始化。
// Implementation
public:
    BOOL flag;//+++++++++++++++++++++
    virtual ~CMainFrame();
CMainFrame::CMainFrame()
{
    // TODO: add member initialization code here
    flag = false;//+++++++++++++++++++++++++

}
void CMainFrame::OnMenu2() 
{
    // TODO: Add your command handler code here
    AfxMessageBox("单击了下拉菜单!");
    flag=!flag;//+++++++++++++++++++++++++++++
}

void CMainFrame::OnUpdateMenu2(CCmdUI* pCmdUI) 
{
    // TODO: Add your command update UI handler code here
    pCmdUI->SetCheck(flag);//++++++++++++Check是钩,Radio是圆点

}

快捷菜单

  • 右键Menu小图标,插入新菜单,IDR_MENU1,设计菜单选项:红、黄、蓝……
  • 在类CTestView中添加数据成员如下:
// Implementation
public:
    CMenu * menupop;//子菜单对象
    CMenu menu;//快捷菜单
    virtual ~CTestView();
  • 初始化
CTestView::CTestView()
{
    // TODO: add construction code here
    menu.LoadMenu(IDR_MENU1);

}

CTestView::~CTestView()
{
    menu.DestroyMenu();
}
  • 选择CTestView类,添加类向导,右击响应函数:
void CTestView::OnRButtonDown(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    menupop = menu.GetSubMenu(0);
    ClientToScreen(&point);
    menupop->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);

    CView::OnRButtonDown(nFlags, point);
}

对话框菜单

  • 新建对话框程序
  • 在资源上右击,插入Menu1
  • 在对话框属性设置中,Menu选择刚才新建的Menu1
  • 利用类向导,为菜单子选项Menu11添加消息映射COMMAND
void CTestDlg::OnMenu11() 
{
    // TODO: Add your command handler code here
    AfxMessageBox("单击了!");
    flag=!flag;

}
  • 同样添加UPDATE_COMMAND_UI消息响应
void CTestDlg::OnUpdateMenu11(CCmdUI* pCmdUI) 
{
    // TODO: Add your command update UI handler code here
    pCmdUI->SetCheck(flag);

}
  • 由于对话框没有初始化函数,故引入如下方法:类向导、选择Class Info标签,在Message filter下拉列表中选择Window,这样就可以截获所有的windows消息。单击Message Maps标签,添加WM_INITMENUPOPUP的消息映射,完全拷贝就行了,不用管。
void CTestDlg::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) 
{
    //CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
    flag=false;//+++++++++++++++++++++++++++++++初始化
    // TODO: Add your message handler code here
    CCmdUI state;
    state.m_pMenu = pPopupMenu;
    ASSERT(state.m_pOther == NULL);
    ASSERT(state.m_pParentMenu == NULL);

    // Determine if menu is popup in top-level menu and set m_pOther to
    // it if so (m_pParentMenu == NULL indicates that it is secondary popup).
    HMENU hParentMenu;
    if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
        state.m_pParentMenu = pPopupMenu;    // Parent == child for tracking popup.
    else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
    {
        CWnd* pParent = this;
        // Child windows don't have menus--need to go to the top!
        if (pParent != NULL &&
            (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
        {
            int nIndexMax = ::GetMenuItemCount(hParentMenu);
            for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
            {
                if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
                {
                    // When popup is found, m_pParentMenu is containing menu.
                    state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
                break;
            }
           }
        }
    }

    state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
    for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
      state.m_nIndex++)
    {
        state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
        if (state.m_nID == 0)
           continue; // Menu separator or invalid cmd - ignore it.

        ASSERT(state.m_pOther == NULL);
        ASSERT(state.m_pMenu != NULL);
        if (state.m_nID == (UINT)-1)
        {
           // Possibly a popup menu, route to first item of that popup.
           state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
           if (state.m_pSubMenu == NULL ||
            (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
            state.m_nID == (UINT)-1)
           {
            continue;       // First item of popup can't be routed to.
           }
           state.DoUpdate(this, TRUE);   // Popups are never auto disabled.
        }
        else
        {
           // Normal menu item.
           // Auto enable/disable if frame window has m_bAutoMenuEnable
           // set and command is _not_a system command.
           state.m_pSubMenu = NULL;
           state.DoUpdate(this, FALSE);
        }

        // Adjust for menu deletions and additions.
        UINT nCount = pPopupMenu->GetMenuItemCount();
        if (nCount < state.m_nIndexMax)
        {
           state.m_nIndex -= (state.m_nIndexMax - nCount);
           while (state.m_nIndex < nCount &&
            pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
           {
            state.m_nIndex++;
           }
        }
        state.m_nIndexMax = nCount;
    }
}

工具栏

  • 新建工具栏IDR_TOOLBAR1,包含按钮选项rect和选项cir
  • 在类CMainFrame类中添加成员CToolBar m_toolbar,添加变量BOOLcir、BOOL rect,并初始化为false。在OnCreate函数中添加代码:
// Implementation
public:
    BOOL rect;//++++++++++++++++
    BOOL cir;//++++++++++++++++
    CToolBar m_toolbar;//++++++++++
    virtual ~CMainFrame();
CMainFrame::CMainFrame()
{
    // TODO: add member initialization code here
    cir = false;//++++++++++初始化
    rect = false; 

}
    //add
    if (!m_toolbar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
        | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
        !m_toolbar.LoadToolBar(IDR_TOOLBAR1))       //创建工具栏
    {
        TRACE0("Failed to create toolbar\n");
        return -1;      // fail to create
    }
  • 为按钮选项添加消息映射:COMMAND、UPDATE_COMMAND_UI
void CMainFrame::Onrect() 
{
    // TODO: Add your command handler code here
    rect = !rect;
    if(cir == true) cir = !cir;
    if(rect == true)
        AfxMessageBox("你单击了矩形工具按钮,此时此按钮处于按下状态");
    else
        AfxMessageBox("你单击了圆形工具按钮,此时此按钮处于按下状态");

}

void CMainFrame::Oncir() 
{
    // TODO: Add your command handler code here
    cir = !cir;
    if(rect == true) rect = !rect;
    if(cir == true)
        AfxMessageBox("你单击了圆形工具按钮,此时此按钮处于按下状态");
    else
        AfxMessageBox("你单击了矩形工具按钮,此时此按钮处于按下状态");

}

void CMainFrame::OnUpdatecir(CCmdUI* pCmdUI) 
{
    // TODO: Add your command update UI handler code here
    pCmdUI->SetCheck(cir);

}

void CMainFrame::OnUpdaterect(CCmdUI* pCmdUI) 
{
    // TODO: Add your command update UI handler code here
    pCmdUI->SetCheck(rect);

}

对话框的工具栏

  • 新建对话框程序
  • 在资源编辑器中插入工具栏,设置四个选项ID_BUTTON1,ID_BUTTON2,ID_BUTTON3,ID_BUTTON4
  • 在类CTestDlg中添加变量CToolBar m_toolbar,来实现加载工具条资源,并进行显示。在OnInitDialog函数中添加代码
// Construction
public:
    CToolBar m_toolbar;
    // TODO: Add extra initialization here
    if (!m_toolbar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_BOTTOM
        |CBRS_TOOLTIPS ) ||
        !m_toolbar.LoadToolBar(IDR_TOOLBAR1))   //创建工具条,显示在底部
    {
        TRACE0("Failed to create toolbar\n");
        return -1;      // fail to create
    }
    RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
    m_toolbar.SetBarStyle(m_toolbar.GetBarStyle()|CBRS_BOTTOM|CBRS_SIZE_DYNAMIC|CBRS_SIZE_DYNAMIC);
  • 利用类向导添加按钮消息映射
void CTestDlg::OnButton2() 
{
    // TODO: Add your command handler code here
    AfxMessageBox("按钮按下了!");
}

状态栏

  • 新建单文档程序
  • 在资源面板中打开String Table下的字符串表,在最后一栏空白处新建:IDS_MOUSE,caption(标题)设置为“鼠标当前位置”
  • 在MainFrm.cpp中添加此项,项目在数组中出现的位置就是状态栏从左至右的位置
static UINT indicators[] =
{
    ID_SEPARATOR,           // status line indicator
    IDS_MOUSE,//+++++++++++
    ID_INDICATOR_CAPS,
    ID_INDICATOR_NUM,
    ID_INDICATOR_SCRL,
};
  • 在CTestView类中添加鼠标移动消息响应函数,testView.cpp包含头文件“MainFrm.h”
void CTestView::OnMouseMove(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    CString str;
    str.Format("当前鼠标位置:%d,%d",point.x,point.y);
    CMainFrame *pmain=(CMainFrame*)AfxGetMainWnd();
    CStatusBar  *pstatusbar=(CStatusBar*)pmain->GetDescendantWindow(AFX_IDW_STATUS_BAR);
    pstatusbar->SetPaneInfo(1,IDS_MOUSE,SBPS_POPOUT,150); //设置样式和宽度
    pstatusbar->SetPaneText(1,str);                    //设置内容,输出鼠标坐标

    CView::OnMouseMove(nFlags, point);
}

快捷键

  • 创建一个单文档程序
  • 在资源面板中,打开Accelerator\IDR_MAINFRAME图标。
  • 在最后一栏空白处添加“帮助|关于”的快捷键,ID_APP_ABOUT、Key为H
  • 勾选”ctrl”
  • 运行,“ctrl+H”即可调用帮助|关于
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值