开发者

Windows.Forms.Listbox selected item font size is changing

开发者 https://www.devze.com 2023-02-03 00:47 出处:网络
I have inherited from a Windows.Forms.Listbox so I could override the OnPaint and OnDrawItem methods in an effort to highlight specific items and alternate backcolors.

I have inherited from a Windows.Forms.Listbox so I could override the OnPaint and OnDrawItem methods in an effort to highlight specific items and alternate backcolors.

I am also skipping some DrawItem events to prevent flickering.

Everything is working fine, however when I select items in the listbox, it looks like the font is shrinking or becoming more compressed.

Then when I mouse leave, the font goes back to its normal size and appearance but the same items are still selected (which is correct). During debugging the font never changes nor do any of its properties, but it does end up looking different when the item is selected and the mousedown event just happened.

Any Ideas?

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)) OrEls开发者_开发百科e 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


Handle OnSelectionIndexChanged and call Refresh method.

0

精彩评论

暂无评论...
验证码 换一张
取 消