使用 GDI 将文本旋转 90 度
我想将文本绘制到 GDI 表面并将该文本逆时针旋转 90 度。我更喜欢使用 DrawText 来绘制文本,因为它支持回车。我尝试使用带有 lfEscapement 的字体(请参阅下面的代码),但该线没有旋转 - 一条线渲染在另一条线之上。是否有可能旋转文本?或者在不旋转的情况下渲染并旋转整个设备上下文?
普通文本布局:
旋转(所需结果):
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
LOGFONT lf = {0};
HANDLE hFont;
ZeroMemory(&lf, sizeof(LOGFONT));
lf.lfWeight = FW_NORMAL;
lstrcpy(lf.lfFaceName, _T("Tahoma"));
lf.lfEscapement = 90;
lf.lfHeight = 30;
hFont = CreateFontIndirect (&lf);
hFont = (HFONT)SelectObject (ps.hdc, hFont);
RECT RectBody = {10,lf.lfHeight+10,::GetSystemMetrics(SM_CXSCREEN)-10,::GetSystemMetrics(SM_CYSCREEN)-lf.lfHeight-20};
{
ScopedLock lock(me->m_mutex);
DrawText (ps.hdc, me->GetMessageString().c_str(), (int)me->GetMessageString().length(), &RectBody, 0);
}
hFont = (HFONT)SelectObject (ps.hdc, hFont);
DeleteObject (hFont);
EndPaint(hWnd, &ps);
break;
}
I want to draw text to a GDI Surface and rotate this text by 90 degrees counter clockwise. I would prefer to use DrawText to draw the text because it supports carriage return. I tried to use a font with lfEscapement (see the code below) but the line is not rotated - one line gets rendered over the other. Is there any possibility to rotate the text? Or to render without rotation and rotate the whole device context?
Normal text layout:
Rotated (desired result):
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
LOGFONT lf = {0};
HANDLE hFont;
ZeroMemory(&lf, sizeof(LOGFONT));
lf.lfWeight = FW_NORMAL;
lstrcpy(lf.lfFaceName, _T("Tahoma"));
lf.lfEscapement = 90;
lf.lfHeight = 30;
hFont = CreateFontIndirect (&lf);
hFont = (HFONT)SelectObject (ps.hdc, hFont);
RECT RectBody = {10,lf.lfHeight+10,::GetSystemMetrics(SM_CXSCREEN)-10,::GetSystemMetrics(SM_CYSCREEN)-lf.lfHeight-20};
{
ScopedLock lock(me->m_mutex);
DrawText (ps.hdc, me->GetMessageString().c_str(), (int)me->GetMessageString().length(), &RectBody, 0);
}
hFont = (HFONT)SelectObject (ps.hdc, hFont);
DeleteObject (hFont);
EndPaint(hWnd, &ps);
break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文本垂直应为 900,单位为 0.1 度。
恐怕您让 DrawText 处理换行符的计划会失败。我无法说服它正确对齐文本。它对齐在最后一行,而不是第一行。一些可以使用的代码:
我想我尝试了所有对齐选项。
That should be 900 to get the text vertical, units are 0.1 degrees.
Your plan to let DrawText take care of the line breaks is going to fall flat I'm afraid. I could not convince it to align the text properly. It aligns on the last line, not the first. Some code to play with:
I think I tried all alignment options.
我的印象是此链接回答了您的问题,但使用 ExtTextOut,而不是 DrawText
http: //www.codeproject.com/KB/GDI/textrotation.aspx
它不是 GDI+,它是 MFC,但它们很接近。
I have the impression that this link answers your question, but using ExtTextOut, and not DrawText
http://www.codeproject.com/KB/GDI/textrotation.aspx
it's not GDI+ it's MFC but they are close.