Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The context menu is getting pop-up on item selection ( not on Text selection) ,
Failed to show bitmaps/icons on context menu

What I have tried:

The context menu is popping up on entire treelist with some item selected .
Posted
Updated 17-Jul-18 23:42pm

1 solution

To get context menu pop- up on exact tree-list ctrl text Add this inside
OnContextMenu function

// If you want this to be done from right click OnNMRCLlickTree1 event you can calulate point as

//::GetCursorPos(&point);

    UINT uFlags;
    CRect rc;
    CPoint clickpoint = point;
    CPoint mypoint = point;
    m_str_tree.ScreenToClient(&mypoint);
    HWND htemp;
    :: GetDlgItem(htemp,IDC_TREE);

    HTREEITEM htItem = m_str_tree.HitTest(mypoint, &uFlags);
    //To know which text is selcted
    CString  TreeText2 = m_str_tree.GetItemText(htItem);
    m_str_tree.GetItemRect(htItem, &rc, TRUE);

     // To check right click is on item text or not
    if (mypoint.x > rc.right || mypoint.x < rc.left)
    {
        return;
    }

or//<pre> if (! (htItem != nullptr && (TVHT_ONITEMLABEL & uFlags)))
    {
        return;
    }

 CMenu mnuPopupSubmit;
        //Add your menu id
        mnuPopupSubmit.LoadMenu(IDR_MENU1);

         CMenu *mnuPopupMenu = mnuPopupSubmit.GetSubMenu(0);
        ASSERT(mnuPopupMenu);

            mnuPopupMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, clickpoint.x, clickpoint.y, this);

<pre>//Adding images to the context Menu

void CPathFinderDlg::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
    CDialogEx::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);

    // TODO: Add your message handler code here



    HICON hIcon = AfxGetApp()->LoadIcon(IDB_IMAGE);
    pPopupMenu->

//or directly load bitmaps as  CBitmap m_bitmapvar_16;
//m_bitmapvar_16.LoadBitmap(IDB_IMAGE); 
//ID__File is menu's option id

SetMenuItemBitmaps(ID__File, MF_BYCOMMAND, &m_bitmapvar_16, NULL);


}
 
Share this answer
 
v8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900