使用 GDI 将文本旋转 90 度

发布于 2024-09-15 23:04:41 字数 1217 浏览 6 评论 0原文

我想将文本绘制到 GDI 表面并将该文本逆时针旋转 90 度。我更喜欢使用 DrawText 来绘制文本,因为它支持回车。我尝试使用带有 lfEscapement 的字体(请参阅下面的代码),但该线没有旋转 - 一条线渲染在另一条线之上。是否有可能旋转文本?或者在不旋转的情况下渲染并旋转整个设备上下文?


普通文本布局:

alt text


旋转(所需结果):

替代文字

    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:

alt text


Rotated (desired result):

alt text

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窗影残 2024-09-22 23:04:41
   lf.lfEscapement = 90;

文本垂直应为 900,单位为 0.1 度。

恐怕您让 DrawText 处理换行符的计划会失败。我无法说服它正确对齐文本。它对齐在最后一行,而不是第一行。一些可以使用的代码:

    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);

我想我尝试了所有对齐选项。

   lf.lfEscapement = 90;

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:

    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.

请恋爱 2024-09-22 23:04:41

我的印象是此链接回答了您的问题,但使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文