【问题标题】:Changing colours using a combo box in Windows Form App使用 Windows 窗体应用程序中的组合框更改颜色
【发布时间】:2018-02-26 19:19:01
【问题描述】:
protected override void OnPaint(PaintEventArgs e)
{
    if (comboBox1.Text == "Circle")
    {
        e.Graphics.FillEllipse(Brushes.Red, new Rectangle(105, 120, 64, 64));
    }

    if (comboBox1.Text == "Rectangle")
    {
        e.Graphics.FillRectangle(Brushes.Red, new Rectangle(105, 120, 75, 50));
    }

    if(comboBox1.Text == "Triangle")
    {
        Point[] points = { new Point(140, 110), new Point(230, 190), new Point(50, 190) };
        e.Graphics.FillPolygon(Brushes.Red, points);
    }
}

我已经在上面创建了这些形状,它们位于组合框中,但我正在尝试获取它,所以我的第二个组合框会改变形状的颜色,有什么办法吗?我似乎无法在任何地方找到它。 第二个组合框包含红色、绿色、蓝色、自定义颜色。我有它,所以调色板出现了,但我也不知道如何在选择时将其设置为形状。

我将如何参考形状并更改画笔颜色是我正在努力解决的部分

【问题讨论】:

  • 您只需要通过迭代 System.Drawing.Colors 来填充第二个组合框,然后在 OnPaint() 中使用所选颜色
  • 请使用标签。许多 UI 都有组合框:WPF/WebForms/WinForms/XamarinForms/WinRT/等。
  • 如何将其分配给我的形状?
  • 除combobox1外,您还需要检查combobox2中选择的内容,并在此基础上应用适当的画笔
  • 你有这方面的代码示例吗? @ChetanRanpariya

标签: c# winforms


【解决方案1】:

可能已经过时,但只是为了简化您的代码并使其更清晰,您可以使用 switch 语句

 if ((comboBox1.Text != defaultTextShape) && (comboBox2.Text != defaultTextColour))
        {
            Brush b;
            switch (comboBox1.Text)
            {
                case "Red"   : b = Brushes.Red; break;
                case "Green" : b = Brushes.Green; break;
                case "Blue"  : b = Brushes.Blue; break;
                default      : b = new SolidBrush(CustomColour); break;
            }

            switch (comboBox2.Text)
            {
                case "Rectangle": e.Graphics.FillRectangle(b, new Rectangle(105, 120, 75, 50)); break;
                case "Circle"   : e.Graphics.FillEllipse(b, new Rectangle(105, 120, 64, 64)); break;                    
                default:
                    Point[] points = { new Point(140, 110), new Point(230, 190), new Point(50, 190) };
                    e.Graphics.FillPolygon(b, points); break;
            }


        }

自定义颜色是一个公共常量,它将您的自定义颜色选择器值分配给它,然后将其分配给 B

【讨论】:

    【解决方案2】:

    正如 Hans Passant 已经指出的:您需要使用 SolidBrush 对象来设置自定义颜色。这在帖子convert from Color to brush 中有描述。

    在您的情况下,我将按以下方式填充颜色 ComboBox

    comboBox_Colour.DataSource = new List<Color> { Color.Red, Color.Blue, Color.Yellow, Color.Green };
    

    现在在OnPaint 事件中,您可以检查是否已经选择了任何项目,然后直接从comboBox_ColourSelectedItem 中获取Color。然后将其插入SolidBrush 对象的构造函数并将SolidBrush 提供给Fill... 方法。它将起作用,因为 SolidBrush 继承自 Brush

    public sealed class SolidBrush : Brush

    protected override void OnPaint(PaintEventArgs e)
    {
        if (comboBox_Shape.SelectedIndex != -1 && comboBox_Colour.SelectedIndex != -1)
        {
            Color c = (Color)comboBox_Colour.SelectedItem;
            SolidBrush sb = new SolidBrush(c);
            if (comboBox_Shape.Text == "Circle")
            {
                e.Graphics.FillEllipse(sb, new Rectangle(105, 120, 64, 64));
            }
            else if (comboBox_Shape.Text == "Rectangle")
            {
                e.Graphics.FillRectangle(sb, new Rectangle(105, 120, 75, 50));
            }
            else if (comboBox_Shape.Text == "Triangle")
            {
                Point[] points = { new Point(140, 110), new Point(230, 190), new Point(50, 190) };
                e.Graphics.FillPolygon(sb, points);
            }
        }
    }
    

    编辑:

    如果我在此处提供的颜色选择不足以满足您的需求,您可以将结构 Color 必须提供的所有颜色转储到 comboBox_Colour

    List<Color> allColours = typeof(Color).GetProperties(
                         System.Reflection.BindingFlags.Static | 
                         System.Reflection.BindingFlags.Public)
                        .Select(x => (Color)x.GetValue(null)).ToList();
    
    comboBox_Colour.DataSource = allColours;
    

    【讨论】:

    • @Twyxz 很高兴听到。不客气。这是一个有趣的问题要解决:)
    • 是否可以从调色板中选择颜色?而不是颜色列表@MongZhu
    • @Twyxz 一切皆有可能;) 你需要 ColorDialog 类。试试看。 Tt 不是很复杂。如果您有困难,那么您可以提出一个新问题并再次戳我;)我会回答它祝你好运
    猜你喜欢
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多