在GDI中旋转矩形 [英] Rotate The Rectangle in GDI

查看:127
本文介绍了在GDI中旋转矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows GDI API ExtTextOut函数绘制这样的文本:

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 .:我的目标是在矩形(如文本区域)中绘制文本,并可以任意角度旋转文本,并设置背景颜色,边框线,换行符,向右对齐等.

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.

谢谢!

推荐答案

100%不清楚您想要什么,但是我想您要绘制一些以相同角度旋转的文本和矩形?如果是这样,使用SetWorldTransform来完成这项工作可能是最简单的.

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.

以下是使用MFC进行编码的代码:

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

在大多数情况下,在没有MFC的情况下执行此操作仅意味着将pDC->foo(args)更改为foo(dc, args).

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

结果如下:

请注意,在这种情况下,您不需要不需要为所使用的字体指定旋转(完全为lfRotationlfEscapement).您就像普通文本一样进行绘制,并且世界变换可以处理所有旋转.

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.

这篇关于在GDI中旋转矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