下拉图像列表 [英] Drop-Down Image List
问题描述
我试图找到一个下拉式列表中的图像,我可以在我的程序中使用。它只需在工具提示每一个网格中显示一些图片,我需要能够获得最后挑哪一个了。例如(没有在它的标签栏)的:
I'm trying to find a dropdown-style list for images I can use in a program of mine. It just needs to display a few images in a grid with tooltips for each one, and I need to be able to get whichever one was last picked. For example (without the tab bar in it):
不幸的是我的货币预算是零,这意味着我不能购买任何控制。有没有免费的这个样子,或者我需要工作在做我自己?
Unfortunately my monetary budget is zero which means I can't purchase any controls. Are there any free ones like this, or will I need to work at making my own?
如果答案是后者,在那里,你也许能够给我这样的任何有用的链接,我可以开始工作,这个控制?
If the answer is the latter, are there any useful links you might be able to give me so I could start to work on a control for this?
推荐答案
可以从 System.Windows.Forms.ComboBox
继承你的类并覆盖保护的方法的OnDrawItem(DrawItemEventArgs E)
You can inherit your class from System.Windows.Forms.ComboBox
and override the protected method OnDrawItem(DrawItemEventArgs e)
示例code:
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);
}
}
在code以上仅仅是一个快速和肮脏的例子,不应该使用,因为它是(应该不是例如创建一个新SolidBrush每个项目绘制时间),但我希望它会给你一个想法如何做到这一点。
The code above is only a quick and dirty example, and should not be used as it is (should not for example create a new SolidBrush each time the item is drawn), but I hope it will give you an idea of how to do it.
这篇关于下拉图像列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!