I very appreciate that you shared your idea and code. it helps me a lot.
But there is a very funny thing that I can not make right click working for popup menu, I need to double click right botton to popup menu. It seems the only one click did not send message WM_CONTEXTMENU to my program.
I know there is some problem of my code, but where?
This code works only;
user must left click the menu item before right clicking the menu item. this combination will make the code work.
but if you select an item in the list, and after that if you right click another item this code does not work correctly. the selected item and the right clicked item is different.
i discover this when i try to disable the menu item when root is clicked and eanble when a chld is clicked. is there a method to solve this problem. thank you fro your comments and codes.......
These days it's almost impossible to get ANY work done without MFC. Microsoft sure did make life difficult for the non-mfc programmer. But I was taught windows programming by Charles Petzold's fine book, so I do my best to stay out of mfc's way. I fought a long battle to get something like this to work without mfc, and finally gave up. I throught that the one sure way to ALWAYS get the right coordinates of the mouse event (even though the tree view does it's best not to give it to me) was through a mouse hook.
The NM_RCLICK notifies me that right mouse button was clicked, and I react appropriately. All I need is the right screen coordinates for my popup menu.
I ended up hooking mouse messages to my thread:
HHOOK g_hook ;
POINT g_point ;
// then in my initialization code
g_hook = SetWindowsHookEx (WH_MOUSE, MouseProc, hInstance, GetCurrentThreadId()) ;
// and in my cleanup code
UnhookWindowsHookEx (g_hook) ;
Private Sub TreeView_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Long, ByVal y As Long)
'check if right-click AND click on a node
If Button = 2 And Not TreeView.HitTest(x, y) Is Nothing Then
'select the node in TreeView
Set TreeView.SelectedItem = TreeView.HitTest(x, y)
To do this, i use the NM_RCLICK event. This event is use when the user has clicked the right mouse button within the control. So with the GetMessagePos() we can get the cursor position into the screen.
// Get the the cursor position into the screen
DWORD Result = GetMessagePos();
CPoint ItemPos(GET_X_LPARAM(Result), GET_Y_LPARAM(Result));
// Retreive the coordinates of the treecrtl viwndow
// Surche the item which have the focus right click. Is not the same think to the selected
// item. During the right click, the item receive a selected option, but the selected item
// not change.
m_treectrl.GetItemRect( hItem, &ItemRect, FALSE );
// If thye scursor is in the bounding rectangle
// If the Item is a main parent (no have parent) or have a children, use the SCRIPT_MAIN_
if(m_treectrl.ItemHasChildren( hItem ) || !m_treectrl.GetParentItem( hItem ))
Using your code in a VCPP6.0 dialog-based app, I can get the popup menu to work just fine, but it takes two clicks (or a double click??) to get it to come up. One click simply doesnt do it. This is annoying and puzzling both. Any ideas how I can eliminate the need for double clicking?