我設法自定義帶有圖像的正常列表框,當在ownerdrawn中選擇項目時更改文本和背景顏色,我現在想要實現的是在項目上繪製自定義突出顯示顏色當鼠標懸停是在列表框上的項目,是可能或不...,我提供我下面的示例代碼是什麼我走了這麼遠..鼠標懸停在項目上時突出顯示列表框項目
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()
感謝,它的工作的......有一點小問題,我該如何最小化閃爍,我認爲它重繪每次移動時鼠標放在物品上,或者有另一種方式做...但除此之外,它的效果很好 – mercenary
@mercenary ListBox控件相當無法控制地閃爍着快樂,但我更新了該帖子以減少其中的一些內容。考慮使用ListView控件。 – LarsTech
謝謝,我在想使用listview,也許在下一個版本的工具,它的文件夾圖標轉換器使用列表框一次容納多個文件夾... – mercenary