旋转形状时,它与旋转的形状保持在一起

vor*_*tex 6 c# drawing winforms

我是学生,我在这里很新.我有一个课程项目来制作类似Paint的程序.我有一个基类Shape与DrawSelf,包含等.现在,Rectangle,Ellipse和Triangle的方法和类.此外,我有两个其他类的DisplayProccesor,它是绘图类,DialogProcessor,它控制与用户的对话.Theese是该项目的要求.

public class DisplayProcessor
{

    public DisplayProcessor()
    {
    }

    /// <summary>
    /// List of shapes
    /// </summary>
    private List<Shape> shapeList = new List<Shape>();
    public List<Shape> ShapeList
    {
        get { return shapeList; }
        set { shapeList = value; }
    }

    /// <summary>
    /// Redraws all shapes in shapeList
    /// </summary>
    public void ReDraw(object sender, PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        Draw(e.Graphics);
    }

    public virtual void Draw(Graphics grfx)
    {
        int n = shapeList.Count;
        Shape shape;

        for (int i = 0; i <= n - 1; i++)
        {
            shape = shapeList[i];
            DrawShape(grfx, shape);
        }
    }

    public virtual void DrawShape(Graphics grfx, Shape item)
    {
        item.DrawSelf(grfx);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是另一个:

public class DialogProcessor : DisplayProcessor
{
    public DialogProcessor()
    {
    }

    private Shape selection;
    public Shape Selection
    {
        get { return selection; }
        set { selection = value; }
    }

    private bool isDragging;
    public bool IsDragging
    {
        get { return isDragging; }
        set { isDragging = value; }
    }

    private PointF lastLocation;
    public PointF LastLocation
    {
        get { return lastLocation; }
        set { lastLocation = value; }
    }

   public void AddRandomRectangle()
    {
        Random rnd = new Random();
        int x = rnd.Next(100, 1000);
        int y = rnd.Next(100, 600);

        RectangleShape rect = new RectangleShape(new Rectangle(x, y, 100, 200));
        rect.FillColor = Color.White;

        ShapeList.Add(rect);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,我想旋转一个由用户选择的形状.我试着这样.它旋转它,但我明白了:http://www.freeimagehosting.net/qj3zp

public class RectangleShape : Shape
{

    public override void DrawSelf(Graphics grfx)
    {
        grfx.TranslateTransform(Rectangle.X + Rectangle.Width / 2, Rectangle.Y + Rectangle.Height / 2);
        grfx.RotateTransform(base.RotationAngle);
        grfx.TranslateTransform( - (Rectangle.X + Rectangle.Width / 2), -( Rectangle.Y + Rectangle.Height / 2));
        grfx.FillRectangle(new SolidBrush(FillColor), Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height);
        grfx.DrawRectangle(Pens.Black, Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height);
        grfx.ResetTransform();
    }
}
Run Code Online (Sandbox Code Playgroud)

vor*_*tex 1

我已经解决了!问题是我为每个形状绘制选择,当我旋转形状时,选择保持不旋转。我对选择进行了与DrawSelf方法相同的转换,一切都很好!干杯!