2016-04-13 95 views
0

我尝试了以下所有可找到的示例,但我错过了一些东西,因此我将所有作品放在此处供其他人查看。仅供参考 - 我正在修改MFC Feature Pack示例滑块。将一个下拉菜单按钮添加到CMFCToolbar,看不到菜单

我看到图层按钮(不是一个字符串或向下的箭头),如果我选择按钮(单击)我看到压下动作,并与按钮的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) 
{ 
} 

回答

0

我认为你正在使用它错误的方法。试试这个方法:

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));