我制作了一个自定义ListBox,因此我可以改变它的绘制方式。我已将DrawMode设置为OwnerDrawVariable,并已处理DrawItem和MeasureItem事件。
当我向下滚动列表框时,它似乎向错误的方向滚动。它最终在正确的位置,但动画不正确。
问题似乎与OwnerDrawVariable有关,因为它在OwnerDrawFixed上滚动得很好。但是我必须使用OwnerDrawVariable。有什么想法吗?
Private Sub wrapListBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
e.DrawBackground()
Dim myBrush As Brush = Brushes.Black
If e.Index Mod 2 = 0 Then
e.Graphics.FillRectangle(Brushes.LightGray, e.Bounds)
Else
e.Graphics.FillRectangle(Brushes.White, e.Bounds)
End If
If (e.State And DrawItemState.Selected) Then
e.Graphics.FillRectangle(Brushes.DarkBlue, e.Bounds)
myBrush = Brushes.White
End Ifsettings.
e.Graphics.DrawString(Me.Items(e.Index), Me.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()
End Sub
Private Sub wrapListBox_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Me.MeasureItem
Dim g As Graphics = e.Graphics
Dim size As SizeF = g.MeasureString(Me.Items.Item(e.Index).ToString, Me.Font, Me.Width - 5 - SystemInformation.VerticalScrollBarWidth)
e.ItemHeight = CInt(size.Height) + 5
e.ItemWidth = CInt(size.Width) + 5
End Sub