在我的Paint程序中绘制矩形时出错

时间:2014-03-24 20:44:55

标签: c# winforms inheritance paint drawrectangle

public partial class Form1 : Form
{
    Point downPoint , upPoint;
    List<Shapes> shapes = new List<Shapes>();
    public ShapesEnum shapeType;

    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        foreach (var s in shapes)
            s.Draw(e.Graphics);
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        downPoint = e.Location;
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
        upPoint = e.Location;
        CreateShape();
        this.Invalidate();
    }

    private void CreateShape()
    {
        if (shapeType == ShapesEnum.Circle)
        {
            Rectangle rect = new Rectangle(downPoint.X, downPoint.Y, upPoint.X - downPoint.X, upPoint.Y - downPoint.Y);
            Ellipse ellipse = new Ellipse() { rect = rect };
            shapes.Add(ellipse);
        }

        else if (shapeType == ShapesEnum.Rectangle)
        {
            Rectangle rect = new Rectangle(downPoint.X, downPoint.Y, upPoint.X - downPoint.X, upPoint.Y - downPoint.Y);
            Rect rectangle = new Rect() { rect = rect };
            shapes.Add(rectangle);
        }

        else if (shapeType == ShapesEnum.Line)
        {
            Point pt1 = new Point (upPoint.X, upPoint.Y);
            Point pt2 = new Point (downPoint.X, downPoint.Y);

            Ln rectangle = new Ln() { PointUp = pt1, PointDown = pt2 };
            shapes.Add(rectangle);
        }
        lblStatus.Text = string.Format("dn:{0} up:{1} ct:{2}", downPoint, upPoint, shapes.Count());
    }

    private void btnCircle_Click(object sender, EventArgs e)
    {
        shapeType = ShapesEnum.Circle;
    }

    private void btnRectangle_Click(object sender, EventArgs e)
    {
        shapeType = ShapesEnum.Rectangle;
    }

    private void btnLine_Click(object sender, EventArgs e)
    {
        shapeType = ShapesEnum.Line;
    }

}

这是我的绘画计划的主要部分。

public enum ShapesEnum { Circle, Rectangle, Line }
abstract class Shapes
{
    public Rectangle rect { get; set; }
    public Color color { get; set; }
    public bool isFilled { get; set; }
    public abstract void Draw(Graphics g);
}

class Ellipse : Shapes
{
    public override void Draw(Graphics g)
    {
        g.DrawEllipse(Pens.Black, rect);
    }
}

class Rect : Shapes
{
    public override void Draw(Graphics g)
    {
        g.DrawRectangle(Pens.Black, rect);
    }
}

class Ln : Shapes
{
    public Point PointUp { get; set; }
    public Point PointDown { get; set; }

    public override void Draw(Graphics g)
    {
        g.DrawLine(Pens.Black, PointUp, PointDown);
    }
}

这是我的类,用于在形状上继承。根据形状,将调用其中一个类。


绘制椭圆效果很好。绘制线也一样。但是,绘制矩形时存在一个错误。如果我把鼠标拖动并从左上方放到右下方,这将有效。但是,如果我转向另一个方向,则表单上不可见,但shapes.Count()仍然会添加到shapes列表中。

是什么导致了这个错误?

1 个答案:

答案 0 :(得分:2)

尝试这样的事情,以便以正确的方式使用起点和终点:

int x0 = Math.Min(upPoint.X, downPoint.X);
int y0 = Math.Min(upPoint.Y, downPoint.Y);
Point upperLeft = new Point(x0, y0);

int x1 = Math.Max(upPoint.X, downPoint.X);
int y1 = Math.Max(upPoint.Y, downPoint.Y);
Point lowerRight = new Point(x1, y1);

Rectangle rect = new Rectangle(x0, y0, x1 - x0, y1 - y0);