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

标签 c# winforms drawing

我是一名学生,我是新来的。我有一个类(class)项目来制作类似 Paint 的程序。我有一个基类Shape,带有 DrawSelf、Contains 等。矩形、椭圆和三角形的方法和类。我还有另外两个类 DisplayProccesor,它是绘图类,以及 DialogProcessor,它控制与用户的对话。这些是项目的要求。

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

最佳答案

我很难理解你的问题。我的猜测是当你旋转第一个形状并绘制它时。然后画另一个形状,第二个形状也旋转。

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

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

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

关于c# - 旋转形状时,它与旋转的形状保持在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12844870/

相关文章:

c# - 在 Blazor 中选择框绑定(bind)

c# - 如何从文件夹中的页面重定向到根目录中的页面?

c# - 同时正确更改字体样式

c# - otside 类放置在 Form 中的 Visual Studio 调用方法

c++ - (不)在其他像素下方绘制像素的有效方法?

swift - 为绘图应用程序添加撤消功能

c# - 同步框架 - 何时更新墓碑?

c# - 使用 C# 在 MongoDB 中查询、过滤和更新多级嵌套数组

最小化屏幕时 C# LinearGradientBrush 故障

java - PorterDuffXfermode DST_IN 未按预期工作