Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么是图形。清除();不在C语言中工作#_C#_.net_Winforms_System.drawing - Fatal编程技术网

C# 为什么是图形。清除();不在C语言中工作#

C# 为什么是图形。清除();不在C语言中工作#,c#,.net,winforms,system.drawing,C#,.net,Winforms,System.drawing,我正在为我的comp103论文做一些修改,但我找不到带有图形的按钮。清除();方法来清除图形文件。有人能指出我代码中的错误吗?因为我花了大约一个小时试图在网上找到答案 很抱歉问了这么一个问题 代码如下: namespace Week_4_Ex1 { public partial class Form1 : Form { const int height1 = 150; // A constant that stores the flags height

我正在为我的comp103论文做一些修改,但我找不到带有图形的按钮。清除();方法来清除图形文件。有人能指出我代码中的错误吗?因为我花了大约一个小时试图在网上找到答案

很抱歉问了这么一个问题

代码如下:

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

    }
}
namespace Week\u 4\u Ex1
{
公共部分类Form1:Form
{
const int height1=150;//存储标志高度的常量
const int width1=100;//存储标志宽度的常量
公共表格1()
{
初始化组件();
}
/// 
///绘制标志的按钮事件
/// 
/// 
/// 
私有void btnDraw\u单击(对象发送者,事件参数e)
{
图形纸=this.CreateGraphics();//创建要在其上绘制的图形纸
Brush br1=新的SolidBrush(Color.Blue);//创建一个蓝色笔刷来绘制
Brush br2=新的SolidBrush(Color.Orange);//创建一个橙色笔刷来绘制
Brush br3=新的SolidBrush(Color.Red);//创建一个用于绘制的红色笔刷
Pen Pen1=新笔(Color.Blue);//创建一支蓝色笔进行绘制
Pen Pen2=新笔(Color.Orange);//创建一支用于绘图的橙色笔
Pen Pen3=新笔(Color.Red);//创建用于绘图的红色笔
//下面的代码绘制一个标志
Paper.DrawRectangle(Pen1,10,10,宽度1,高度1);//绘制一个蓝色矩形
Paper.FillRectangle(br1,10,10,width1,height1);//用蓝色填充矩形
Paper.DrawRectangle(Pen2,110,10,宽度1,高度1);//绘制一个蓝色矩形
Paper.FillRectangle(br2,110,10,width1,height1);//用蓝色填充矩形
Paper.DrawRectangle(Pen32110,宽度1,高度1);//绘制一个蓝色矩形
Paper.FillRectangle(br3、210、10、宽度1、高度1);//用蓝色填充矩形
}
/// 
///关闭窗体的按钮
/// 
/// 
/// 
私有void btnExit\u单击(对象发送者,事件参数e)
{
this.Close();//关闭窗体
}
/// 
///清除图片框
/// 
/// 
/// 
私有void b生成单击(对象发送者,事件参数e)
{
Graphics.Clear();//**这不起作用**
}
}
}
干杯, Rab

尝试以下颜色的方法

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

首先,快速查看MSDN可以发现
Clear
不是类型为
Graphics
的静态方法,它还需要
color
参数。这是您的实际代码吗?您的类没有Graphics成员,
Graphics.Clear
方法不是静态的…如果我删除Graphics.Clear();它将在“启动调试”中运行,并按预期执行所有操作。但是,我在创建清除表单的按钮时遇到问题。所以说“不起作用”,我的意思是我不能得到图形;去工作。为那些帮忙的人干杯。我想我明白了我的不足:)你不应该以这种方式在表格上画图。尝试以下实验:单击按钮后,在窗体上拖动另一个窗口(如记事本)。会发生什么?表格上的所有绘图均应采用OnPaint方法。有关更多信息,请参阅本文:@Damith,图形对象是非托管对象。如果我们这样清除它,它会导致内存泄漏吗?