鼠标悬停在项目上时突出显示列表框项目

问题描述 投票:3回答:2

我设法在owner55中选择项目时自定义带有图像,更改文本和背景颜色的普通列表框,我现在想要实现的是当鼠标悬停在列表框项目上时在项目上绘制自定义高亮颜色,是那可能与否......,我提供了下面的示例代码,说明我到目前为止所做的事情。

If e.Index = -1 Then Exit Sub
        Dim listBox As ListBox = CType(sender, ListBox)
        e.DrawBackground()
        Dim isItemSelected As Boolean = ((e.State And DrawItemState.Selected) = DrawItemState.Selected)
        If e.Index >= 0 AndAlso e.Index < listBox.Items.Count Then
            Dim textSize As SizeF = e.Graphics.MeasureString(listBox.Items(e.Index).ToString(), listBox.Font)
            Dim itemImage As Image = My.Resources.FolderHorizontal
            'set background and text color
            Dim backgroundColorBrush As New SolidBrush(If((isItemSelected), Color.CornflowerBlue, Color.White))
            Dim itemTextColorBrush As Color = If((isItemSelected), Color.White, Color.Black)

            e.Graphics.FillRectangle(backgroundColorBrush, e.Bounds)
            'draw the item image
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality
            e.Graphics.DrawImage(itemImage, e.Bounds.X + 2, _
                               e.Bounds.Y + (e.Bounds.Height - textSize.Height) / 2, _
                               itemImage.Width, itemImage.Height)
            'draw the item text
            Dim x, y As Single
            Dim h As Single = textSize.Height
            Dim rect As Rectangle = e.Bounds
            rect.X += listBox.ItemHeight
            rect.Width -= listBox.ItemHeight

            x = rect.X - 3
            y = rect.Y + (rect.Height - h) / 2

            Dim itemText As String = listBox.Items(e.Index).ToString()
            TextRenderer.DrawText(e.Graphics, itemText, e.Font, _
                                  New Rectangle(x, y, ClientRectangle.Width, ClientRectangle.Height), _
                                  itemTextColorBrush, TextFormatFlags.Default)
            'clean up
            backgroundColorBrush.Dispose()

        End If
        e.DrawFocusRectangle()
vb.net winforms
2个回答
2
投票

您可以使用IndexFromPoint执行类似的操作:

Dim mouseIndex As Integer = -1

Private Sub ListBox1_MouseMove(sender As Object, e As MouseEventArgs) _
                               Handles ListBox1.MouseMove
  Dim index As Integer = ListBox1.IndexFromPoint(e.Location)
  If index <> mouseIndex Then
    If mouseIndex > -1 Then
      Dim oldIndex As Integer = mouseIndex
      mouseIndex = -1
      If oldIndex <= ListBox1.Items.Count - 1 Then
        ListBox1.Invalidate(ListBox1.GetItemRectangle(oldIndex))
      End If
    End If
    mouseIndex = index
    If mouseIndex > -1 Then
      ListBox1.Invalidate(ListBox1.GetItemRectangle(mouseIndex))
    End If
  End If
End Sub

然后在你的绘图代码中:

If mouseIndex > -1 AndAlso mouseIndex = e.Index Then
  backgroundColorBrush = New SolidBrush(Color.DarkMagenta)
End If

1
投票

我会告诉你如何做到这一点。所有专家都说它很复杂,不能用列表框来完成......我能在5分钟内做到这一点

我创建的列表框的名称是listPOSSIBILITIES

1)创建一个对您的表单是全局的变量

    Dim HOVERTIME As Boolean = True

2)创建MouseEnter事件

Private Sub listPOSSIBILITIES_MouseEnter(sender as Object,e as system.EventArgs)handle listPOSSIBILITIES.MouseEnter

    HOVERTIME = True

结束子

3)创建MouseLeave事件

Private Sub listPOSSIBILITIES_MouseLeave(sender As Object,e As System.EventArgs)handle listPOSSIBILITIES.MouseLeave

    HOVERTIME = False

结束子

4)创建MouseMove事件

Private Sub listPOSSIBILITIES_MouseMove(sender As Object,e As System.Windows.Forms.MouseEventArgs)handle listPOSSIBILITIES.MouseMove

    Dim mypoint As Point
    mypoint = listPOSSIBILITIES.PointToClient(Cursor.Position)
    Dim myindex As Integer = listPOSSIBILITIES.IndexFromPoint(mypoint)
    If myindex < 0 Then Exit Sub
    listPOSSIBILITIES.SelectedIndex = myindex

结束子

5)创建MouseClick事件

Private Sub listPOSSIBILITIES_MouseClick(sender As Object,e As System.Windows.Forms.MouseEventArgs)处理listPOSSIBILITIES.MouseClick

    HOVERTIME = False

结束子

6)创建SelectedIndexChanged事件

Private Sub listPOSSIBILITIES_SelectedIndexChanged(sender As System.Object,e As System.EventArgs)处理listPOSSIBILITIES.SelectedIndexChanged

    If HOVERTIME Then Exit Sub

    'put the rest of your code after this above If statement

这是有效的,因为MouseIn事件在SelectedIndexChanged事件之前触发

© www.soinside.com 2019 - 2024. All rights reserved.