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

wkftcu5l  于 2022-11-17  发布在  C#
关注(0)|答案(5)|浏览(143)

我需要为一个Windows窗体应用程序创建一个下拉菜单或组合框,其中包含一个小图像,然后在图像旁边包含一个文本字符串。基本上,您可以将下拉菜单中的每一“行”看作需要一个图标,然后在图标右侧显示图标的名称。我在这样做时遇到了麻烦--事实上,我完全失败了。有人知道完成这个任务的方法吗?任何帮助都将不胜感激。谢谢!

az31mfrm

az31mfrm1#

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

图像:

代码:

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;
    }
}
ut6juiuv

ut6juiuv2#

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

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;
    }
}
xesrikrc

xesrikrc3#

  • 注意:此代码来自用户que dal的优化 * 如果您希望字符串不仅仅是颜色的名称,请将DropDownItem更改为具有两个参数,字符串和颜色,然后只更改画笔设置颜色的方式,如下所示:
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)
    {}

希望这对你有帮助:)

sqyvllje

sqyvllje4#

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

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);
}

3mpgtkmj

3mpgtkmj5#

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

comboBox.Items.Add("String");

相关问题