C++ 在屏幕上旋转矩形
我正在使用GDI+尝试在屏幕上绘制一个矩形,并将其旋转45度。C++ 在屏幕上旋转矩形,c++,gdi+,rotation,C++,Gdi+,Rotation,我正在使用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); 矩形会旋转,但旋转越多,其位置就会在一个圆圈内移
这是我正在使用的代码
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);
矩形会旋转,但旋转越多,其位置就会在一个圆圈内移动。我很确定这是因为我正在旋转屏幕的中心,而不是矩形的中心?
那么我该如何围绕矩形的中心旋转它呢?问题是它没有像您所注意到的那样围绕矩形的中心旋转。因此,在旋转对象后,需要对其进行平移
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,否则变换的顺序可能会更改,这将在旋转之前应用平移(使您看到的效果类似)问题在于它没有像您所注意到的那样围绕矩形的中心旋转。因此,在旋转对象后,需要对其进行平移
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,否则变换的顺序可能会改变,这将在旋转之前应用转换(给您提供与所看到的效果类似的效果)