【问题标题】:Windows.Forms.Listbox selected item font size is changingWindows.Forms.Listbox 所选项目字体大小正在改变
【发布时间】:2011-01-04 18:25:58
【问题描述】:

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

我还跳过了一些 DrawItem 事件以防止闪烁。
一切正常,但是当我在列表框中选择项目时,字体看起来正在缩小或变得更加压缩。
然后当我鼠标离开时,字体会恢复到正常大小和外观,但仍然选择相同的项目(这是正确的)。 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.

有什么想法吗?

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

【问题讨论】:

  • 因为我对 C#.Net 中的答案也很满意。我只是想要一些 .net 代码来解决这个问题。
  • 你能做到这一点真是一个奇迹。但是不,您将永远与文物作斗争。像 ListBox 这样的原生 Windows 控件包装器不支持 UserPaint。
  • 一定是我缺少一些东西。为什么我能够使其他所有内容看起来都像我想要的那样,除非一个项目被选中?

标签: .net vb.net winforms


【解决方案1】:

处理 OnSelectionIndexChanged 并调用 Refresh 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多