Как нарисовать изогнутый текст, используя функции MF C? - PullRequest
1 голос
/ 16 января 2020

Как нарисовать изогнутый текст, используя функции MF C? Я хочу добиться, как это ниже.

enter image description here

Функция DrawText () dr aws текст только по прямой линии, я не знаю, как рисовать изогнутый текст под определенным углом. Пожалуйста, помогите мне.

Спасибо.

1 Ответ

6 голосов
/ 16 января 2020

Вы можете использовать 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 + (или иметь их go из область), прежде чем позвонить GdiplusShutdown.

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

Результат:

enter image description here

ОБНОВЛЕНИЕ:

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

Результат:

enter image description here

...