Windows.Forms.Listbox选中的项目字体大小正在变化

时间:2011-01-04 18:25:59

标签: .net vb.net winforms

我继承了Windows.Forms.Listbox,因此我可以覆盖OnPaintOnDrawItem方法,以突出显示特定项目和备用背景颜色。

我也正在跳过一些DrawItem事件以防止闪烁 一切都运行正常,但是当我在列表框中选择项目时,看起来字体正在缩小或变得更加压缩。
然后当我离开时,字体恢复到正常大小和外观,但仍然选择相同的项目(这是正确的)。在调试过程中,字体永远不会改变,也不会改变它的任何属性,但是当选择了项目并且刚刚发生了mousedown事件时,它最终看起来会有所不同。

任何想法?

Public Sub New(ByVal relativityHighlightColor As System.Drawing.Color)
            _relativityHighlightColor = relativityHighlightColor
            Me.SetStyle( _
             System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer Or System.Windows.Forms.ControlStyles.ResizeRedraw Or System.Windows.Forms.ControlStyles.UserPaint, True)
            Me.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
            Me.IntegralHeight = False
        End Sub

        Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
            Dim rowBackColor As System.Drawing.Color = If(Me.AlternateColors AndAlso e.Index Mod 2 = 1, System.Drawing.Color.LightGray, e.BackColor)
            Dim newArgs As System.Windows.Forms.DrawItemEventArgs = e
            If Me.Items.Count > 0 Then
                newArgs = New System.Windows.Forms.DrawItemEventArgs(e.Graphics, e.Font, Me.GetItemRectangle(e.Index), e.Index, e.State, e.ForeColor, rowBackColor)
                newArgs.DrawBackground()
                newArgs.Graphics.DrawString(Me.Items(e.Index).ToString(), newArgs.Font, New System.Drawing.SolidBrush(If(newArgs.Index = _highlightIndex, Me.MyHighlightColor, newArgs.ForeColor)), New System.Drawing.PointF(e.Bounds.X, e.Bounds.Y))
            End If
            MyBase.OnDrawItem(newArgs)
        End Sub

        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            Dim region As New System.Drawing.Region(e.ClipRectangle)
            e.Graphics.FillRegion(New System.Drawing.SolidBrush(Me.BackColor), region)
            If Me.Items.Count > 0 Then
                For i As Int32 = 0 To Me.Items.Count - 1 Step 1
                    Dim rect As System.Drawing.Rectangle = Me.GetItemRectangle(i)
                    If (e.ClipRectangle.IntersectsWith(rect)) Then
                        If ((Me.SelectionMode = System.Windows.Forms.SelectionMode.One AndAlso Me.SelectedIndex = i) OrElse (Me.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple AndAlso Me.SelectedIndices.Contains(i)) OrElse Me.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended AndAlso Me.SelectedIndices.Contains(i)) Then
                            OnDrawItem(New System.Windows.Forms.DrawItemEventArgs(e.Graphics, Me.Font, rect, i, System.Windows.Forms.DrawItemState.Selected, Me.ForeColor, Me.BackColor))
                        Else
                            OnDrawItem(New System.Windows.Forms.DrawItemEventArgs(e.Graphics, Me.Font, rect, i, System.Windows.Forms.DrawItemState.Default, Me.ForeColor, Me.BackColor))
                        End If
                        region.Complement(rect)
                    End If
                Next
            End If
            MyBase.OnPaint(e)
        End Sub

1 个答案:

答案 0 :(得分:0)

处理OnSelectionIndexChanged并调用Refresh方法。