Rotate The Rectangle in GDI

我们两清 提交于 2020-01-05 03:35:18

问题


I am using the windows GDI API ExtTextOut function to draw text like this:

ExtTextOut(hDC, 2000, 2000, 0, &stRect, PrintText, TextOutLen, aiCharCellDistances);

I am trying to rotate the text, and I do rotate the text. But when I fill the rectangle with colors, I found that the rectangle didn't rotate with the text.

Is there any way to rotate the rectangle with the text? Or is there a better way to do this?

P.S.: My goal is to draw text in the rectangle (like text area) and can rotate it in any angle, and set background color, border line, line break, align right, etc.

Thanks!


回答1:


It's not 100% clear what you want, but I think you want to draw some text and rectangle rotated at the same angle? If so, it's probably easiest to use SetWorldTransform to do the job.

Here's some code doing it with MFC:

double factor = (2.0f * 3.1416f)/360.0f;
double rot = 45.0f * factor;

// Create a matrix for the transform we want (read the docs for details)
XFORM xfm = { 0.0f };
xfm.eM11 = (float)cos(rot);
xfm.eM12 = (float)sin(rot);
xfm.eM21 = (float)-sin(rot);
xfm.eM22 = (float)cos(rot);

pDC->SetGraphicsMode(GM_ADVANCED);
pDC->SetWorldTransform(&xfm);    // Tell Windows to use that transform matrix

pDC->SetBkMode(TRANSPARENT);
CRect rect{ 290, 190, 450, 230 };
CBrush red;
red.CreateSolidBrush(RGB(255, 0, 0));

pDC->FillRect(rect, &red); // Draw a red rectangle behind the text

pDC->TextOut(300, 200, L"This is a string"); // And draw the text at the same angle

For the most part, doing this without MFC just means changing pDC->foo(args) to foo(dc, args).

The result looks like this:

Note that in this case, you do not need to specify rotation (at all--either lfRotation or lfEscapement) for the font you use. You just draw like it was normal text, and the world transform handles all the rotation.



来源:https://stackoverflow.com/questions/38757823/rotate-the-rectangle-in-gdi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!