首页 > 解决方案 > 在 RECT 中自动创建 DrawText 的函数

问题描述

我正在编写 WinAPI 应用程序,并尝试编写一个函数,该函数将为我自动创建用于文本输入的矩形(使用 Font、BkMode、TextColor),然后由 WM_PAINT 调用的 DrawContent 函数调用该函数。在我打电话后,文字没有出现ELW::AddTextControl

LRESULT CALLBACK ELW::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        case WM_PAINT:
            {
            ELW::DrawContent(hWnd, hInst, hdc);
            }
            break;

        case WM_CLOSE:
            {
            DestroyWindow(hWnd);
            UnregisterClass(CLASS_NAME, hInst);
            }
            break;

        case WM_SYSCOMMAND:
            {
            if (wParam == SC_CLOSE);
            break;
            }
    }

    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

// Content drawing

void ELW::DrawContent(HWND hWnd, HINSTANCE hInst, HDC hdc)
{
    PAINTSTRUCT ps;
    hdc = BeginPaint(hWnd, &ps);

    RECT testowyRectangle = { 0, 1 , 20 , 30 };
    FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_GRAYTEXT + 1));
    ELW::AddTextControl("Testowy test", 80, 80, 100, 100, CUSTOM_COLOR_YELLOW_COLORREF, 10, "Euroscope", hdc);


    EndPaint(hWnd, &ps);

};

void ELW::AddTextControl(const char* text, int xPos, int yPos, int xSize, int ySize, COLORREF rgb, int fontSize, const char* fontName, HDC hdc)
{
    RECT TextRectangle = { xPos, yPos, ( xPos + xSize ) , ( yPos + ySize ) }; // Left, Top, Right, Bottom
    
    if (rgb == NULL)
        rgb = RGB(0, 0, 0);
    if (fontSize == NULL)
        fontSize = 10;
    if (fontName == NULL)
        fontName = "Euroscope";


    HFONT hFont = CreateFont(fontSize, 8, 0, 0, 600, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT(fontName));
    SelectObject(hdc, hFont);
    SetBkMode(hdc, TRANSPARENT);
    SetTextColor(hdc, rgb);
    DrawText(hdc, TEXT(text), strlen(text), &TextRectangle, DT_CENTER | DT_NOCLIP);
}

标签: c++winapi

解决方案


发现错误: strlen(text) 应该是 strlen(TEXT(text))


推荐阅读