将字符串放入指定的矩形内

Fit string inside specified rectangle

本文关键字:字符串      更新时间:2023-10-16

我有需要在矩形内绘制的字符串。

问题在于,有时绳子可能太大而无法放入其中。

如何调整字体大小以使字符串适合内部?

我已经阅读了GDI的文档,但一无所获。我仍然不停地在互联网上搜索,希望能找到一些东西或得到我自己的想法......

GDI+也是一种选择...

以下代码是为了响应用户Jonathan Potter的评论而发布的:

#include <windows.h>
#include <windowsx.h>   
#include <CommCtrl.h>
#include <stdio.h>      // swprintf_s()
#include <math.h>
#include <gdiplus.h>
#include <string>
using namespace Gdiplus;
// enable Visual Styles
#pragma comment( linker, "/manifestdependency:"type='win32' 
                         name='Microsoft.Windows.Common-Controls' version='6.0.0.0' 
                         processorArchitecture='*' publicKeyToken='6595b64144ccf1df' 
                         language='*'"")
// link with Common Controls library
#pragma comment(lib, "comctl32.lib") 
#pragma comment(lib, "GdiPlus.lib")
//global variables
HINSTANCE hInst;
// main window procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_PAINT:
    {
        PAINTSTRUCT ps = { 0 };
        RECT rcClient = { 0 };
        HDC hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rcClient);
        int pageWidth = rcClient.right - rcClient.left,
            pageHeight = rcClient.bottom - rcClient.top;
        HFONT font = NULL, oldFont = NULL;
        // target rectangle, text should fit inside
        Rectangle(hdc, 0, 0, pageWidth / 4, pageHeight / 10);
        SIZE sz;
        GetTextExtentPoint32(hdc, L"This is very long string that might not fit into specified rectangle",
            lstrlen(L"This is very long string that might not fit into specified rectangle"), &sz);
        if (sz.cx > (pageWidth / 4))
        {
            // get current font
            LOGFONT lf;
            GetObject(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
            // scale it
            lf.lfHeight = MulDiv(lf.lfHeight, (pageWidth / 4), sz.cx);
            font = CreateFontIndirect(&lf);
            oldFont = SelectFont(hdc, font);
        }
        SetBkMode(hdc, TRANSPARENT);
        SetTextColor(hdc, RGB(255, 0, 0));
        // draw text in test rectangle 
        RECT rcText = { 0 };
        rcText.left = 0;
        rcText.top = 0;
        rcText.right = pageWidth / 4;
        rcText.bottom = pageHeight / 10;
        DrawTextEx(hdc,
            L"This is very long string that might not fit into specified rectangle",
            wcslen(L"This is very long string that might not fit into specified rectangle"),
            &rcText, DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_NOCLIP, NULL);
        if (font != NULL)
        {
            SelectFont(hdc, oldFont);
            DeleteFont(font);
        }
        EndPaint(hwnd, &ps);
    }
        return 0L;
    case WM_SIZE:
    {
        InvalidateRect(hwnd, NULL, TRUE);
    }
        return 0L;
    case WM_CLOSE:
        ::DestroyWindow(hwnd);
        return 0L;
    case WM_DESTROY:
    {
        ::PostQuitMessage(0);
    }
        return 0L;
    default:
        return ::DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
// WinMain
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
    int nCmdShow)
{
    // store hInstance in global variable for later use
    hInst = hInstance;
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
    // register main window class
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInst;
    wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = L"Main_Window";
    wc.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
    if (!RegisterClassEx(&wc))
    {
        MessageBox(NULL, L"Window Registration Failed!", L"Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    // initialize common controls
    INITCOMMONCONTROLSEX iccex;
    iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    iccex.dwICC = ICC_LISTVIEW_CLASSES | ICC_STANDARD_CLASSES;
    InitCommonControlsEx(&iccex);
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    // create main window
    hwnd = CreateWindowEx(0, L"Main_Window", L"Autofit text inside rectangle",
        WS_OVERLAPPEDWINDOW, 50, 50, 200, 200, NULL, NULL, hInstance, 0);
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    while (GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    GdiplusShutdown(gdiplusToken);
    return Msg.wParam;
}

您正在寻找 DrawText:

int DrawText(_In_     HDC     hDC,
             _Inout_  LPCTSTR lpchText,
             _In_     int     nCount,
             _Inout_  LPRECT  lpRect,
             _In_     UINT    uFormat
            );

指定矩形,它将确保文本不会绘制在该矩形之外。 如果您需要根据文本和当前所选字体计算矩形,它还具有DT_CALCRECT标志。 或者,您可以使用 DT_END_ELLIPSISDT_PATH_ELLIPSISDT_WORD_ELLIPSIS 标志截断添加省略号的文本绘图,以便用户可以查看文本何时比矩形长。

理论上,这样的东西应该可以工作,但我还没有测试过。添加适当的错误检查等。

SIZE sz;
GetTextExtentPoint32(hDC, pszMyString, lstrlen(pszMyString), &sz);
if (sz.cx > iMyMaximumWidth)
{
    // get current font
    LOGFONT lf;
    GetObject(GetCurrentObject(hDC, OBJ_FONT), sizeof(lf), &lf);
    // scale it
    lf.lfHeight = MulDiv(lf.lfHeight, iMyMaximumWidth, sz.cx);
    HFONT hNewFont = CreateFontIndirect(&lf);
    .. use hNewFont to render string, remember to delete it when done
}