使用 C# 组合框放置图像和字符串

发布于 2024-09-30 15:39:48 字数 158 浏览 1 评论 0原文

我需要为 Windows 窗体应用程序创建一个下拉菜单或组合框,其中包含一个小图像,然后是它旁边的一串文本。基本上,您可以将下拉列表中的每个“行”视为需要有一个图标,然后在图标右侧显示图标的名称。我在做这件事时遇到了困难——事实上,我完全没有成功。有谁知道完成这项任务的方法?任何帮助将不胜感激。谢谢!

I need to create a dropdown menu, or combobox, for a Windows Forms application which contains a small image and then a string of text next to it. Basically, you can think of each 'row' in the dropdown as needing to have an icon and then the name of the icon to the right of the icon. I am having trouble doing this -- in fact, I've been completely unsuccessful. Does anyone know of a way to accomplish this task? Any help will be greatly appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡写薰衣草的香 2024-10-07 15:39:48

我能够想出一些非常简单的代码来执行此操作(请参阅下面的代码片段)。该代码创建一个下拉控件,它在同一行中显示一个小彩色方块和该颜色的名称(参见照片)。感谢您在最初发布时提供的链接!希望这个控件将来可以帮助其他人。

图片:

下拉颜色选择器


代码:

class ColorSelector : ComboBox
{
    public ColorSelector()
    {
        DrawMode = DrawMode.OwnerDrawFixed;
        DropDownStyle = ComboBoxStyle.DropDownList;
    }
 
    // Draws the items into the ColorSelector object
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        e.DrawBackground();
        e.DrawFocusRectangle();
 
        DropDownItem item = new DropDownItem(Items[e.Index].ToString());
        // Draw the colored 16 x 16 square
        e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);
        // Draw the value (in this case, the color name)
        e.Graphics.DrawString(item.Value, e.Font, new
                SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);
 
        base.OnDrawItem(e);
    }
}
 
public class DropDownItem
{
    public string Value
    {
        get { return value; }
        set { this.value = value; }
    }
    private string value;
 
    public Image Image
    {
        get { return img; }
        set { img = value; }
    }
    private Image img;
 
    public DropDownItem() : this("")
    {}
 
    public DropDownItem(string val)
    {
        value = val;
        this.img = new Bitmap(16, 16);
        Graphics g = Graphics.FromImage(img);
        Brush b = new SolidBrush(Color.FromName(val));
        g.DrawRectangle(Pens.White, 0, 0, img.Width, img.Height);
        g.FillRectangle(b, 1, 1, img.Width - 1, img.Height - 1);
    }
 
    public override string ToString()
    {
        return value;
    }
}

I was able to come up with some very simple code to perform this (see snippet below). The code creates a control that is a dropdown control which shows a small colored square and that color's name in the same row (see photo). Thanks for the links provided for this back when it was originally posted! Hopefully this control can help someone else out in the future.

Image:

Drop Down Color Selector


Code:

class ColorSelector : ComboBox
{
    public ColorSelector()
    {
        DrawMode = DrawMode.OwnerDrawFixed;
        DropDownStyle = ComboBoxStyle.DropDownList;
    }
 
    // Draws the items into the ColorSelector object
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        e.DrawBackground();
        e.DrawFocusRectangle();
 
        DropDownItem item = new DropDownItem(Items[e.Index].ToString());
        // Draw the colored 16 x 16 square
        e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);
        // Draw the value (in this case, the color name)
        e.Graphics.DrawString(item.Value, e.Font, new
                SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);
 
        base.OnDrawItem(e);
    }
}
 
public class DropDownItem
{
    public string Value
    {
        get { return value; }
        set { this.value = value; }
    }
    private string value;
 
    public Image Image
    {
        get { return img; }
        set { img = value; }
    }
    private Image img;
 
    public DropDownItem() : this("")
    {}
 
