简体   繁体   English

在同一图形C#上绘制对象

[英]Draw object on same Graphics C#

 private void main_pic_Paint(object sender, PaintEventArgs e)
    {
       g = e.Graphics;
        if (rect_bt_clicked)
        {
            if (_r >= 0)
            {
                for (int j = 0; j <= _r; j++)
                {
                    rect = new Rectangle(
                        RectArray[j].SP.X,
                        RectArray[j].SP.Y,
                        RectArray[j].EP.X - RectArray[j].SP.X,
                        RectArray[j].EP.Y - RectArray[j].SP.Y);
                    Brush b = new SolidBrush(Color.Red);
                    Pen p = new Pen(Color.Blue, 2);
                    g.FillRectangle(b, rect);
                    g.DrawRectangle(p, rect);

                }
            }
        }
        if (ellip_bt_clicked)
        {
            if (_e >= 0)
            {
                for (int j = 0; j <= _e; j++)
                {
                    rect = new Rectangle(
                        EllipArray[j].SP.X,
                        EllipArray[j].SP.Y,
                        EllipArray[j].EP.X - EllipArray[j].SP.X,
                        EllipArray[j].EP.Y - EllipArray[j].SP.Y);
                    Brush b = new SolidBrush(Color.Red);
                    Pen p = new Pen(Color.Blue, 2);
                    g.FillEllipse(b, rect);
                    g.DrawEllipse(p, rect);

                }
            }
        }

    }

I have two buttons. 我有两个按钮。 One for draw rectangle and one for draw ellipse. 一个用于绘制矩形,另一个用于绘制椭圆。 But when i click draw rect. 但是,当我单击绘制矩形。 after that i click draw ellipse. 之后,我单击绘制椭圆。 It not work in one graphics. 它不能在一个图形中使用。 I don't know how to fix. 我不知道该如何解决。 Sorry about my English. 对不起,我的英语。

You need to draw EVERYTHING in the Paint event handler EVERY time. 您需要每次在Paint事件处理程序中绘制所有内容。 What you normally would do is store all the data that represents the entire drawing in one or more member variables and then, in the Paint event handler, you read that data and draw the drawing. 通常,将代表整个图形的所有数据存储在一个或多个成员变量中,然后在Paint事件处理程序中,读取该数据并绘制图形。

If you want to draw one rectangle and one ellipse then I would suggest you declare one or more variables to represent the rectangle and then one or more variables to represent the ellipse. 如果要绘制一个矩形和一个椭圆,那么我建议您先声明一个或多个变量来代表矩形,然后再声明一个或多个变量来代表椭圆。 That would include a variable to to indicate whether or not that shape is to be drawn, which would be false for both to start with. 这将包括一个变量,以指示是否要绘制该形状,这对于两者开始都是false的。 When you click the rectangle button you would set all the rectangle fields and when you click the ellipse button you would set all the ellipse fields. 单击矩形按钮时,将设置所有矩形字段;单击椭圆按钮时,将设置所有椭圆字段。

If you want to draw multiple rectangles and multiple ellipses then I would suggest that you define a class to represent each of them, possibly inheriting a common base class for the common functionality. 如果要绘制多个矩形和多个椭圆,则建议您定义一个类来表示它们中的每个矩形,并可能继承通用功能的通用基类。 You would then declare two variables to store a collection of each type. 然后,您将声明两个变量来存储每种类型的集合。 When you click the rectangle button you add an instance of one type to the collection of rectangles and when you click the ellipse button you add an instance of the other type to the ellipse collection. 当您单击矩形按钮时,您将一种类型的实例添加到矩形集合中,而当您单击椭圆按钮时,您将另一种类型的实例添加到椭圆集合中。

In either case, after putting the appropriate data in the appropriate place, you would call Refresh or, preferably, Invalidate and Update on the control you want to draw on. 无论哪种情况,在将适当的数据放置在适当的位置后,您都将在要绘制的控件上调用“刷新”,或者最好是“无效和更新”。 That will raise the Paint event and invoke the Paint event handler. 这将引发Paint事件并调用Paint事件处理程序。 In the handler, you read the data from the appropriate place and do the drawing. 在处理程序中,您从适当的位置读取数据并进行绘制。

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

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