什么是Graphics.Clear();不在C#中工作

时间:2012-04-20 09:33:38

标签: c# .net winforms system.drawing

我正在为我的comp103文件做一些修改,我无法使用Graphics.Clear()获取按钮;清除图形纸的方法。有人可以指出我的代码中的错误,因为我花了大约一个小时试图在网上找到答案。

很抱歉提出这样一个noob问题。

以下是代码:

namespace Week_4_Ex1
{
    public partial class Form1 : Form
    {
        const int height1 = 150; // A constant that stores the flags height
        const int width1 = 100; // A constant that stores the flags width

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Button event that Draws a flag
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDraw_Click(object sender, EventArgs e)
        {
            Graphics Paper = this.CreateGraphics(); // Creates graphics paper to draw on
            Brush br1 = new SolidBrush(Color.Blue); // Creates a blue brush to draw with
            Brush br2 = new SolidBrush (Color.Orange); // Creates a orange brush to draw with
            Brush br3 = new SolidBrush (Color.Red); // Creates a red brush to draw with

            Pen Pen1 = new Pen(Color.Blue);         // Creates a blue pen to draw with
            Pen Pen2 = new Pen(Color.Orange);       // Creates a orange pen to draw with
            Pen Pen3 = new Pen(Color.Red);          // Creates a red pen to draw with

            //The following code draws a flag
            Paper.DrawRectangle(Pen1, 10, 10, width1, height1); // Draws a blue rectangle
            Paper.FillRectangle(br1, 10, 10, width1, height1); // Fills the rectangle with blue
            Paper.DrawRectangle(Pen2, 110, 10, width1, height1); // Draws a blue rectangle
            Paper.FillRectangle(br2, 110, 10, width1, height1); // Fills the rectangle with blue
            Paper.DrawRectangle(Pen3, 210, 10, width1, height1); // Draws a blue rectangle
            Paper.FillRectangle(br3, 210, 10, width1, height1); // Fills the rectangle with blue

        }
        /// <summary>
        /// Button that closes the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); // Closes the form
        }

        /// <summary>
        /// Clears the picturebox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnErase_Click(object sender, EventArgs e)
        {
            Graphics.Clear(); //**THIS DOESN'T WORK**
        }

    }
}

干杯, 拉布

1 个答案:

答案 0 :(得分:6)

尝试Graphics.Clear方法,颜色如下。

this.CreateGraphics().Clear(Form1.ActiveForm.BackColor);