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?
我想把文本绘制到一个GDI表面,然后顺时针旋转90度。我更喜欢使用DrawText来绘制文本,因为它支持回车。我尝试使用带有lfEscapement的字体(参见下面的代码),但是这条线不是旋转的——一条线被呈现在另一条线上。有没有可能旋转文本?或者不旋转和旋转整个设备上下文?
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;
}
2 个解决方案
#1
3
lf.lfEscapement = 90;
That should be 900 to get the text vertical, units are 0.1 degrees.
应该是900,让文本垂直,单位是0。1度。
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:
我恐怕你的计划是让提纲来处理断线。我无法说服它正确地对齐文字。它对准最后一行,而不是第一行。一些可以玩的代码:
wchar_t* msg = L"Hello\r\nworld";
RECT rcMeasure = {0, 0, 400, 0};
DrawTextEx(hdc, msg, -1, &rcMeasure, DT_CALCRECT, 0);
RECT rcDraw = {10, 30, 10 + rcMeasure.bottom - rcMeasure.top, 30 + rcMeasure.right - rcMeasure.left };
FillRect(hdc, &rcDraw, (HBRUSH) (COLOR_WINDOW+2));
SetTextAlign(hdc, TA_TOP | TA_CENTER);
DrawTextEx(hdc, msg, -1, &rcDraw, DT_BOTTOM, 0);
I think I tried all alignment options.
我想我尝试了所有的对齐选项。
#2
0
I have the impression that this link answers your question, but using ExtTextOut, and not DrawText
我觉得这个链接可以回答您的问题,但是使用ExtTextOut,而不是DrawText。
http://www.codeproject.com/KB/GDI/textrotation.aspx
http://www.codeproject.com/KB/GDI/textrotation.aspx
it's not GDI+ it's MFC but they are close.
它不是GDI+它是MFC,但它们很接近。
#1
3
lf.lfEscapement = 90;
That should be 900 to get the text vertical, units are 0.1 degrees.
应该是900,让文本垂直,单位是0。1度。
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:
我恐怕你的计划是让提纲来处理断线。我无法说服它正确地对齐文字。它对准最后一行,而不是第一行。一些可以玩的代码:
wchar_t* msg = L"Hello\r\nworld";
RECT rcMeasure = {0, 0, 400, 0};
DrawTextEx(hdc, msg, -1, &rcMeasure, DT_CALCRECT, 0);
RECT rcDraw = {10, 30, 10 + rcMeasure.bottom - rcMeasure.top, 30 + rcMeasure.right - rcMeasure.left };
FillRect(hdc, &rcDraw, (HBRUSH) (COLOR_WINDOW+2));
SetTextAlign(hdc, TA_TOP | TA_CENTER);
DrawTextEx(hdc, msg, -1, &rcDraw, DT_BOTTOM, 0);
I think I tried all alignment options.
我想我尝试了所有的对齐选项。
#2
0
I have the impression that this link answers your question, but using ExtTextOut, and not DrawText
我觉得这个链接可以回答您的问题,但是使用ExtTextOut,而不是DrawText。
http://www.codeproject.com/KB/GDI/textrotation.aspx
http://www.codeproject.com/KB/GDI/textrotation.aspx
it's not GDI+ it's MFC but they are close.
它不是GDI+它是MFC,但它们很接近。