简体   繁体   English

如何在c#中更改笔的颜色?

[英]How do I change the color of the pen in c#?

I am trying to change the color of my pen and solid brush.我正在尝试更改钢笔和实心画笔的颜色。

I hard coded them, but now I would like to know how to make only one pen and change the color to any color I want using a GUI (Drop down list, color palette, etc).我对它们进行了硬编码,但现在我想知道如何只制作一支笔并使用 GUI(下拉列表、调色板等)将颜色更改为我想要的任何颜色。

Here is my coded:这是我的编码:

    private void imageComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        // create graphics object, Pen and SolidBrush
        Graphics myGraphics = base.CreateGraphics();

        // create Pen using different colors
        Pen myPen = new Pen(Color.DarkRed);
        Pen myPen2 = new Pen(Color.Blue);
        Pen myPen3 = new Pen(Color.Purple);
        Pen myPen4 = new Pen(Color.DarkGreen);

        // create SolidBrush using color DarkRed
        SolidBrush mySolidBrush = new SolidBrush(Color.DarkRed);
        SolidBrush mySolidBrush2 = new SolidBrush(Color.Orange);
        SolidBrush mySolidBrush3 = new SolidBrush(Color.Blue);
        SolidBrush mySolidBrush4 = new SolidBrush(Color.Green);

        // clear drawing area setting it to color white
        myGraphics.Clear(Color.White);

        // find index, draw proper shape
        switch (imageComboBox.SelectedIndex)
        {
            case 0: // case Circle is selected
                myGraphics.DrawEllipse(myPen, 50, 50, 150, 150);
                break;
            case 1: // case Rectangle is selected
                myGraphics.DrawRectangle(myPen2, 50, 50, 150, 150);
                break;
            case 2: // case Ellipse is selected
                myGraphics.DrawEllipse(myPen3, 50, 85, 150, 115);
                break;
            case 3: // case Pie is selected
                myGraphics.DrawPie(myPen4, 50, 50, 150, 150, 0, 45);
                break;
            case 4: // case Filled Circle is selected
                myGraphics.FillEllipse(mySolidBrush, 50, 50, 150, 150);
                break;
            case 5: // case Filled Rectangle is selected
                myGraphics.FillRectangle(mySolidBrush2, 50, 50, 150,
                   150);
                break;
            case 6: // case Filled Ellipse is selected
                myGraphics.FillEllipse(mySolidBrush3, 50, 85, 150, 115);
                break;
            case 7: // case Filled Pie is selected
                myGraphics.FillPie(mySolidBrush4, 50, 50, 150, 150, 0,
                   45);
                break;}
        myGraphics.Dispose(); // release the Graphics object
    }

You can easily change the color of a pen using a ColorDialog and the result that the user selects.您可以使用ColorDialog和用户选择的结果轻松更改笔的颜色。 Here is an example of what this might look like:以下是这可能是什么样子的示例:

Pen pen = new(Color.Black); //Black for a default starting color

ColorDialog colorDialog = new();
if (colorDialog.ShowDialog() == DialogResult.OK){
    pen.Color = colorDialog.Color;
}

You would then use that pen where you would need it.然后,您可以在需要的地方使用该笔。

This could also work with a dropdown where you manually define the colors in the drop down and a switch can be used on the dropdown index change and update the pen color inside each case.这也可以与下拉列表一起使用,您可以在下拉列表中手动定义颜色,并且可以在下拉索引更改时使用开关并更新每种情况下的笔颜色。

Another way you could be creative is by letting the user type in the R, B, G values in 3 different text boxes.另一种可以发挥创意的方法是让用户在 3 个不同的文本框中键入 R、B、G 值。 You could put a rectangle next to these textboxes for a color preview.您可以在这些文本框旁边放置一个矩形以进行颜色预览。 When one of the textbox values changes, the preview rectangle's back color, as well as the pen's color, could change to the R, G, B value given by the user and default to black if the value is not valid.当文本框值之一发生变化时,预览矩形的背景颜色以及笔的颜色可能会更改为用户指定的 R、G、B 值,如果该值无效,则默认为黑色。

You can change the color of a Pen using the Color property of the Pen like this: 您可以更改颜色Pen使用Color的财产Pen是这样的:

Pen p = new Pen();
p.Color = Color.Red;

Then you can use p.Color = ... in each case and use the same pen. 然后可以在每种情况下使用p.Color = ...并使用同一支笔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM