vb.net - 更改列表框的颜色、突出显示、背景大小?

标签 vb.net winforms listbox

https://i.imgur.com/2LTdcR2.png

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

enter image description here

这是我当前的代码:

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”来增加突出显示值的矩形/大小,但似乎没有任何效果。

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

最佳答案

当您设置DrawModeListBox 控件的 OwnerDrawVariable或者在创建控制句柄后(即在处理了 WM_MEASUREITEM 消息之后)更改字体大小,您需要手动设置 ItemHeigh属性设置为新的字体高度。

ItemHeight 属性设置为订阅 ListBox MeasureItem事件并设置 MeasureItemEventArgs e.ItemHeight 属性。

此外,如果您即时更改字体大小,您还需要强制将 WM_MEASUREITEM 消息重新发送到 ListBox 控制,否则 Items Bounds 将不会更新。
换句话说,当引发 DrawItem 事件时,DrawItemEventArgs e.Bounds 属性将报告错误的测量值。

强制 ListBox 控件重新测量其项目边界的一种方法是设置 ListBox.DrawMode = DrawMode.Normal 并立即将其重置回 OwnerDrawVariable。这会导致再次处理 WM_MEASUREITEM 消息。

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

这里,我使用 Font.Height 来测量 MeasureItem 事件中的当前 ItemHeight,因为它会向上舍入测量值。您可以使用 TextRenderer.MeasureTextFont.GetHeight() ;您最终会得到相同的度量,但向下舍入。

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

测试调整字体大小:

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()

enter image description here

关于vb.net - 更改列表框的颜色、突出显示、背景大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50715075/

相关文章:

c# - 这段代码能正确判断两种类型是否相等吗?

vba - 在 Word VBA 中右键单击选择列表框项

c# - 列表框项目删除

c# - 如何删除由 c# 中的 xmlreader 打开的文件?

javascript - 使用 z-index 放在前面后下拉列表停止工作

c# - 如何创建共享代码项目 (.shproj)

asp.net - 如何检查 SqlDataSource 是否返回数据?

vb.net - 在 VB.NET 中声明一个字节数组

c# - 删除(x);它会处理元素 x 吗?

winforms - 如何用 DevExpress 替换消失的 60 个免费控件?