// This method initializes the owner-drawn combo box. // The drop-down width is set much wider than the size of the combo box // to accomodate the large items in the list. The drop-down style is set to // ComboBox.DropDown, which requires the user to click on the arrow to // see the list. private void InitializeComboBox() { this.ComboBox1 = new ComboBox(); this.ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; this.ComboBox1.Location = new System.Drawing.Point(10, 20); this.ComboBox1.Name = "ComboBox1"; this.ComboBox1.Size = new System.Drawing.Size(100, 120); this.ComboBox1.DropDownWidth = 250; this.ComboBox1.TabIndex = 0; this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown; animals = new string[]{"Elephant", "c r o c o d i l e", "lion"}; ComboBox1.DataSource = animals; this.Controls.Add(this.ComboBox1); // Hook up the MeasureItem and DrawItem events this.ComboBox1.DrawItem += new DrawItemEventHandler(ComboBox1_DrawItem); this.ComboBox1.MeasureItem += new MeasureItemEventHandler(ComboBox1_MeasureItem); }// If you set the Draw property to DrawMode.OwnerDrawVariable, // you must handle the MeasureItem event. This event handler // will set the height and width of each item before it is drawn. private void ComboBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { switch(e.Index) { case 0: e.ItemHeight = 45; break; case 1: e.ItemHeight = 20; break; case 2: e.ItemHeight = 35; break; } e.ItemWidth = 260;}// You must handle the DrawItem event for owner-drawn combo boxes. // This event handler changes the color, size and font of an // item based on its position in the array. private void ComboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { float size = 0; System.Drawing.Font myFont; FontFamily family = null; System.Drawing.Color animalColor = new System.Drawing.Color(); switch(e.Index) { case 0: size = 30; animalColor = System.Drawing.Color.Gray; family = FontFamily.GenericSansSerif; break; case 1: size = 10; animalColor = System.Drawing.Color.LawnGreen; family = FontFamily.GenericMonospace; break; case 2: size = 15; animalColor = System.Drawing.Color.Tan; family = FontFamily.GenericSansSerif; break; } // Draw the background of the item. e.DrawBackground(); // Create a square filled with the animals color. Vary the size // of the rectangle based on the length of the animals name. Rectangle rectangle = new Rectangle(2, e.Bounds.Top+2, e.Bounds.Height, e.Bounds.Height-4); e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle); // Draw each string in the array, using a different size, color, // and font for each item. myFont = new Font(family, size, FontStyle.Bold); e.Graphics.DrawString(animals[e.Index], myFont, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X+rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)); // Draw the focus rectangle if the mouse hovers over an item. e.DrawFocusRectangle(); }
按楼主的要求,大致应是这样的: private void initCom() { this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; this.comboBox1.FormattingEnabled = true; this.comboBox1.Items.AddRange(new object[] { "abcdefg", "bcdefgh", "cdefghi", "defghij", "efghijk", "fghijkl", "ghijklm"}); this.comboBox1.Location = new System.Drawing.Point(37, 100); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(121, 22); this.comboBox1.TabIndex = 5; this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem); this.comboBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.comboBox1_MeasureItem); } private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e) { e.ItemHeight = 20; e.ItemWidth = 100; } private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); e.DrawFocusRectangle(); using (SolidBrush brush = new SolidBrush(e.ForeColor)) { using (StringFormat format = new StringFormat()) { format.Alignment = StringAlignment.Near; format.LineAlignment = StringAlignment.Center; e.Graphics.DrawString(this.comboBox1.Items[e.Index].ToString(), this.comboBox1.Font, brush, e.Bounds, format); } } using (Pen pen = new Pen(e.ForeColor)) { e.Graphics.DrawRectangle(pen, e.Bounds); } }
感谢您的帮忙, 如果combobox是多列的情况,该如何处理呢? 我写的DrawItem()方法如下,请问该如何 改动, 谢谢。 protected override void OnDrawItem(DrawItemEventArgs e) { e.DrawBackground(); if (0 <= e.Index) { e.Graphics.DrawString(this.m_table.Rows[e.Index][0].ToString(), e.Font, new SolidBrush(e.ForeColor), (float)e.Bounds.X, (float)e.Bounds.Y); if (1 < this.m_table.Columns.Count) { int x = (base.DropDownWidth - base.Width) / (this.m_table.Columns.Count - 1); if (this.m_table.Columns.Count <= columnLimit) { for (int i = 1; i < this.m_table.Columns.Count; i++) { e.Graphics.DrawString(this.m_table.Rows[e.Index][i].ToString(), e.Font, new SolidBrush(e.ForeColor), (float)(base.Width + (x * (i - 1))), (float)e.Bounds.Y); } } else if (columnLimit < this.m_table.Columns.Count ) { for (int i = 1; i < columnLimit; i++) { e.Graphics.DrawString(this.m_table.Rows[e.Index][i].ToString(), e.Font, new SolidBrush(e.ForeColor), (float)(base.Width + (x * (i - 1))), (float)e.Bounds.Y); } } } } e.DrawFocusRectangle(); }
获取或设置一个值,该值指示是由您的代码还是由操作系统来处理列表中的元素的绘制。下面的代码示例演示如何创建所有者描述的组合框,具体方法为将 DrawMode 属性设置为 OwnerDrawnVariable,并处理 DrawItem 和 MeasureItem 事件。它还阐释了如何设置 DropDownWidth 和 DropDownStyle 属性。要运行该示例,请将以下代码粘贴到一个窗体中。在该窗体的构造函数或 Load 事件中调用 InitializeComboBox 方法。internal System.Windows.Forms.ComboBox ComboBox1;
private string[] animals;
// This method initializes the owner-drawn combo box.
// The drop-down width is set much wider than the size of the combo box
// to accomodate the large items in the list. The drop-down style is set to
// ComboBox.DropDown, which requires the user to click on the arrow to
// see the list.
private void InitializeComboBox()
{
this.ComboBox1 = new ComboBox();
this.ComboBox1.DrawMode =
System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.ComboBox1.Location = new System.Drawing.Point(10, 20);
this.ComboBox1.Name = "ComboBox1";
this.ComboBox1.Size = new System.Drawing.Size(100, 120);
this.ComboBox1.DropDownWidth = 250;
this.ComboBox1.TabIndex = 0;
this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown;
animals = new string[]{"Elephant", "c r o c o d i l e", "lion"};
ComboBox1.DataSource = animals;
this.Controls.Add(this.ComboBox1); // Hook up the MeasureItem and DrawItem events
this.ComboBox1.DrawItem +=
new DrawItemEventHandler(ComboBox1_DrawItem);
this.ComboBox1.MeasureItem +=
new MeasureItemEventHandler(ComboBox1_MeasureItem);
}// If you set the Draw property to DrawMode.OwnerDrawVariable,
// you must handle the MeasureItem event. This event handler
// will set the height and width of each item before it is drawn.
private void ComboBox1_MeasureItem(object sender,
System.Windows.Forms.MeasureItemEventArgs e)
{ switch(e.Index)
{
case 0:
e.ItemHeight = 45;
break;
case 1:
e.ItemHeight = 20;
break;
case 2:
e.ItemHeight = 35;
break;
}
e.ItemWidth = 260;}// You must handle the DrawItem event for owner-drawn combo boxes.
// This event handler changes the color, size and font of an
// item based on its position in the array.
private void ComboBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{ float size = 0;
System.Drawing.Font myFont;
FontFamily family = null; System.Drawing.Color animalColor = new System.Drawing.Color();
switch(e.Index)
{
case 0:
size = 30;
animalColor = System.Drawing.Color.Gray;
family = FontFamily.GenericSansSerif;
break;
case 1:
size = 10;
animalColor = System.Drawing.Color.LawnGreen;
family = FontFamily.GenericMonospace;
break;
case 2:
size = 15;
animalColor = System.Drawing.Color.Tan;
family = FontFamily.GenericSansSerif;
break;
} // Draw the background of the item.
e.DrawBackground(); // Create a square filled with the animals color. Vary the size
// of the rectangle based on the length of the animals name.
Rectangle rectangle = new Rectangle(2, e.Bounds.Top+2,
e.Bounds.Height, e.Bounds.Height-4);
e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle); // Draw each string in the array, using a different size, color,
// and font for each item.
myFont = new Font(family, size, FontStyle.Bold);
e.Graphics.DrawString(animals[e.Index], myFont, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X+rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)); // Draw the focus rectangle if the mouse hovers over an item.
e.DrawFocusRectangle();
}
private void initCom()
{
this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"abcdefg",
"bcdefgh",
"cdefghi",
"defghij",
"efghijk",
"fghijkl",
"ghijklm"});
this.comboBox1.Location = new System.Drawing.Point(37, 100);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 22);
this.comboBox1.TabIndex = 5;
this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
this.comboBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.comboBox1_MeasureItem); }
private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 20;
e.ItemWidth = 100;
} private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
using (SolidBrush brush = new SolidBrush(e.ForeColor))
{
using (StringFormat format = new StringFormat())
{
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(this.comboBox1.Items[e.Index].ToString(), this.comboBox1.Font, brush, e.Bounds, format);
}
}
using (Pen pen = new Pen(e.ForeColor))
{
e.Graphics.DrawRectangle(pen, e.Bounds);
}
}
如果combobox是多列的情况,该如何处理呢?
我写的DrawItem()方法如下,请问该如何 改动, 谢谢。
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
if (0 <= e.Index)
{
e.Graphics.DrawString(this.m_table.Rows[e.Index][0].ToString(), e.Font, new SolidBrush(e.ForeColor), (float)e.Bounds.X, (float)e.Bounds.Y);
if (1 < this.m_table.Columns.Count)
{
int x = (base.DropDownWidth - base.Width) / (this.m_table.Columns.Count - 1);
if (this.m_table.Columns.Count <= columnLimit)
{
for (int i = 1; i < this.m_table.Columns.Count; i++)
{
e.Graphics.DrawString(this.m_table.Rows[e.Index][i].ToString(), e.Font, new SolidBrush(e.ForeColor), (float)(base.Width + (x * (i - 1))), (float)e.Bounds.Y);
}
}
else if (columnLimit < this.m_table.Columns.Count )
{
for (int i = 1; i < columnLimit; i++)
{
e.Graphics.DrawString(this.m_table.Rows[e.Index][i].ToString(), e.Font, new SolidBrush(e.ForeColor), (float)(base.Width + (x * (i - 1))), (float)e.Bounds.Y);
}
}
}
} e.DrawFocusRectangle();
}
仿照您的代码,我实现了 行与行 之间的分割线。
请问 列于列 之间的分割线 该如何添加?