【问题标题】:Adding a drop-down menu button to a CMFCToolbar, not seeing menu向 CMFCToolbar 添加下拉菜单按钮,看不到菜单
【发布时间】:2023-03-10 02:02:01
【问题描述】:

我尝试按照我能找到的所有示例进行操作,但我遗漏了一些内容,因此我将所有部分都放在这里以供其他人查看。仅供参考 - 我正在修改 MFC 功能包示例滑块。

如果我选择按钮(单击),我会看到图层按钮(不是字符串或向下箭头)我看到按下动作并使用按钮 ID 进入 OnLayers() 函数。我几乎看起来 ReplaceButton() 什么也没做。

有什么想法吗?

谢谢

我为工具栏添加了 ID_LAYERS_1

IDR_MAINFRAME TOOLBAR 16, 15
BEGIN
    BUTTON      ID_FILE_NEW
    BUTTON      ID_FILE_OPEN
    BUTTON      ID_FILE_SAVE
    SEPARATOR
    BUTTON      ID_SLIDER
    SEPARATOR
    BUTTON      ID_EDIT_CUT
    BUTTON      ID_EDIT_COPY
    BUTTON      ID_EDIT_PASTE
    SEPARATOR
    BUTTON      ID_FILE_PRINT
    SEPARATOR
    BUTTON      ID_APP_ABOUT
    SEPARATOR
    BUTTON      ID_LAYERS_1
END

我的菜单是

IDR_LAYERS MENU
BEGIN
    POPUP "Layers"
    BEGIN
        MENUITEM "0",                           ID_LAYERS_1
        MENUITEM "1",                           ID_LAYERS_2
        MENUITEM "2",                           ID_LAYERS_3
    END
END

和代码

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)
   ON_WM_CREATE()
   ON_WM_CLOSE()
   ON_COMMAND(ID_SLIDER, OnSlider)
   ON_COMMAND(ID_VIEW_CUSTOMIZE, OnViewCustomize)
   ON_REGISTERED_MESSAGE(AFX_WM_RESETTOOLBAR, OnToolbarReset)
   ON_REGISTERED_MESSAGE(AFX_WM_TOOLBARMENU, OnToolbarContextMenu)
   ON_UPDATE_COMMAND_UI_RANGE(ID_LAYERS_1, ID_LAYERS_3, OnUpdateLayers)
   ON_COMMAND_RANGE(ID_LAYERS_1, ID_LAYERS_3, OnLayers)
END_MESSAGE_MAP()

CMFCToolBarMenuButton* CreateLayerButton()
{
   CMenu menu;
   VERIFY(menu.LoadMenu(IDR_LAYERS));

   CMFCToolBarMenuButton* pLayerButton = NULL;
   CMenu* pPopup = menu.GetSubMenu(0);
   ASSERT(pPopup != NULL);
   if (pPopup != NULL)
   {
      HMENU hMenu = pPopup->GetSafeHmenu();
      pLayerButton = new CMFCToolBarMenuButton(ID_LAYERS_1, hMenu, -1, NULL, FALSE);
   }

   return pLayerButton;
}

afx_msg LRESULT CMainFrame::OnToolbarReset(WPARAM wp, LPARAM)
{
   UINT uiToolBarId = (UINT)wp;
   if (uiToolBarId == IDR_MAINFRAME)
   {
      CSliderButton btnSlider(ID_SLIDER);
      btnSlider.SetRange(0, 100);
      m_wndToolBar.ReplaceButton(ID_SLIDER, btnSlider);

      // layer button/menu
      CMFCToolBarMenuButton* pLayerButton = CreateLayerButton();
      m_wndToolBar.ReplaceButton(ID_LAYERS_1, *pLayerButton);
      delete pLayerButton;
   }

   return 0;
}

void CMainFrame::OnUpdateLayers(CCmdUI* pCmdUI)
{
   //pCmdUI->SetCheck(true);
}

void CMainFrame::OnLayers(UINT id)
{
}

【问题讨论】:

    标签: drop-down-menu cmfctoolbar


    【解决方案1】:

    我认为你用错了。试试这个方法:

    CMenu menu;
    VERIFY(menu.LoadMenu(IDR_LAYERS));
    
    CString str;
    
    str.LoadString (IDS_TEXT_OF_YOUR BUTTON);
    m_wndToolBar.ReplaceButton (ID_LAYERS1, 
        CMFCToolBarMenuButton ( (UINT)-1, menu, 
                    GetCmdMgr ()->GetCmdImage (ID_LAYERS1), str,TRUE));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      相关资源
      最近更新 更多