gpt4 book ai didi

c# - Listbox 手动 DrawItem 大字体

转载 作者:可可西里 更新时间:2023-11-01 09:09:07 26 4
gpt4 key购买 nike

我正在尝试绘制以红色 * 字符结尾的项目(并删除该 * 字符)并以黑色绘制其他项目。

这是我的代码:

    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground() ; //Draw our regular background
if (Microsoft.VisualBasic.Strings.Right(listBox1.Items[e.Index].ToString(), 1) == "*")
{
e.Graphics.DrawString(Microsoft.VisualBasic.Strings.Mid(listBox1.Items[e.Index].ToString(),1,listBox1.Items[e.Index].ToString().Length - 1), e.Font, Brushes.Red, e.Bounds); //Draw the item text in red!
}
else
{
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds); //Draw the item text in its regular color
}
}

此外,列表框的 DrawMode 属性设置为 OwnerDrawVariable

当列表框的字体是默认字体时,我的代码工作正常。

但是当我将字体大小从 8.25(默认大小)更改为 14 时,一半的文本绘制在列表框中。像这样: My listbox when size is 14!

但是使用默认字体大小,一切都是正确的。

问题是什么?

最佳答案

你必须处理 MeasureItem事件并在那里设置项目的高度:

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = listBox1.Font.Height;
}

关于c# - Listbox 手动 DrawItem 大字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8835568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com