以下是使用GDI+的C++代码示例,用于绘制文本并在超过指定像素长度时截断并将剩余部分输出到下一行:

#include <Windows.h>
#include <string>
#include <gdiplus.h>

using namespace Gdiplus;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // 初始化GDI+
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    // 创建窗口
    WNDCLASSEX wcex;
    ZeroMemory(&wcex, sizeof(WNDCLASSEX));
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.hInstance = hInstance;
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.lpszClassName = L"GDIPlusDrawStringExample";
    RegisterClassEx(&wcex);

    HWND hWnd = CreateWindow(wcex.lpszClassName, L"GDI+ DrawString Example", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, hInstance, NULL);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    // 消息循环
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // 释放GDI+
    GdiplusShutdown(gdiplusToken);

    return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);

        // 创建GDI+绘图对象
        Graphics graphics(hdc);

        // 设置绘图参数
        Font font(L"Arial", 12);
        SolidBrush brush(Color::Black);
        PointF origin(10.0f, 10.0f);

        std::wstring text = L"This is a sample text that will be truncated if it exceeds 200 pixels in length.";

        // 绘制原始文本
        graphics.DrawString(text.c_str(), -1, &font, origin, &brush);

        // 获取文本像素长度
        SizeF textSize;
        graphics.MeasureString(text.c_str(), -1, &font, origin, &textSize);

        // 如果文本超过200像素长度,则进行截断
        if (textSize.Width > 200)
        {
            std::wstring truncatedText;
            int lastSpaceIndex = text.find_last_of(L' ', text.size() - 1);

            if (lastSpaceIndex != std::wstring::npos)
            {
                truncatedText = text.substr(0, lastSpaceIndex);
                origin.Y += textSize.Height; // 移动到下一行
                graphics.DrawString(truncatedText.c_str(), -1, &font, origin, &brush);

                std::wstring remainingText = text.substr(lastSpaceIndex + 1);
                origin.Y += textSize.Height; // 移动到下一行
                graphics.DrawString(remainingText.c_str(), -1, &font, origin, &brush);
            }
        }

        EndPaint(hWnd, &ps);
        break;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }

    return 0;
}

该代码创建一个窗口,并在窗口客户区绘制指定的文本。如果文本的像素长度超过200,它将从最后一个空格处截断,并将剩余部分输出到下一行

写个C++代码使用GDI+的DrawString输出文本当文本像素长度超过200时从文本的前一个空格处截断剩下部分输出到下面

原文地址: https://www.cveoy.top/t/topic/i1sZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录