【问题标题】:Drop-Down Image List下拉图像列表
【发布时间】:2013-05-23 17:00:40
【问题描述】:

我正在尝试为可以在我的程序中使用的图像找到一个下拉式列表。它只需要在网格中显示一些图像,每个图像都有工具提示,我需要能够获得最后选择的任何一个。例如(没有标签栏):

很遗憾,我的货币预算为零,这意味着我无法购买任何控件。有没有像这样免费的,还是我需要自己制作?

如果答案是后者,您是否可以给我任何有用的链接,以便我可以开始为此进行控制?

【问题讨论】:

  • 题外话:改用wpf会大大缓解问题

标签: .net vb.net winforms


【解决方案1】:

那里有很多自定义图像下拉列表的实现。

您应该查看ComboBox 的DrawMode 属性。

这是一个实现ImageList 使用的简单示例,通过从ComboBox 继承,将DrawMode 设置为OwnerDrawFixed 并在OnDrawItem 中绘制其元素:

ComboBox With Images

我不确定这种方法是否能让您实现您提到的网格样式图像列表,但请看一下示例 - 我确信它非常适合您的需求。

【讨论】:

    【解决方案2】:

    您可以从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),但我希望它能让您了解如何去做吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 2018-07-30
      • 2019-11-30
      • 1970-01-01
      • 2017-09-14
      相关资源
      最近更新 更多