开发者

how to add message map to dynamic menu item in MFC

开发者 https://www.devze.com 2022-12-15 02:14 出处:网络
I writing a MFC which has a listview control. When the user right clicks any item , I am generating a dynamic menu item with that text that is selected in listview. Everything is displaying properly,

I writing a MFC which has a listview control. When the user right clicks any item , I am generating a dynamic menu item with that text that is selected in listview. Everything is displaying properly, but I do not know how to add a message map to that dynamic menu item.

Any help?

void CMyListDlg::OnRclickList(NMHDR* pNMHDR, LRESULT* pResult) 
    {
        // TODO: Add your control notification handler code here


        int nIndex = m_List.GetSelectionMark();
        CString pString = m_List.GetItemText(nIndex,1);
        CMenu menu, * pSubMenu;
        int pos=0;
        menu.LoadMenu(IDR_MENU1);
        pSubMenu = menu.GetSubMenu (0);
        pSubMenu-开发者_Python百科>DeleteMenu(0,MF_BYPOSITION);
        pSubMenu->InsertMenu(pos,MF_BYPOSITION,NULL,pString);
            CPoint oPoint;
        GetCursorPos (& oPoint);
        pSubMenu-> TrackPopupMenu (TPM_LEFTALIGN, oPoint.x, oPoint.y, this);






        *pResult = 0;
    }


At the moment you are inserting the menu item with ID = 0 (NULL). That way you can't figure out which command was pressed. You have to assign an ID to the item, the simplest one is to

#define WM_MYMESSAGE WM_USER + 1

then you insert it like this:

pSubMenu->InsertMenu(pos,MF_BYPOSITION,WM_MYMESSAGE,pString);

If you override OnCommand for your window, you get your ID as wParam. To actually figure out what happened, store some additional information in another class member, like m_nLastItemClicked or ... you get the idea?!


Check the MFCIE sample, it generates a favorite menu from the user's favorite folder and navigates to the favorite url when a favorite menu item is clicked.


Just add ON_COMMAND (and ON_UPDATE_COMMAND_UI if necessary) handlers for the menu items' IDs on your class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号