旋转屏幕上的矩形
我正在使用 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);
矩形会旋转,但旋转得越多,它的位置就会以圆形移动。
我很确定这是因为我正在旋转屏幕的中心,而不是矩形的中心?
那么我该如何围绕矩形的中心旋转它呢?
I'm using GDI+ to try to draw a rectangle on the screen and rotate it 45 degrees.
Here's the code I'm using
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);
The rectangle rotates, but it's position moves in a circle the more it rotates.
I'm pretty sure this is because I'm rotating the center of the screen, instead of the center of the rectangle?
So how would I rotate it around the center of the rectangle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是它没有像您注意到的那样围绕矩形的中心旋转。因此,您需要在旋转对象后对其进行平移。
度是您想要旋转矩形的量。 posX 和 posY 是您要在屏幕上绘制它的位置。
此外,您还需要确保传递 MatrixOrder::Append,否则变换的顺序可能会更改,并且将在旋转之前应用平移(给您带来与您所看到的类似的效果)
The problem is that it's not rotating around the center of the Rectangle as you noticed. So you need to translate the object after it's rotated.
degrees is the amount you want to rotate your rectangle. posX and posY are the position where you want to draw it in screen.
Also you need to make sure you pass MatrixOrder::Append otherwise the order of the Transform might be changed and that will apply the Translation before rotating (giving you a similar effect to what you are seeing)