我使用windows GDI API ExtTextOut函数来绘制这样的文本:
ExtTextOut(hDC, 2000, 2000, 0, &stRect, PrintText, TextOutLen, aiCharCellDistances);
Run Code Online (Sandbox Code Playgroud)
我正在尝试旋转文本,我会旋转文本.但是当我用颜色填充矩形时,我发现矩形没有随文本一起旋转.
有没有办法用文本旋转矩形?或者有更好的方法吗?
PS:我的目标是在矩形(如文本区域)中绘制文本,并可以任意角度旋转,并设置背景颜色,边框线,换行符,右对齐等.
谢谢!
它不是100%清楚你想要什么,但我想你想画一些文字和矩形以相同的角度旋转?如果是这样,它可能最容易SetWorldTransform用来完成这项工作.
以下是使用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
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,没有MFC这样做只意味着pDC->foo(args)改为foo(dc, args).
结果如下:
请注意,在这种情况下,您不需要为您使用的字体指定旋转(根本不是 - lfRotation或者lfEscapement).你只需绘制它就像普通文本一样,世界变换处理所有旋转.