如何处理 WTL/Win32 应用程序中树视图的右键单击?

发布于 2024-07-15 23:32:08 字数 122 浏览 4 评论 0原文

我有一个用 ATL 编写的基本应用程序,使用 VS2008 的向导。 我在应用程序的左侧有一个树视图。 我了解了如何(痛苦地)添加树项目。 问题是当鼠标右键单击时如何显示菜单? 如何捕获每个可以选择的项目上的任何点击事件?

I have a basic app written with ATL, using the wizard with VS2008. I have a treeview in the left side of the app. I see how to (painfully) add tree items. Question is how do I show a menu when the mouse is right clicked? How do I trap any click events on each item that could be selected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轮廓§ 2024-07-22 23:32:08

您应该检测到 WM_CONTEXTMENU Windows 消息通过在消息映射中指定处理程序。 然后,您可以在处理程序中显示上下文菜单。 然后,您需要确保在从上下文菜单中选择命令时也处理消息映射中的菜单命令。 在消息中使用 COMMAND_HANDLER 宏这部分的地图。

You should detect the WM_CONTEXTMENU windows message by specifying a handler in your message map. In the handler you can then show the context menu. You then need to make sure you also handle the menu commands in your message map for when a command is selected from the context menu. Use the COMMAND_HANDLER macro in your message map for this part.

清醇 2024-07-22 23:32:08

杰夫·耶茨的回答给了我方向。 由于我使用的是 C,所以解决方案有点不同(并且像往常一样,有点复杂):

想法是计算树视图中执行右键单击的点,然后获取项目。 现在您可以检查项目的类型并显示相应的上下文菜单。 为了防止用户混淆,下面还选择树视图中的右键节点。

该示例假设您的对话框中有一个树视图。 您可能需要在对话框中循环浏览树视图。

    case WM_CONTEXTMENU:
    {
        RECT rcTree;
        HWND hwndTV;
        HTREEITEM htvItem;
        TVHITTESTINFO htInfo = {0};

        long xPos = GET_X_LPARAM(lParam);   // x position from message, in screen coordinates
        long yPos = GET_Y_LPARAM(lParam);   // y position from message, in screen coordinates 

        hwndTV=GetDlgItem(hDlg, IDC_TREE1);         // get the tree view 
        GetWindowRect(hwndTV,&rcTree);              // get its window coordinates
        htInfo.pt.x= xPos-rcTree.left;              // convert to client coordinates
        htInfo.pt.y= yPos-rcTree.top;

        if (htvItem=TreeView_HitTest(hwndTV, &htInfo)) {    // hit test
            TreeView_SelectItem(hwndTV, htvItem);           // success; select the item
            /* display your context menu */
        }
        break;
    }

下面显示了一个上下文菜单:

        RECT winPos, itemPos;

        HMENU hCtxtMenuBar= LoadMenu(ghInst,MAKEINTRESOURCE(ID_CTXTMENUS_RESOURCE));
        HMENU hCtxtMenu= GetSubMenu(hCtxtMenuBar, MY_CTXMENU);

        TreeView_GetItemRect(hwndTV, htvItem, &itemPos, TRUE);
        GetWindowRect(hwndTV, &winPos);

        SendMessage (hDlg, WM_COMMAND, 
            TrackPopupMenuEx(hCtxtMenu,TPM_LEFTALIGN|TPM_TOPALIGN|TPM_NONOTIFY|TPM_RETURNCMD,
                winPos.left+itemPos.left, winPos.top+itemPos.bottom, hDlg,0),
            (LPARAM)hwndTV);

Jeff Yates answer gave me the direction. Since I am using C, the solution is a bit different (and, as usual, a bit more complex):

The idea is to calculate the point in the tree view where the right click was performed, then get the item. Now you can check the type of the item and display the appropriate context menu. To prevent user confusion, the following also selects the right-clicked node in the tree view.

The example assumes there is one tree view in your dialog. You may need to cycle over the tree views in your dialog.

    case WM_CONTEXTMENU:
    {
        RECT rcTree;
        HWND hwndTV;
        HTREEITEM htvItem;
        TVHITTESTINFO htInfo = {0};

        long xPos = GET_X_LPARAM(lParam);   // x position from message, in screen coordinates
        long yPos = GET_Y_LPARAM(lParam);   // y position from message, in screen coordinates 

        hwndTV=GetDlgItem(hDlg, IDC_TREE1);         // get the tree view 
        GetWindowRect(hwndTV,&rcTree);              // get its window coordinates
        htInfo.pt.x= xPos-rcTree.left;              // convert to client coordinates
        htInfo.pt.y= yPos-rcTree.top;

        if (htvItem=TreeView_HitTest(hwndTV, &htInfo)) {    // hit test
            TreeView_SelectItem(hwndTV, htvItem);           // success; select the item
            /* display your context menu */
        }
        break;
    }

The following displays a context menu:

        RECT winPos, itemPos;

        HMENU hCtxtMenuBar= LoadMenu(ghInst,MAKEINTRESOURCE(ID_CTXTMENUS_RESOURCE));
        HMENU hCtxtMenu= GetSubMenu(hCtxtMenuBar, MY_CTXMENU);

        TreeView_GetItemRect(hwndTV, htvItem, &itemPos, TRUE);
        GetWindowRect(hwndTV, &winPos);

        SendMessage (hDlg, WM_COMMAND, 
            TrackPopupMenuEx(hCtxtMenu,TPM_LEFTALIGN|TPM_TOPALIGN|TPM_NONOTIFY|TPM_RETURNCMD,
                winPos.left+itemPos.left, winPos.top+itemPos.bottom, hDlg,0),
            (LPARAM)hwndTV);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文