创建“鼠标悬停”对 VB TreeView 节点的影响

发布于 2024-11-04 18:54:34 字数 368 浏览 7 评论 0原文

TreeView 控件的节点没有要测试的“鼠标悬停”属性。我希望“突出显示”该节点(向用户提供有关选择哪个节点的反馈)。

例如,当 MouseMove 事件在 TreeView 控件上触发时,我可以将节点对象设置为“HitTest”返回的内容:

Set nde = trvChoices.HitTest(x, y * 15)

我正在寻找当鼠标悬停在该节点上方时“突出显示”该节点(或其他内容)的方法,以便向用户提供选择 TreeView 中哪个节点的反馈。是的,我使用 TreeView 作为“右键单击”菜单。我不想使用不同的控件,尽管我可能不得不......

Nodes of a TreeView control do not have a 'mouse over' property to test for. I was hoping to "highlight" the node (to give the user feedback on which one is selected).

For example, when the MouseMove event fires on the TreeView control, I can set a node object to what "HitTest" returns:

Set nde = trvChoices.HitTest(x, y * 15)

I am looking for a way to have this node "highlighted" (or something) when the mouse is over it, in order to give the user feedback of which node in the TreeView is selected. Yes, I am using TreeView as a 'right-click' menu. I do not wish to use a different control, although I may have to...

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

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

发布评论

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

评论(3

不寐倦长更 2024-11-11 18:54:34

让节点在悬停时变为粗体是理所当然的。但是,将 BackColor 或 ForeColor 设置为任何颜色(例如 wdYellow)只会使整个节点变黑...

发布示例代码,以防其他人遇到此情况:

    Private Sub trvChoices_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)

    If Not (trvChoices.HitTest(x, y * 15) Is Nothing) Then

        Dim nde As Node
        Set nde = trvChoices.HitTest(x, y * 15)

        'I have three nodes only, but the proper way would be to loop through the trvChoices and set      each node to Bold = False
        trvChoices.Nodes(1).Bold = False
        trvChoices.Nodes(2).Bold = False
        trvChoices.Nodes(3).Bold = False

        nde.Bold = True

        Set nde = Nothing
    End If

End Sub

It was a no-brainer to get the node to be Bold on hover. However, setting the BackColor or ForeColor to any color e.g. wdYellow would just black out the entire node...

Posting example code in case anyone else runs into this:

    Private Sub trvChoices_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)

    If Not (trvChoices.HitTest(x, y * 15) Is Nothing) Then

        Dim nde As Node
        Set nde = trvChoices.HitTest(x, y * 15)

        'I have three nodes only, but the proper way would be to loop through the trvChoices and set      each node to Bold = False
        trvChoices.Nodes(1).Bold = False
        trvChoices.Nodes(2).Bold = False
        trvChoices.Nodes(3).Bold = False

        nde.Bold = True

        Set nde = Nothing
    End If

End Sub
錯遇了你 2024-11-11 18:54:34

我一直在尝试让 OLEDragDrop 与 Treeview 和 Listview 一起使用,但遇到了一个问题,即 StartDrag 试图在用户启动 StartDrag 之前遍历 Treeview 中处于活动状态的项目,而不是他们想要的项目。试图拖过去。我在其他地方看到过要求用户在拖动之前单击某个项目的解决方案,但这是违反直觉的。通过稍微修改您的代码,我能够将鼠标下的项目设置为活动项目:
(a) 向用户提供反馈并且
(b) 使 OLEDragDrop 正常工作。

Private Sub trvChoices_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)

  If Not (trvChoices.HitTest(x * 15, y * 15) Is Nothing) Then

    Dim nde As node
    Set nde = trvChoices.HitTest(x * 15, y * 15)

    nde.Selected = True

    Set nde = Nothing
  End If

End Sub

I've been trying to get OLEDragDrop to work with a Treeview and Listview and had run into an issue where the StartDrag tried to take across the item that was active in the Treeview before the user had started the StartDrag, rather than the item that they were trying to drag across. I had seen solutions elsewhere that required the user to click on an item before dragging, but this was counterintuitive. By modifying your code slightly I was able to set the item under the mouse as the active item which:
(a) gives feedback to the user and
(b) makes the OLEDragDrop work correctly.

Private Sub trvChoices_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)

  If Not (trvChoices.HitTest(x * 15, y * 15) Is Nothing) Then

    Dim nde As node
    Set nde = trvChoices.HitTest(x * 15, y * 15)

    nde.Selected = True

    Set nde = Nothing
  End If

End Sub
忆沫 2024-11-11 18:54:34

* 15 用于像素/缇转换。不幸的是,它并不适用于所有显示器,因为不同的显示器在像素和缇之间具有不同的速率,具体取决于显示器比率。但 15 确实符合标准 4:3 显示器。

Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, _
  ByVal nIndex As Long) As Long

Const WU_LOGPIXELSX = 88
Const WU_LOGPIXELSY = 90

使用上面的方法来得到这个。

此外,您需要检查是否需要进行转换,因为不同版本使用不同的输出 x,y 值。

The * 15 is for pixel/twip conversion. Unfortunately it doesn't work for all monitors as different monitors have a different rate between pixel and twips depending on monitor ratio. BUT 15 does comply to standard 4:3 monitors.

Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, _
  ByVal nIndex As Long) As Long

Const WU_LOGPIXELSX = 88
Const WU_LOGPIXELSY = 90

Use above to get this.

Additionally you need to check if you need to do the conversion at all as different versions use different output x,y values.

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