-1

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

很抱歉问了这样一个菜鸟问题。

这是代码:

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**
        }

    }
}

干杯,拉布

4

1 回答 1

6

尝试使用以下颜色的Graphics.Clear方法。

this.CreateGraphics().Clear(Form1.ActiveForm.BackColor);  
于 2012-04-20T09:40:39.417 回答