如何在 5 分钟内显示 ComboBox 中的图像?






4.86/5 (19投票s)
这是一个扩展的 ComboBox,
引言
这是一个简单的 ComboBox
,可以向用户显示图像。我看到了许多其他的解决方案,其中大多数都很复杂,并且重写了大多数 ComboBox
类属性和绘制方法,这可能会导致很多问题。
这是一种在 ComboBox
中添加图像的非常简单的方法。
图 1
如何使用?
将*ImagedComoBox.exe*从项目路径*bin\Release*复制到您的项目目录。 在 Visual Studio 中,打开“解决方案资源管理器”,右键单击项目名称,然后选择“添加引用”。 单击“浏览”按钮,找到*ImagedComoBox.exe*并选择它。 要在工具箱中显示控件,请右键单击工具箱,然后单击“添加/移除项”。 在“自定义工具箱”窗口中,选择*ImagedComoBox.exe*。
从工具箱中拖动 ImagedComboBox
并将其放在您的窗体中,如图 2 所示。
图 2. 拖放控件
Using the Code
构造函数
- 将
ComboBoxStyle
设置为DropDownList
。 您可以根据需要更改它。 - 设置所有者绘制模式
- 订阅
DrawItem
事件
public ImagedComboBox()
{
//Specifies that the list is displayed by clicking the down arrow
//and that the text portion is not editable.
//This means that the user cannot enter a new value.
//Only values already in the list can be selected. The list displays only if
DropDownStyle = ComboBoxStyle.DropDownList;
//All the elements in the control are drawn manually and can differ in size.
DrawMode = DrawMode.OwnerDrawVariable;
//using DrawItem event we need to draw item
DrawItem += ComboBoxDrawItemEvent;
}
在 ComboBoxDrawItemEvent
中:我绘制项目内容和图像。
我在 Overload:System.Windows.Forms.DrawItemEventArgs
中指定的边界内绘制项目背景。
e.DrawBackground();
我在指定位置和指定大小处绘制项目图像。
e.Graphics.DrawImage(comboboxItem.Image, e.Bounds.X,
e.Bounds.Y, ItemHeight, ItemHeight);
参数
comboBox.image
:项目图像,随项目一起给出。e.BoundsX
:要绘制的项目图像的左上角的 X 坐标。e.Bounds.Y
:要绘制的项目图像的左上角的 Y 坐标。ItemHeight
:要绘制的项目图像的宽度。ItemHeight
:要绘制的项目图像的高度。
如果您只想使用该属性,也可以将图像缩放到矩形。destRect
:System.Drawing.Rectangle
结构,用于指定绘制图像的位置和大小。
之后,我使用指定的 System.Drawing.Brush
和 System.Drawing.Font
对象在计算出的矩形中绘制项目文本字符串。
//we need to draw the item as string because we made drawmode to ownervariable
e.Graphics.DrawString(Items[e.Index].Value.ToString(), Font, Brushes.Black,
new RectangleF(e.Bounds.X + ItemHeight, e.Bounds.Y, DropDownWidth, ItemHeight));
参数
Items[e.Index]
:选定的项目font
:System.Drawing.Font
,用于定义string
的文本格式brush
:System.Drawing.Brush
,用于确定绘制的文本的颜色和纹理layoutRectangle
:System.Drawing.RectangleF
结构,用于指定绘制的位置
layoutRectangle
的大小是
x = ComboBox top left x.
x= ComboBox top left y.
Width = Iterate through all items in Combobox and get MaxWidth.
Height = item height.
/// </summary>
/// Draws overridden items.
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxDrawItemEvent(object sender, DrawItemEventArgs e)
{
//Draw background of the item e.DrawBackground();
if (e.Index != -1)
{
// selected item
var comboboxItem = Items[e.Index];
//Draw the image in combo box using its bound and ItemHeight
e.Graphics.DrawImage(comboboxItem.Image,
e.Bounds.X, e.Bounds.Y, ItemHeight, ItemHeight);
//we need to draw the item as string because we made drawmode to ownervariable
e.Graphics.DrawString(Items[e.Index].Value.ToString(), Font,
Brushes.Black, new RectangleF(e.Bounds.X + ItemHeight, e.Bounds.Y,
DropDownWidth, temHeight));
}
//draw rectangle over the item selected
e.DrawFocusRectangle();
}
ComboBox_MeasureItem
方法测量 item.ToString()
的宽度,该宽度对于将项目文本放入绘制矩形内非常重要。
它是如何工作的?
- 将
MaxWidth
初始值设置为ImagedCombobox.Width
- 迭代
ImagedCombobox
中的所有项目以查找新的最大宽度 - 检查
item.ToString()
测量的宽度是否大于maxWidth
g.MeasureString(item.ToString(), Font).width).Where(width=> width> maxWidth))
- 如果更大,则将
ComboBoxItem.Width
设置为maxWidth
并添加偏移量maxWidth = width; DropDownWidth = maxWidth + 20;
参数
item.ToString()
:要测量的字符串 (ComboBoxItem
)font
:System.Drawing.Font
,用于定义string
的文本格式width
:字符串的最大width
/// <summary>
/// I have set the Draw property to DrawMode.OwnerDrawVariable,
/// so I must calculate the item measurement.
/// I will set the height and width of each item before it is drawn.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
var g = CreateGraphics();
var maxWidth = 0;
foreach (var width in Items.ItemsBase.Cast<object>().Select(element =>
(int)g.MeasureString(element.ToString(), Font).Width).Where(width => width > maxWidth))
{
maxWidth = width;
}
DropDownWidth = maxWidth + 20;
}
重要类
ComboBoxItem
:此类表示ImagedComboBox
项,该项可能包含图像和值。ComboCollection
:ComboBoxItem
的集合
历史
- 2010 年 9 月 1 日:首次发布