简体   繁体   English

如何在 Windows 窗体应用程序上的 Refresh() 之后重绘所有内容

[英]How to redraw everything after Refresh() on windows form Application

I have 9 rectangles and I need to draw a text inside them, I have two buttons which they are choosing the text that I need to draw into.我有 9 个矩形,我需要在其中绘制一个文本,我有两个按钮,他们正在选择我需要绘制的文本。 when I click on the button then click on the rectangle it will take the location of mouse and then check in which rectangle I need to draw into.当我单击按钮然后单击矩形时,它将占用鼠标的位置,然后检查我需要绘制的矩形。 when I need to draw in another rectangle because of Refresh() it drew the new one and clear the past draw.当我因为 Refresh() 而需要绘制另一个矩形时,它会绘制新的矩形并清除过去的绘制。 How can I save it?我怎样才能保存它?

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;

                        }
                
            }
        }

    }

Not sure if I understand correctly, but it seems as though you need to create a database to save your object into.不确定我是否理解正确,但似乎您需要创建一个数据库来保存您的对象。 Look into SQL and UML moddeling.查看SQLUML 建模 Once you are familiar with those consider taking a look at this older post of mine on how to extract data from a database here .一旦你熟悉这些考虑考虑看看我的关于如何从数据库中提取数据这一较早的帖子在这里

(Sidenote: There are many different ways to generate database entities in code, one of which is Entity Framework , once you get comfortable with SQL you should consider looking into these options) (旁注:有许多不同的方法可以在代码中生成数据库实体,其中之一是Entity Framework ,一旦您对 SQL 感到满意,您应该考虑研究这些选项)

If you want to temporarily save data (this means during the runtime of your application), consider making objects and fill a List<T> with those objects.如果您想临时保存数据(这意味着在应用程序运行期间),请考虑创建objects并用这些对象填充List<T> Then you can create a loop and retreive those objects.然后您可以创建一个循环并检索这些对象。 Like so:像这样:

//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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM