Re: menus and right clicking
You should do it for the WM_CONTEXTMENU message.
This is the method that I'm using. It's called in the derived
CTreeCtrl class. I'm thinking that this might be the issue, since it
would make more sense to call this method in the parent dialog class.
What is your feedback in regards to this?
It is normal to handle the message in the parent control of the tree
control, however I don't see what effect that would have on your
problem.
I notice you're selecting the item before displaying your menu. In a
similar situation I did this:
I've simulated the way Explorer's right click works in an application
I've worked on. Here's the essence of what I've done...
I handle the NM_RCLICK message and if the clicked item isn't the
currently selected item I save the clicked item and then call my
WM_CONTEXTMENU handler (so the same code works from the keyboard
operations).
In the OnContextMenu handler I use TrackPopupMenu with the
TPM_RETURNCMD flag. This lets me temporarily select the item that was
right clicked, execute any menu command by sending a WM_COMMAND
message, restore the original selected item. then null out the saved
clicked item:
CWnd * pMenuParent = AfxGetMainWnd();
UINT uCmd = pop->TrackPopupMenu( TPM_RETURNCMD | TPM_LEFTALIGN
| TPM_RIGHTBUTTON,
point.x, point.y, pMenuParent, NULL );
/* Selected a menu? */
if ( uCmd != 0 )
{
/* temporarily select any right-clicked item that is
* different from the currently selected item
*/
if ( m_hTempSelect )
{
HTREEITEM hOldSel = tc.GetSelectedItem();
tc.Select( m_hTempSelect, TVGN_CARET );
/* Execute the selected menu command */
pMenuParent->SendMessage( WM_COMMAND, uCmd,
0 );
tc.Select( hOldSel, TVGN_CARET );
}
else
{
/* Execute the selected menu command */
pMenuParent->SendMessage( WM_COMMAND, uCmd,
0 );
}
}
/* no longer need the temporary selected item */
m_hTempSelect = NULL;
Dave