Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.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#_Windows_Forms_Graphics - Fatal编程技术网

C# 单击即可绘制图形并使其保持不变

C# 单击即可绘制图形并使其保持不变,c#,windows,forms,graphics,C#,Windows,Forms,Graphics,这个小程序打开一个windows窗体并绘制70个红色矩形,用户在其中单击窗体 每次用户单击时,矩形消失,并在新的单击点上绘制新的矩形 我希望当用户单击并绘制一组新的矩形时,矩形保持不变 public partial class Form1 : Form { private List<Rectangle> Rectangles { get; set; } public Form1() { InitializeComponent();

这个小程序打开一个windows窗体并绘制70个红色矩形,用户在其中单击窗体

每次用户单击时,矩形消失,并在新的单击点上绘制新的矩形

我希望当用户单击并绘制一组新的矩形时,矩形保持不变

public partial class Form1 : Form
{
    private List<Rectangle> Rectangles { get; set; }

    public Form1()
    {
        InitializeComponent();
        Rectangles = new List<Rectangle>();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        if (Rectangles.Count > 0)
            e.Graphics.DrawRectangles(Pens.Red, Rectangles.ToArray());
    }

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        for (int i = 1; i <= 70; i++)
        {
            Rectangles.Add(new Rectangle(e.X - 50 - i * 5, e.Y - 40 - i * 5, 100, 80));
        }
        Invalidate();
    }
}
我该怎么做

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace tegnRektangel
{
    public partial class Form1 : Form
    {
        int x;
        int y;
        bool mouseClicked = false;
        Graphics g = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            Invalidate();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (mouseClicked)
            {
                g = panel1.CreateGraphics();
                paintRectangel();
            }


        }
        private void paintRectangel()
        {
            for (int i = 1; i <= 70; i++)
            {
                g.DrawRectangle(Pens.Red, x - 50-i*5, y - 40-i*5, 100, 80);  
            }
            g.Dispose();

        }//end paint

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            mouseClicked = true;
            Point clickPoint = new Point(e.X,e.Y);
            x = clickPoint.X;
            y = clickPoint.Y;
            panel1.Invalidate();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间tegnRektangel
{
公共部分类Form1:Form
{
int x;
int-y;
bool mouseClicked=false;
图形g=空;
公共表格1()
{
初始化组件();
}
私有void Form1_Paint(对象发送器、PaintEventArgs e)
{
}
私有void Form1_Resize(对象发送方,事件参数e)
{
使无效();
}
私有void panel 1_Paint(对象发送器,PaintEventArgs e)
{
如果(鼠标单击)
{
g=panel1.CreateGraphics();
天使();
}
}
私人天使()
{
对于(int i=1;iFrom:

通过CreateGraphics检索的图形对象 方法通常不应在当前窗口之后保留 消息已被处理,因为使用该对象绘制的任何内容 将与下一个WM_PAINT消息一起擦除

您可以这样处理它:

  • 在单击事件中,将(x,y)坐标添加到坐标列表中

  • 在绘制事件中,迭代所有这些(x,y)坐标并绘制每个矩形


下面是一些代码,演示如何为每次单击创建矩形、存储矩形以及绘制所有存储的矩形

public partial class Form1 : Form
{
    private List<Rectangle> Rectangles { get; set; }

    public Form1()
    {
        InitializeComponent();
        Rectangles = new List<Rectangle>();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        if (Rectangles.Count > 0)
            e.Graphics.DrawRectangles(Pens.Red, Rectangles.ToArray());
    }

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        for (int i = 1; i <= 70; i++)
        {
            Rectangles.Add(new Rectangle(e.X - 50 - i * 5, e.Y - 40 - i * 5, 100, 80));
        }
        Invalidate();
    }
}
公共部分类表单1:表单
{
私有列表矩形{get;set;}
公共表格1()
{
初始化组件();
矩形=新列表();
}
私有void Form1_Paint(对象发送器、PaintEventArgs e)
{
如果(矩形数>0)
e、 图形.绘图矩形(Pens.Red,矩形.ToArray());
}
private void Form1\u鼠标单击(对象发送方,鼠标目标e)
{

对于(int i=1;i),您必须保留一组矩形(或坐标)以供
panel1
绘制。当前它只绘制“生成”的矩形,当再次绘制时,上一次绘制中的所有内容都会被擦除。黄金法则:永远不要使用CreateGraphics()@HansPassant你能推荐一个替代方案吗?我其实很好奇,因为我对绘画没有什么经验。而且不要存储那个图形对象。杀死这个:
Graphics g=null;
@BlakeThingstad我做了这个,它绝对有效。也很有意义。谢谢。