【问题标题】:Rotate rectangle on screen在屏幕上旋转矩形
【发布时间】:2012-02-18 02:42:46
【问题描述】:

我正在使用 GDI+ 尝试在屏幕上绘制一个矩形并将其旋转 45 度。
这是我正在使用的代码

Pen RedPen(Color(255, 255, 0, 0), 4);

HDC screenDC = GetDC(NULL);
Graphics graphics(screenDC);

graphics.RotateTransform(45);
graphics.DrawRectangle(&RedPen, 150, 150, 50, 50);

矩形旋转,但它的位置在一个圆圈内移动,它旋转得越多。
我很确定这是因为我正在旋转屏幕的中心,而不是矩形的中心?
那么如何围绕矩形的中心旋转它呢?

【问题讨论】:

    标签: c++ gdi+ rotation


    【解决方案1】:

    问题是它没有像您注意到的那样围绕 Rectangle 的中心旋转。所以你需要在对象旋转后平移它。

            e->Graphics->RotateTransform(degrees);
            e->Graphics->TranslateTransform(posX, posY, MatrixOrder::Append);
            e->Graphics->DrawRectangle(gcnew Pen( Color::Blue,3.0f ),  -width / 2, -height / 2, width, height);
    

    度数 是您要旋转矩形的量。 posX 和 posY 是您要在屏幕上绘制的位置。

    您还需要确保传递 MatrixOrder::Append 否则变换的顺序可能会改变,并且会在旋转之前应用翻译(给您带来与您所看到的类似的效果)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      相关资源
      最近更新 更多