首页 > 解决方案 > 如何在 Windows 窗体应用程序上的 Refresh() 之后重绘所有内容

问题描述

我有 9 个矩形,我需要在其中绘制一个文本,我有两个按钮,它们正在选择我需要绘制的文本。当我单击按钮然后单击矩形时,它将获取鼠标的位置,然后检查我需要在哪个矩形中绘制。当我因为 Refresh() 而需要在另一个矩形中绘制时,它会绘制新的矩形并清除过去的绘制。我怎样才能保存它?

public partial class Form1 : Form
    {
        List<Rectangle> rects = new List<Rectangle>();
        Rectangle rectangle = new Rectangle();
        string str;
        Point start;
        int x = 0;
        Point[] points=new Point[100];
        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    rects.Add( new Rectangle(130 + (j * 130), 130 + (i * 130), 130, 130));

                }
            }
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            foreach(Rectangle rect in rects)
                    e.Graphics.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width, rect.Height);

           
                getRectangle(rectangle);
                if (rectangle.Width != 0 && rectangle.Height != 0)
                {
                    using (SolidBrush solidBrush = new SolidBrush(Color.DarkGreen))
                    {
                        using (FontFamily fontFamily = new FontFamily("Arial"))
                        {
                            using (Font font = new Font(fontFamily, 20))
                            {
                                e.Graphics.DrawString(str, font, solidBrush, rectangle.X + 55, rectangle.Y + 55);
                            }
                        }
                    }
                }
            
            base.OnPaint(e);
        }

        private void Cross_Click(object sender, EventArgs e)
        {
            str = "X";

        }

        private void Circle_Click_1(object sender, EventArgs e)
        {
            str = "O";
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            start = e.Location;
            points[x] = start;
            x++;
            Invalidate();
        }

        public void getRectangle(Rectangle rectan)
        {
            rectangle.Width = 0;
            rectangle.Height = 0;
            if (str == "X")
            {
                foreach(Rectangle rect in rects)
                        if (rect.Contains(start))
                        {
                            rectangle.X = rect.X;
                            rectangle.Y = rect.Y;
                            rectangle.Width = rect.Width;
                            rectangle.Height = rect.Height;
                          
                        }
            }
            else if (str == "O")
            {
                
                    foreach (Rectangle rect in rects)
                        if (rect.Contains(start))
                        {
                            rectangle.X = rect.X;
                            rectangle.Y = rect.Y;
                            rectangle.Width = rect.Width;
                            rectangle.Height = rect.Height;

                        }
                
            }
        }

    }

标签: c#winforms

解决方案


不确定我是否理解正确,但似乎您需要创建一个数据库来保存您的对象。查看SQLUML 建模。一旦你熟悉了这些,可以考虑看看我这篇关于如何从数据库中提取数据的旧文章

(旁注:有许多不同的方法可以在代码中生成数据库实体,其中之一是Entity Framework,一旦您熟悉了 SQL,您应该考虑研究这些选项)

如果您想临时保存数据(这意味着在应用程序运行期间),请考虑使用这些对象制作objects并填充 a 。List<T>然后您可以创建一个循环并检索这些对象。像这样:

//create new class
public class Drawing
{
    public int X { get; private set; }
    public int Y { get; private set; }
    public Drawing(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }
}

//Initiate new object
public List<Drawing> drawings = new List<Drawing>();

//Add this to your loop
drawings.Items.Add(new Drawing() { X= YourX, Y= YourY });

//And foreach loop through your drawings to draw them.
foreach(Drawing drawing in drawings)
{
   //Draw your item here.
}

推荐阅读