Whys is Graphics.Clear();在 C# 中不起作用
本文关键字:不起作用 is Graphics Clear Whys | 更新日期: 2023-09-27 18:31:49
我正在为我的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**
}
}
}
干杯拉布
尝试使用如下颜色的Graphics.Clear Method。
this.CreateGraphics().Clear(Form1.ActiveForm.BackColor);