1

如何使用 MFC 函数绘制曲线文本?我想在下面实现这样的。

在此处输入图像描述

DrawText() 函数仅以直线绘制文本,我不知道如何以特定角度绘制弯曲文本。请帮我。

谢谢。

4

1 回答 1

6

您可以使用 GDI+,代码项目中有一个示例,它是用 C# 编写的,我将其翻译成 C++:

    Graphics graphics(hWnd);

    RECT rect = { 0 };
    GetWindowRect(hWnd, &rect);
    POINT center = { (rect.right - rect.left) / 2,(rect.bottom - rect.top) / 2 };
    double radius = min(rect.right - rect.left, (rect.bottom - rect.top)) / 3;
    TCHAR text[] = L"ABCDEFGHIJLKMNOPQRSTUVWXYZ";
    REAL emSize = 24;
    Font* font = new Font(FontFamily::GenericSansSerif(), emSize, FontStyleBold);
    for (int i = 0; i < _tcslen(text); ++i)
    {
        RectF re, in;
        Status result = graphics.MeasureString(&text[i], 1, font, in, &re);;

        double charRadius = radius + re.Height;

        double angle = (((float)i / _tcslen(text)) - 0.25) * 2 * M_PI;

        double x = (int)(center.x + cos(angle) * charRadius);
        double y = (int)(center.y + sin(angle) * charRadius);


        result = graphics.TranslateTransform(x, y);

        result = graphics.RotateTransform((float)(90 + 360 * angle / (2 * M_PI)));
        PointF start(0, 0);
        SolidBrush Red(Color(255, 255, 0, 0));
        result = graphics.DrawString(&text[i], 1, font, start, &Red);

        result = graphics.ResetTransform();

        SolidBrush Green(Color(255, 0, 255, 0));
        Pen* pen = new Pen(&Green, 2.0f);
        result = graphics.DrawArc(pen, (REAL)(center.x - radius), (REAL)(center.y - radius), radius * 2, radius * 2, 0, 360);
    }

一些头文件:

#define _USE_MATH_DEFINES
#include <math.h>
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")

用法:

您必须GdiplusStartup在创建任何 GDI+ 对象之前调用,并且必须在调用之前删除所有 GDI+ 对象(或让它们超出范围)GdiplusShutdown

GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
//To Do.
GdiplusShutdown(gdiplusToken);

结果:

在此处输入图像描述

更新:

    Graphics graphics(hWnd);

    RECT rect = { 0 };
    GetWindowRect(hWnd, &rect);
    POINT center = { (rect.right - rect.left) / 2,(rect.bottom - rect.top) / 2 };
    double radius = min(rect.right - rect.left, (rect.bottom - rect.top)) / 3;
    TCHAR text[72][4] = { 0 };
    for (int i = 0; i < 72; i++)
    {
         _itot((i/2)*10, text[i],10);
         i++;
         _tcscpy(text[i],L"");
    }
    REAL emSize = 8;
    Font* font = new Font(FontFamily::GenericSansSerif(), emSize, FontStyleBold);
    for (int i = 0; i < 72; ++i)
    {
        RectF re, in,rel;
        Status result = graphics.MeasureString(text[i], _tcslen(text[i]), font, in, &re);
        result = graphics.MeasureString(L"|", 1, font, in, &rel);
        double charRadius = radius - re.Height;

        double angle = (((float)i / 72) - 0.25) * 2 * M_PI;

        double x = (center.x + cos(angle) * charRadius);
        double y = (center.y + sin(angle) * charRadius);


        result = graphics.TranslateTransform(x, y);

        result = graphics.RotateTransform((float)(90 + 360 * angle / (2 * M_PI)));
        PointF start(0- re.Width/2, 0);
        SolidBrush Red(Color(255, 255, 0, 0));
        result = graphics.DrawString(text[i], _tcslen(text[i]), font, start, &Red);
        result = graphics.ResetTransform();

        x = (int)(center.x + cos(angle) * radius);
        y = (int)(center.y + sin(angle) * radius);
        result = graphics.TranslateTransform(x, y);
        result = graphics.RotateTransform((float)(90 + 360 * angle / (2 * M_PI)));

        PointF start1(0 - rel.Width / 2, 0);
        result = graphics.DrawString(L"|", 1, font, start1, &Red);
        result = graphics.ResetTransform();
    }
    SolidBrush Green(Color(255, 0, 255, 0));
    Pen* pen = new Pen(&Green, 2.0f);
    Status result = graphics.DrawArc(pen, (REAL)(center.x - radius), (REAL)(center.y - radius), radius * 2, radius * 2, 0, 360);

结果:

在此处输入图像描述

于 2020-01-16T08:45:19.190 回答