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

时间:2012-10-11 17:06:39

标签: c# winforms drawing

我是一名学生,而且我是一名新生儿。我有一个课程项目来制作类似Paint的程序。我有一个带有DrawSelf的基类 Shape ,包含ect。现在,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);
    }
}

这是另一个:

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);
    }
}

所以,我想旋转一个由用户选择的形状。 我试着这样。它旋转它,但我明白了: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();
    }
}

2 个答案:

答案 0 :(得分:2)

我在解释你的问题时遇到了困难。我的猜测是,当你旋转第一个形状并绘制它。然后绘制另一个形状,第二个形状也旋转。

这是因为,所有DrawSelf方法都使用相同的图形引用,因此在一个方法上使用的任何转换都将影响同一上下文中的所有连续调用。

您只需在每个DrawSelf方法的末尾调用Graphics.ResetTransform方法即可解决此问题。

public override void DrawSelf(Graphics grfx)
    {
        base.DrawSelf(grfx);

        //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.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();
    }

答案 1 :(得分:1)

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