ListBox重绘

本文介绍了如何通过编程调整.NET Framework类库中的ListBox和ComboBox控件的文字显示样式与高度,包括自定义绘制模式、字体大小与高度的动态设定等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

.NET Framework 类库  ListBox.ItemHeight 属性

DrawMode 属性设置为 DrawMode.OwnerDrawFixed 时,所有项具有相同的高度。当 DrawMode 属性设置为 DrawMode.OwnerDrawVariable 时,ItemHeight 属性指定添加到 ListBox 中的每个项的高度。因为所有者描述的列表中的每个项可具有不同的高度,所以可使用 GetItemHeight 方法获取 ListBox 中特定项的高度。如果对具有可变高度的项的 ListBox 使用 ItemHeight 属性,则此属性返回控件中第一个项的高度。

ListBox 项的最大高度是 255 像素。

 

 

 

listbox每行的文字名称重绘

http://files.cnblogs.com/xe2011/CSharpListBoxDrawItem.rar

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //添加字体
            foreach (FontFamily fam in FontFamily.Families)
            {
                listBox1.Items.Add(fam.Name);
            }
            
            //OwnerDrawVariable
            listBox1.DrawMode = DrawMode.OwnerDrawVariable;
        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            //e.DrawFocusRectangle();
            e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), new Font(listBox1.Items[e.Index].ToString(), 12), Brushes.Black, e.Bounds);
        }

        private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            e.ItemHeight =22;
        }
    }
}
View Code

 

 

ListBoxbox1的每行的行高

http://files.cnblogs.com/xe2011/CSharpListBoxDrawItemHeight.rar

private void Form1_Load(object sender, EventArgs e)
{
    //OwnerDrawVariable
    listBox1.DrawMode = DrawMode.OwnerDrawVariable;
}

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    //e.DrawFocusRectangle();

    string s= listBox1.Items[e.Index].ToString();            
    int fontSize = Convert.ToInt32( listBox1.Items[e.Index].ToString() );
    Font font =new Font("Times New Roman", fontSize, FontStyle.Bold);

    e.Graphics.DrawString(s, font, Brushes.Black, e.Bounds);
}

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
    e.ItemHeight =  Convert.ToInt32(listBox1.Items[e.Index].ToString())+12;
}
View Code

 

Combobox的和ListBox的写法一样

Combobox 文字名称http://files.cnblogs.com/xe2011/CSharpComboboxDrawItem.rar

        private void Form1_Load(object sender, EventArgs e)
        {
            ////添加字体
            foreach (FontFamily f in FontFamily.Families)
            {
                comboBox1.Items.Add(f.Name);
            }
            
            //OwnerDrawVariable
            comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
            comboBox1.MaxDropDownItems = 15;
            comboBox1.DropDownWidth = 200;
        }

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            //e.DrawFocusRectangle();
            string s = comboBox1.Items[e.Index].ToString();
            string fontName = comboBox1.Items[e.Index].ToString();
            Font font = new Font(fontName, 12);
            e.Graphics.DrawString(s, font, Brushes.Black, e.Bounds);
        }

        private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            e.ItemHeight = 20;
        }
View Code

 

Combobox文字行高

 
附件http://files.cnblogs.com/xe2011/CSharpComboboxDrawItemHeight.rar

private void Form1_Load(object sender, EventArgs e)
{
    this.comboBox1.Items.AddRange(new string[] {
    "8",
    "9",
    "10",
    "11",
    "12",
    "14",
    "16",
    "18",
    "20",
    "22",
    "24",
    "26",
    "28",
    "36",
    "48",
    "72"});

    //OwnerDrawVariable
    comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
    comboBox1.MaxDropDownItems = 15;
    comboBox1.DropDownWidth = 200;

}

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{

    e.DrawBackground();
    //e.DrawFocusRectangle();

    string s = comboBox1.Items[e.Index].ToString();
    int fontSize = Convert.ToInt32(comboBox1.Items[e.Index].ToString());
    Font font = new Font("Times New Roman", fontSize, FontStyle.Bold);

    e.Graphics.DrawString(s, font, Brushes.Black, e.Bounds);
}

private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
    e.ItemHeight = Convert.ToInt32(comboBox1.Items[e.Index].ToString()) + 12;
}
View Code

 

 

 

 

转载于:https://www.cnblogs.com/xe2011/p/3466079.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值