.NET Framework 类库  ListBox.ItemHeight 属性

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

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

 

 

ListBox重绘

 

listbox每行的文字名称重绘

https://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

相关文章: