I have created a shortcut menu for a tree and am triggering processing off the Win32 WM_CONTEXTMENU message (activated by right click). However, I am having problems with making the item right-clicked be the selected item in the tree view. I thought the way to do this was using the Hit Test, but it does not seem to work. I have consulted the Win32 help file and a few other Internet sources, but I cannot figure out what I am doing wrong.

Any help or pointers would be appreciated. Below you will find the code I`m using to process the WM_CONTEXTMENU message.

I often get the "No item selected" error. Thanks, Alan

case WM_CONTEXTMENU:
{
hit_test.pt.x = LOWORD(lParam);
hit_test.pt.y = HIWORD(lParam);
hit_test.flags = TVHT_ONITEMRIGHT;
Selected = TreeView_HitTest(tree_hwnd, &hit_test);
if (Selected != NULL)
{
TreeView_SelectItem(tree_hwnd, Selected);
TrackPopupMenuEx(hMenu, TPM_VERTICAL, (int)LOWORD(lParam), (int)HIWORD(lParam), hwnd, NULL);
}
else MessageBox(NULL, TEXT("No item selected"),
TEXT("ERROR"), MB_OK | MB_ICONWARNING);
}
break;