CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2004
    Posts
    16

    Question Win32 Hit Test problems

    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;

  2. #2
    Join Date
    Jan 2004
    Posts
    56
    I think u should convert mouse's co-ordinates to client co-ordinates first.
    Trust urself!

  3. #3
    Join Date
    Apr 2004
    Posts
    16

    Thumbs up Got it

    It turns out that both the WM_CONTEXTMENU message and the TrackPopUpMenuEx use screen coordinates, but the Hit Test needs client coordinates. Doing the ScreentoClient conversion solved the problem. Code may be found below.

    Thanks for pointing me in the right direction. Alan

    case WM_CONTEXTMENU:
    {
    POINT point;
    point.x = LOWORD(lParam);
    point.y = HIWORD(lParam);
    ScreenToClient(hwnd, &point);
    hit_test.pt.x = point.x;
    hit_test.pt.y = point.y;
    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;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured