更改列表框的颜色,突出显示,背景大小?

问题描述:


因此,我四处搜索并设法突出显示ListBox中的值,但是我试图增加ListBox中文本的字体大小,但其结果如下图所示:

So I've searched around and managed to highlight the values in my ListBox, but I'm trying to increase the font size for the text inside the ListBox but it results in the following image as attached:


这是我当前的代码:

This is my current code:

Private Sub ListBox1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

    e.DrawBackground()

    If ListBox1.Items(e.Index).ToString().Contains("*") Then
        e.Graphics.FillRectangle(Brushes.Red, e.Bounds)
    End If

    e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, Brushes.Black, New System.Drawing.PointF(e.Bounds.X, e.Bounds.Y))
    e.DrawFocusRectangle()

我尝试弄乱"e.Bounds.X,e.Bounds.Y"以增加突出显示值的矩形/大小,但似乎无济于事.

I've tried messing with the "e.Bounds.X, e.Bounds.Y" to increase the rectangle/size of the highlighted value but nothing seemed to work.

如何允许突出显示的矩形根据字体大小增加?

How does one allow the highlighted rectangle increase according to the font size?

设置 OwnerDrawVariable ,或者在创建控件句柄后更改字体大小(即在它已经处理完WM_MEASUREITEM消息之后),您需要手动设置

When you set the DrawMode of a ListBox control to OwnerDrawVariable or you change the Font size after the control handle has been created (i.e. after it has already processed the WM_MEASUREITEM message), you need to manually set the ItemHeigh property to the new Font height.

ItemHeight属性设置为订阅ListBox MeasureItemEventArgs e.ItemHeight属性.

The ItemHeight property is set subscribing the ListBox MeasureItem event and setting the MeasureItemEventArgs e.ItemHeight property.

此外,如果您即时更改字体大小 ,还需要强制将WM_MEASUREITEM消息重新发送到ListBox控件,否则,项目边界不会进行更新.
换句话说,当引发DrawItem事件时,

Also, if you change the Font size on the fly, you also need to force the WM_MEASUREITEM message to be re-sent to the ListBox control, otherwise the Items Bounds will not be updated.
In other words, when the DrawItem event is raised, the DrawItemEventArgs e.Bounds property will report wrong measures.

强制ListBox控件重新测量其项目"范围的一种方法是设置ListBox.DrawMode = DrawMode.Normal,然后立即将其重置为OwnerDrawVariable.这将导致再次处理WM_MEASUREITEM消息.

A way to force the ListBox control to re-measure its Items bounds, is to set ListBox.DrawMode = DrawMode.Normal and immediatly resetting it back to OwnerDrawVariable. This causes the WM_MEASUREITEM message to be processed again.

listBox1.DrawMode = DrawMode.Normal
listBox1.DrawMode = DrawMode.OwnerDrawVariable
listBox1.Update()

在这里,我正在使用Font.Height来度量MeasureItem事件中的当前ItemHeight,因为它会四舍五入.您可以使用 Font.GetHeight();您将得到相同的结果,但四舍五入.

Here I'm using the Font.Height to measure the current ItemHeight in the MeasureItem event, because it rounds up the measure. You could use TextRenderer.MeasureText or Font.GetHeight(); you'll end up with the same measure, but rounded down.

Private Sub ListBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem

    Dim ItemForeColor As Color
    Dim ItemBackColor As Color

    e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit

    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
        ItemForeColor = Color.FromKnownColor(KnownColor.HighlightText)
        ItemBackColor = If(ListBox1.Items(e.Index).ToString().Contains("*"), Color.Red, Color.FromKnownColor(KnownColor.Highlight))
    Else
        ItemForeColor = ListBox1.ForeColor
        ItemBackColor = If(ListBox1.Items(e.Index).ToString().Contains("*"), Color.Red, ListBox1.BackColor)
    End If

    Using TextBrush As New SolidBrush(ItemForeColor)
        Using ItemBrush As New SolidBrush(ItemBackColor)
            e.Graphics.FillRectangle(ItemBrush, e.Bounds)
            e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), ListBox1.Font, TextBrush, e.Bounds, StringFormat.GenericTypographic)
        End Using
    End Using
    e.DrawFocusRectangle()
End Sub

Private Sub ListBox1_MeasureItem(sender As Object, e As MeasureItemEventArgs) Handles ListBox1.MeasureItem
    e.ItemHeight = ListBox1.Font.Height
End Sub

测试它调整字体大小:

Test it resizing the Font:

ListBox1.Font = New Font(ListBox1.Font.FontFamily, ListBox1.Font.SizeInPoints + 2, ListBox1.Font.Style, GraphicsUnit.Point)
ListBox1.DrawMode = DrawMode.Normal
ListBox1.DrawMode = DrawMode.OwnerDrawVariable
ListBox1.Height = '[OriginalHeight]
ListBox1.Update()