views:

505

answers:

3

Hi,

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->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;
    }
A: 

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

djeidot
but these are dynamic menu items, for which I do not know the ID's
JPro
do you know the range of possible ID numbers? In that case you could add ON_COMMAND_RANGE
djeidot
I am generating just one menu item. I tried using the ON_COMMAND_RANGE, but could understand how to use this for unknown ( just 1) dynamic menu
JPro
@dwo provided the correct answer, I didn't realize you were inserting the menu item with NULL as the ID.
djeidot
+2  A: 

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?!

dwo
I dont exactly understand how to override onCommand, my message map has this currently ON_COMMAND(IDM_FILE_OPEN, OnFileOpen)
JPro
You simply add the ON_COMMAND(WM_MYMESSAGE, OnMyMessage) handler, assuming you used WM_MYMESSAGE on InsertMenu
djeidot
A: 

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.

Sheng Jiang 蒋晟