ListBox重绘

时间:2023-03-08 17:31:14
ListBox重绘

.NET Framework 类库  ListBox.ItemHeight 属性

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

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

ListBox重绘

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(), ), Brushes.Black, e.Bounds);
} private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight =;
}
}
}

ListBox重绘

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())+;
}

Combobox的和ListBox的写法一样

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

        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 = ;
comboBox1.DropDownWidth = ;
} 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, );
e.Graphics.DrawString(s, font, Brushes.Black, e.Bounds);
} private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = ;
}

Combobox文字行高

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

private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.Items.AddRange(new string[] {
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""}); //OwnerDrawVariable
comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
comboBox1.MaxDropDownItems = ;
comboBox1.DropDownWidth = ; } 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()) + ;
}