开发者

Make the PopupMenu appear at the selected Item of a TreeView in VB6

开发者 https://www.devze.com 2023-03-21 09:54 出处:网络
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.

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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消