我正在尝试为我可以在我的程序中使用的图像找到下拉式列表。它只需要在网格中显示一些图像,每个图像都有工具提示,我需要能够获得最后一个选择的图像。例如(没有标签栏):
不幸的是我的货币预算为零,这意味着我无法购买任何控件。有这样的免费的,还是我需要自己创作?
如果答案是后者,那么您是否可以给我任何有用的链接,以便我可以开始为此进行控制?
答案 0 :(得分:2)
有很多自定义图像下拉列表的实现。
您应该查看ComboBox
的{{3}}属性。
这是一个实现ImageList
使用的简单示例,继承自ComboBox
,将DrawMode
设置为OwnerDrawFixed
并在OnDrawItem
中绘制其元素}:
我不确定这种方法是否可以让您实现您提到的网格式图像列表,但请看一下示例 - 我确信它很适合您的需求。
答案 1 :(得分:1)
您可以从System.Windows.Forms.ComboBox
继承您的课程并覆盖受保护的方法OnDrawItem(DrawItemEventArgs e)
示例代码:
public class ImageComboBox : ComboBox
{
protected override void OnDrawItem(DrawItemEventArgs e)
{
// Get the item.
var item = this.Items[e.Index] as string;
if(item == null)
return;
// Get the coordinates to where to draw the image.
int imageX = e.Bounds.X + 5;
int imageY = (e.Bounds.Height - image.Height) / 2;
// Draw image
e.Graphics.DrawImage(image, new Point(imageX, imageY));
// Draw text
e.Graphics.DrawString(item, this.Font, new SolidBrush(Color.Black),
new PointF(textX, textY);
}
}
上面的代码只是一个快速而肮脏的例子,不应该按原样使用(例如,每次绘制项目时都不应该创建一个新的SolidBrush),但我希望它能让你了解如何这样做。