列表框手动DRAWITEM大字体大小 [英] Listbox manual DrawItem big font size

查看:157
本文介绍了列表框手动DRAWITEM大字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想画的项目,他们到底是一个 * 红色字符(并删除 * 字符)并绘制在黑色的其他项目

I'm trying to draw items that end of them is an * character in red (and remove that * character) and draw other items in black color.

这是我的代码:

    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

Also DrawMode property of the listbox is set to OwnerDrawVariable.

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

My code is working fine when the font of listbox is the default font.

但是,当我改变从8.25(默认大小),字体大小为14,一半文本借鉴了列表框。像这样:

But When I change the font size from 8.25 (default size) to 14, half of the text draws on the listbox. like this:

但随着默认字体大小,一切是正确的。

But with default font size, everything is correct.

这是什么问题?

推荐答案

您必须处理的 MeasureItem 事件,并设置项目有高度:

You have to handle the MeasureItem event and set the height of the items there:

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

这篇关于列表框手动DRAWITEM大字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