    public DropDownItem(string val)
    {
        value = val;
        this.img = new Bitmap(16, 16);
        Graphics g = Graphics.FromImage(img);
        Brush b = new SolidBrush(Color.FromName(val));
        g.DrawRectangle(Pens.White, 0, 0, img.Width, img.Height);
        g.FillRectangle(b, 1, 1, img.Width - 1, img.Height - 1);
    }
 
    public override string ToString()
    {
        return value;
    }
}
何其悲哀 2024-10-07 15:39:48

非常有帮助..
一些优化:

public sealed class ColorSelector : ComboBox
{
    public ColorSelector()
    {
        DrawMode = DrawMode.OwnerDrawFixed;
        DropDownStyle = ComboBoxStyle.DropDownList;
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        e.DrawBackground();

        e.DrawFocusRectangle();

        if (e.Index >= 0 && e.Index < Items.Count)
        {
            DropDownItem item = (DropDownItem)Items[e.Index];

            e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);

            e.Graphics.DrawString(item.Value, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);
        }

        base.OnDrawItem(e);
    }
}

和...

public sealed class DropDownItem
{
    public string Value { get; set; }

    public Image Image { get; set; }

    public DropDownItem()
        : this("")
    { }

    public DropDownItem(string val)
    {
        Value = val;
        Image = new Bitmap(16, 16);
        using (Graphics g = Graphics.FromImage(Image))
        {
            using (Brush b = new SolidBrush(Color.FromName(val)))
            {
                g.DrawRectangle(Pens.White, 0, 0, Image.Width, Image.Height);
                g.FillRectangle(b, 1, 1, Image.Width - 1, Image.Height - 1);
            }
        }
    }

    public override string ToString()
    {
        return Value;
    }
}

Very helpful..
some optimisations :

public sealed class ColorSelector : ComboBox
{
    public ColorSelector()
    {
        DrawMode = DrawMode.OwnerDrawFixed;
        DropDownStyle = ComboBoxStyle.DropDownList;
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        e.DrawBackground();

        e.DrawFocusRectangle();

        if (e.Index >= 0 && e.Index < Items.Count)
        {
            DropDownItem item = (DropDownItem)Items[e.Index];

            e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);

            e.Graphics.DrawString(item.Value, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);
        }

        base.OnDrawItem(e);
    }
}

and ...

public sealed class DropDownItem
{
    public string Value { get; set; }

    public Image Image { get; set; }

    public DropDownItem()
        : this("")
    { }

    public DropDownItem(string val)
    {
        Value = val;
        Image = new Bitmap(16, 16);
        using (Graphics g = Graphics.FromImage(Image))
        {
            using (Brush b = new SolidBrush(Color.FromName(val)))
            {
                g.DrawRectangle(Pens.White, 0, 0, Image.Width, Image.Height);
                g.FillRectangle(b, 1, 1, Image.Width - 1, Image.Height - 1);
            }
        }
    }

    public override string ToString()
    {
        return Value;
    }
}
只为守护你 2024-10-07 15:39:48

我解决了问题,我这样做了:

ComboBox MarcadorNS = new ComboBox();
MarcadorNS.Height = 30;
MarcadorNS.Width = 150;
MarcadorNS.SelectedValuePath = "Uid";
foreach (var temporalItem in GetPredefinedKinds())
{
    Image ImagenCombo = new Image();
    ImagenCombo.Source =
    new BitmapImage(new Uri(
       "Imagenes/Marcadores/" +
        temporalItem.Name.ToLower() + ".png", UriKind.Absolute));
    ImagenCombo.Height = 28;
    ImagenCombo.Width = 28;
    ImagenCombo.VerticalAlignment = VerticalAlignment.Top;
    ImagenCombo.HorizontalAlignment = HorizontalAlignment.Left;
    Label textoCombo = new Label();
    textoCombo.VerticalAlignment = VerticalAlignment.Top;
    textoCombo.HorizontalAlignment = HorizontalAlignment.Left;
    textoCombo.Content = BaseDatos.NombresDeMarcadores(temporalItem.ToString());
    Grid GridCombo = new Grid();
    GridCombo.Uid = ObtenerMarcador(temporalItem.ToString());
    StackPanel stackCombo = new StackPanel();
    stackCombo.Orientation = Orientation.Horizontal;
    stackCombo.Children.Add(ImagenCombo);
    stackCombo.Children.Add(textoCombo);
    GridCombo.Children.Add(stackCombo);
    MarcadorNS.Items.Add(GridCombo);
}

