2012-02-18 111 views
1

我使用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); 

矩形旋转的代码,但它的位置移动了一圈越旋转。
我很确定这是因为我旋转屏幕的中心,而不是矩形的中心?
那么我如何将它围绕矩形的中心旋转呢?

回答

1

问题是,它没有像你注意到的那样绕着矩形的中心旋转。所以你需要在旋转后翻译对象。

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

是要旋转您的矩形量。 posXposY是你想要在屏幕上绘制它的位置。

另外你需要确保你通过MatrixOrder ::追加否则的变换顺序可能会改变,这将旋转(给你一个类似的效果,你所看到的)前申请翻译