我设法使用图像自定义普通列表框,在ownerdrawn中选择项目时更改文本和背景颜色,我现在想要实现的是当鼠标悬停在列表框上时在项目上绘制自定义高亮颜色item,是否可能...,我在下面提供了我的示例代码到目前为止...
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()
答案 0 :(得分: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 :(得分:1)
我会告诉你如何做到这一点。所有专家都说它很复杂,无法用列表框完成......我能够在5分钟内完成这项工作
我创建的列表框的名称是listPOSSIBILITIES
1)创建一个对表单而言是全局的变量
Dim HOVERTIME As Boolean = True
2)创建MouseEnter事件
Private Sub listPOSSIBILITIES_MouseEnter(sender as Object,e as system.EventArgs)处理listPOSSIBILITIES.MouseEnter
HOVERTIME = True
End Sub
3)创建MouseLeave事件
Private Sub listPOSSIBILITIES_MouseLeave(sender As Object,e As System.EventArgs)处理listPOSSIBILITIES.MouseLeave
HOVERTIME = False
End Sub
4)创建MouseMove事件
Private Sub listPOSSIBILITIES_MouseMove(sender As Object,e As System.Windows.Forms.MouseEventArgs)处理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
End Sub
5)创建MouseClick事件
Private Sub listPOSSIBILITIES_MouseClick(sender As Object,e As System.Windows.Forms.MouseEventArgs)处理listPOSSIBILITIES.MouseClick
HOVERTIME = False
End Sub
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
这是有效的,因为在SelectIndexChanged事件
之前触发了MouseClick事件