如何动态地在MFC SDI上绘制自定义菜单

时间:2016-08-01 03:25:24

标签: c++ mfc

我正在尝试在MFC单文档界面(SDI)项目上绘制自定义弹出菜单
here开始,我找到The framework calls this member function for the owner of an owner-draw button control, combo-box control, list-box control, or menu when a visual aspect of the control or menu has changed.

所以我添加了句柄OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)并在此函数中添加了一些代码,但我发现当我右键单击视图时,框架不会调用此回调,如下所示:
enter image description here
如何动态制作弹出菜单?

以下是我弹出菜单的方式:

void Cdynamic_menu_sdiView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
    if (IDR_MY_MENU == 0)
        return;
    CMenu dynamicMenu, proxyMenu;
    if (dynamicMenu.GetSafeHmenu())
        dynamicMenu.DestroyMenu();
    // Create a new popup menu.
    if (dynamicMenu.CreatePopupMenu() == FALSE)
        return;

    if (proxyMenu.LoadMenu(IDR_MY_MENU) == FALSE)
        return;

    int nSubMenu = 1;
    CMenu* pProxyMenu = proxyMenu.GetSubMenu(nSubMenu);

    build_dynamic_menu(m_map_menu_element_2, pProxyMenu, dynamicMenu, CString(""));
    //m_menu_on_draw = &dynamicMenu; // link up the dynamic menu to the on draw menu, so that we can print it on the screen

    CPoint ptCurPos; // current cursor position
    GetCursorPos(&ptCurPos);
    dynamicMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON, ptCurPos.x, ptCurPos.y, AfxGetMainWnd(), NULL);
}

在函数build_dynamic_menu()内部,它是深度优先搜索功能,我执行InsertMenu(i, MF_BYPOSITION | MF_POPUP | MF_OWNERDRAW, uSubMenuID, strLabel);,以便我可以动态更改弹出菜单的文本。
由于把这个功能放在这里太长了,我只说出这个想法 我还想动态更改文本颜色和背景颜色,所以我试图找到一种通过代码绘制菜单的方法
我应该从哪里开始? owner drawn menus对我有用吗?

0 个答案:

没有答案