在此处输入图像描述

I resolved the problem, i did this:

ComboBox MarcadorNS = new ComboBox();
MarcadorNS.Height = 30;
MarcadorNS.Width = 150;
MarcadorNS.SelectedValuePath = "Uid";
foreach (var temporalItem in GetPredefinedKinds())
{
    Image ImagenCombo = new Image();
    ImagenCombo.Source =
    new BitmapImage(new Uri(
       "Imagenes/Marcadores/" +
        temporalItem.Name.ToLower() + ".png", UriKind.Absolute));
    ImagenCombo.Height = 28;
    ImagenCombo.Width = 28;
    ImagenCombo.VerticalAlignment = VerticalAlignment.Top;
    ImagenCombo.HorizontalAlignment = HorizontalAlignment.Left;
    Label textoCombo = new Label();
    textoCombo.VerticalAlignment = VerticalAlignment.Top;
    textoCombo.HorizontalAlignment = HorizontalAlignment.Left;
    textoCombo.Content = BaseDatos.NombresDeMarcadores(temporalItem.ToString());
    Grid GridCombo = new Grid();
    GridCombo.Uid = ObtenerMarcador(temporalItem.ToString());
    StackPanel stackCombo = new StackPanel();
    stackCombo.Orientation = Orientation.Horizontal;
    stackCombo.Children.Add(ImagenCombo);
    stackCombo.Children.Add(textoCombo);
    GridCombo.Children.Add(stackCombo);
    MarcadorNS.Items.Add(GridCombo);
}

enter image description here

靖瑶 2024-10-07 15:39:48

注意:此代码来自用户 que dal 的优化
如果您希望拥有一个不仅仅是颜色名称的字符串,请将 DropDownItem 更改为具有 2 个参数:字符串和颜色,然后只需更改画笔设置颜色的方式,如下所示:

    public DropDownItem(string val, Color color)
    {
        Value = val;
        Image = new Bitmap(16, 16);
        using (Graphics g = Graphics.FromImage(Image))
        {
            using (Brush b = new SolidBrush(color))
            {
                g.DrawRectangle(Pens.White, 0, 0, Image.Width, Image.Height);
                g.FillRectangle(b, 1, 1, Image.Width - 1, Image.Height - 1);
            }
        }
    }

然后您必须更改下拉列表项目如下:

    public DropDownItem()
        : this("", Color.Empty)
    {}

希望这有帮助:)

NOTE: This code is from user que dal's optimization
If you wish to have a string that isn't just the name of the color, change DropDownItem to have 2 arguments, the string and the color, then just change how the brush sets the color, as such:

    public DropDownItem(string val, Color color)
    {
        Value = val;
        Image = new Bitmap(16, 16);
        using (Graphics g = Graphics.FromImage(Image))
        {
            using (Brush b = new SolidBrush(color))
            {
                g.DrawRectangle(Pens.White, 0, 0, Image.Width, Image.Height);
                g.FillRectangle(b, 1, 1, Image.Width - 1, Image.Height - 1);
            }
        }
    }

You must then change the dropdown item as such:

    public DropDownItem()
        : this("", Color.Empty)
    {}

Hope this was helpful :)

枯叶蝶 2024-10-07 15:39:48

不确定图像,但这应该适用于字符串:

comboBox.Items.Add("String");

Not sure about images but this should work for the strings:

comboBox.Items.Add("String");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文