如何为 window 表单工具条创建带有按钮控件的组合框(下拉列表)???
How to create a combo box(drop down list) with button controls for window form tool strip???
类似于 图层控件 AutoCAD 中的下拉菜单。
谢谢。
我自己解决了...
我创建了一个继承组合框的用户控件 class,这个控件对我来说就像普通的组合框一样工作正常。
这是一段代码,可以帮助您更深入地了解这个控件。
public 部分 class 下拉列表:ComboBox
{
public dropdown()
{
this.DrawMode = DrawMode.OwnerDrawVariable;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
// Make sure we're not trying to draw something that isn't there.
if (e.Index >= this.Items.Count || e.Index <= -1)
return;
// Get the item object.
object item = this.Items[e.Index];
if (item == null)
return;
// Draw the background color depending on
// if the item is selected or not.
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
// The item is selected.
// We want a blue background color.
e.Graphics.FillRectangle(new SolidBrush(Color.Moccasin), e.Bounds);
}
else
{
// The item is NOT selected.
// We want a white background color.
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
}
if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit)
{
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
}
// Draw the items...
LayerEdit le = (LayerEdit)item;
string text = le.LayerName.ToString();
SizeF stringSize = e.Graphics.MeasureString(text, this.Font);
// Draw layer visible(on/off)...
e.Graphics.DrawImage(byteArrayToImage(le.Visible), 2, 1 + e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2, 14, 14);
// Draw layer Lock...
e.Graphics.DrawImage(byteArrayToImage(le.Lock), 18, 1 + e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2, 15, 14);
// Draw layer plot...
e.Graphics.DrawImage(byteArrayToImage(le.Plot), 37, 1 + e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2, 14, 14);
// Draw layer color...
Brush brush = new SolidBrush(ColorTranslator.FromHtml(le.ColourValue));
e.Graphics.FillRectangle(brush, 55, 4 + e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2, 10, 10);
e.Graphics.DrawRectangle(Pens.Black, 55, 4 + e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2, 10, 10);
// Draw layer name...
e.Graphics.DrawString(text, this.Font, new SolidBrush(Color.Black), new PointF(68, e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2));
}
//convert bytes into image...
public Image byteArrayToImage(byte[] byteArrayIn)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
}
类似于 图层控件 AutoCAD 中的下拉菜单。
谢谢。
我自己解决了...
我创建了一个继承组合框的用户控件 class,这个控件对我来说就像普通的组合框一样工作正常。
这是一段代码,可以帮助您更深入地了解这个控件。
public 部分 class 下拉列表:ComboBox
{
public dropdown()
{
this.DrawMode = DrawMode.OwnerDrawVariable;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
// Make sure we're not trying to draw something that isn't there.
if (e.Index >= this.Items.Count || e.Index <= -1)
return;
// Get the item object.
object item = this.Items[e.Index];
if (item == null)
return;
// Draw the background color depending on
// if the item is selected or not.
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
// The item is selected.
// We want a blue background color.
e.Graphics.FillRectangle(new SolidBrush(Color.Moccasin), e.Bounds);
}
else
{
// The item is NOT selected.
// We want a white background color.
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
}
if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit)
{
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
}
// Draw the items...
LayerEdit le = (LayerEdit)item;
string text = le.LayerName.ToString();
SizeF stringSize = e.Graphics.MeasureString(text, this.Font);
// Draw layer visible(on/off)...
e.Graphics.DrawImage(byteArrayToImage(le.Visible), 2, 1 + e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2, 14, 14);
// Draw layer Lock...
e.Graphics.DrawImage(byteArrayToImage(le.Lock), 18, 1 + e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2, 15, 14);
// Draw layer plot...
e.Graphics.DrawImage(byteArrayToImage(le.Plot), 37, 1 + e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2, 14, 14);
// Draw layer color...
Brush brush = new SolidBrush(ColorTranslator.FromHtml(le.ColourValue));
e.Graphics.FillRectangle(brush, 55, 4 + e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2, 10, 10);
e.Graphics.DrawRectangle(Pens.Black, 55, 4 + e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2, 10, 10);
// Draw layer name...
e.Graphics.DrawString(text, this.Font, new SolidBrush(Color.Black), new PointF(68, e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2));
}
//convert bytes into image...
public Image byteArrayToImage(byte[] byteArrayIn)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
}