简体   繁体   中英

How can I use a graphic object's clear function(which requires a color argument) to clear the graphics of a form?

How can I use a graphic object's clear function(which requires a color argument) to clear the graphics of a form?

Using WFA

    public partial class Form1 : Form
    {
        Graphics gr;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            gr = this.CreateGraphics();
            gr.DrawLine(new Pen(Color.Red), 0, 0, 500, 500);
            //gr.Clear(); // ERROR IT NEEDS AN ARGUMENT BUT WHAT?
        }


    }
}

gr.Clear() needs an argument, but what colour?

I tried MessageBox.Show(this.BackColor.ToString()); so as to see the color of the form. But it just displays "Color [Control]" which doesn't tell me anything.

I tried gr.Clear(Color.Gray) but that's not the correct colour, it's not the color of the default background of the form.

Since you are creating the control, I assume you should know what the color is. If you want the default control background color, just use gr.Clear(BackColor) There is no need to see what color it is, it just is the color you assigned to it or the system default.

For transparent controls you would use Color.Transparent, but usually controls are not needed to be transparent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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