使 PopupMenu 出现在 VB6 中 TreeView 的选定项上

发布于 2024-11-24 12:46:34 字数 159 浏览 3 评论 0原文

我在VB6中有一个TreeView,当右键单击节点时,它使用PopupMenu。由于 VB6 PopupMenu 默认其位置为鼠标坐标,因此菜单出现在正确的位置。

我想要完成的是,当选择 TreeView 节点时,Popupmenu 也会出现在 KeyDown 事件的正确位置。我该怎么做?

I hava a TreeView in VB6 that uses a PopupMenu when a Node is right clicked. As the VB6 PopupMenu defaults its position to the mouse coordinates the menu appears at the right place.

What I want to accomplish is that the Popupmenu appears at the right place too on a KeyDown event when a TreeView Node is selected. How can I do this?

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

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

发布评论

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

评论(1

舂唻埖巳落 2024-12-01 12:46:34

您需要获取该项目的坐标。为此,您需要首先获取其句柄。当你得到矩形时,你必须将它转换为坐标。

Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Private Declare Function MapWindowPoints Lib "user32.dll" (ByVal hwndFrom As Long, ByVal hwndTo As Long, ByRef lppt As Any, ByVal cPoints As Long) As Long

Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type

Private Type RECTF
  Left As Single
  Top As Single
  Right As Single
  Bottom As Single
End Type

Private Const TV_FIRST As Long = &H1100&
Private Const TVM_GETITEMRECT As Long = (TV_FIRST + 4)
Private Const TVM_GETNEXTITEM As Long = (TV_FIRST + 10)
Private Const TVGN_CARET As Long = &H9&


Private Function GetSelectedItemRect(ByVal tv As TreeView, ByRef outRect As RECTF) As Boolean
  Dim hItem As Long
  hItem = SendMessage(tv.hwnd, TVM_GETNEXTITEM, TVGN_CARET, ByVal 0&)

  If hItem Then
    Dim r As RECT
    r.Left = hItem

    If SendMessage(tv.hwnd, TVM_GETITEMRECT, 1, r) Then
      MapWindowPoints tv.hwnd, Me.hwnd, r, 2

      outRect.Left = Me.ScaleX(r.Left, vbPixels, Me.ScaleMode)
      outRect.Top = Me.ScaleY(r.Top, vbPixels, Me.ScaleMode)
      outRect.Right = Me.ScaleX(r.Right, vbPixels, Me.ScaleMode)
      outRect.Bottom = Me.ScaleY(r.Bottom, vbPixels, Me.ScaleMode)

      GetSelectedItemRect = True
    End If
  End If

End Function

用法:

Dim r As RECT

If GetSelectedItemRect(TreeView1, r) Then
  PopupMenu whatever, , r.Right, r.Top
End If

You need to obtain coordinates of the item. For that you need to first obtain its handle. And when you get the rect, you must translate it to form coordinates.

Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Private Declare Function MapWindowPoints Lib "user32.dll" (ByVal hwndFrom As Long, ByVal hwndTo As Long, ByRef lppt As Any, ByVal cPoints As Long) As Long

Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type

Private Type RECTF
  Left As Single
  Top As Single
  Right As Single
  Bottom As Single
End Type

Private Const TV_FIRST As Long = &H1100&
Private Const TVM_GETITEMRECT As Long = (TV_FIRST + 4)
Private Const TVM_GETNEXTITEM As Long = (TV_FIRST + 10)
Private Const TVGN_CARET As Long = &H9&


Private Function GetSelectedItemRect(ByVal tv As TreeView, ByRef outRect As RECTF) As Boolean
  Dim hItem As Long
  hItem = SendMessage(tv.hwnd, TVM_GETNEXTITEM, TVGN_CARET, ByVal 0&)

  If hItem Then
    Dim r As RECT
    r.Left = hItem

    If SendMessage(tv.hwnd, TVM_GETITEMRECT, 1, r) Then
      MapWindowPoints tv.hwnd, Me.hwnd, r, 2

      outRect.Left = Me.ScaleX(r.Left, vbPixels, Me.ScaleMode)
      outRect.Top = Me.ScaleY(r.Top, vbPixels, Me.ScaleMode)
      outRect.Right = Me.ScaleX(r.Right, vbPixels, Me.ScaleMode)
      outRect.Bottom = Me.ScaleY(r.Bottom, vbPixels, Me.ScaleMode)

      GetSelectedItemRect = True
    End If
  End If

End Function

Usage:

Dim r As RECT

If GetSelectedItemRect(TreeView1, r) Then
  PopupMenu whatever, , r.Right, r.Top
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文