如何制作具有自定义宽度和颜色的静态控件?

How do I make a static control with custom width and color?

要更改静态控件的颜色和宽度,我确实处理了 WM_NCPAINT 消息,但它无法正常工作:

  1. 文本显示不正确。我应该在这条消息中画出来吗?当我调用 SetWindowText() 时,新文本已正确设置,但问题 #2 仍然存在。

  2. 在调用SetWindowText()后,文本设置好了,但是边框不同,它比我在WM_NCPAINT消息中画的要小。

我是这样处理的 WM_NCPAINT:

case WM_NCPAINT:
{
    PAINTSTRUCT ps;
    HDC dc = BeginPaint(hwnd, &ps);
    RECT rt = {0};
    GetClientRect(hwnd, &rt);
    HPEN pen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
    HPEN holdPen = SelectObject(dc, pen);
    DrawBorder(dc, &rt);
    SelectObject(dc, holdPen);
    EndPaint(hwnd, &ps);
    DeleteObject(pen);
    pen = NULL;
    return 0;
}

当应用程序启动时,静态控件边框看起来像我在 WM_NCPAINT:

中绘制的那个

但是一旦我调用 SetWindowText(),例如,通过点击按钮,它看起来像这样:

我错过了什么?

完整代码如下:

#pragma comment(lib, "user32.lib")
#pragma comment(lib, "Comctl32.lib")
#pragma comment(lib, "Gdi32.lib")
    
#define WIN32_LEAN_AND_MEAN
#define UNICODE
#define _UNICODE
    
#include <windows.h>
#include <Commctrl.h>
#include <crtdbg.h>
#include <strsafe.h>
#include <string.h>
#include <assert.h>
    
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void EnableVisualStyles2(void);
LRESULT CALLBACK ButtonProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void SetDefaultFont(HWND hwnd);
void DrawLine(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2);
void DrawBorder(HDC hdc, RECT *rect);
HFONT getSystemDefaultFont(void);
    
WNDPROC oldButtonProc;
HINSTANCE ghInstance;
HFONT hDefaultFont;
HWND btn, btn2;
    
enum 
{
    BTN_ID = 10,
    BTN2_ID,
};
    
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PWSTR pCmdLine, int nCmdShow)
{
    MSG  msg = {0};
    HWND hwnd;
    WNDCLASSW wc = {0};

    wc.lpszClassName = L"Window";
    wc.hInstance     = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc   = WndProc;
    wc.hCursor = LoadCursor(0, IDC_ARROW);

    EnableVisualStyles2();
    if(!RegisterClass(&wc)) {
        return -1;
    }

    int width = 540;
    int height = 460;
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    int cx = (screenWidth - width) / 2;
    int cy = (screenHeight - height) / 2;
    hwnd = CreateWindowW(wc.lpszClassName, L"Window",
                        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                        cx, cy, width, height, NULL, NULL, 
                        hInstance, NULL);
    ghInstance = hInstance;

    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!IsDialogMessage(hwnd, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    DeleteObject(hDefaultFont);
    return (int) msg.wParam;
}
    
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
        {
            btn =
                CreateWindow(L"static", L"+",
                    WS_VISIBLE | WS_CHILD | WS_TABSTOP |
                    SS_NOTIFY | SS_CENTER | SS_CENTERIMAGE,
                    15, 25, 50, 50,
                    hwnd, (HMENU) BTN_ID, NULL, NULL);
            SetDefaultFont(btn);
            oldButtonProc = (WNDPROC) SetWindowLongPtr(btn,
                                        GWLP_WNDPROC,
                                        (LONG_PTR) ButtonProc);
            btn2 = 
                CreateWindow(L"Button", L"Click me!",
                            WS_VISIBLE | WS_CHILD | WS_TABSTOP,
                            15, 100, 70, 25,
                            hwnd, (HMENU) BTN2_ID,
                            NULL, NULL);
            SetDefaultFont(btn2);
        }
        break;
    
        case WM_COMMAND:
        {
            switch(LOWORD(wParam))
            {
                case BTN_ID:
                {
                    //InvalidateRect(btn, NULL, TRUE);
                    
                    if(IsWindowVisible(btn2))
                    {
                        SetWindowText(btn, L"+");
                        ShowWindow(btn2, SW_HIDE);
                    }
                    else
                    {
                        SetWindowText(btn, L"-");
                        ShowWindow(btn2, SW_SHOW);
                    }
                }
                break;
    
                case BTN2_ID:
                {
                    InvalidateRect(btn, NULL, TRUE);
                    //SetWindowText(btn, L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0");
                }
                break;
            }
        }
        break;    
        
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    
    return DefWindowProc(hwnd, msg, wParam, lParam);
}
    
LRESULT CALLBACK ButtonProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_NCPAINT:
        {
            PAINTSTRUCT ps;
            HDC dc = BeginPaint(hwnd, &ps);
            RECT rt = {0};
            GetClientRect(hwnd, &rt);
            HPEN pen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
            HPEN holdPen = SelectObject(dc, pen);
            DrawBorder(dc, &rt);
            SelectObject(dc, holdPen);
            EndPaint(hwnd, &ps);
            DeleteObject(pen);
            pen = NULL;
            return 0;
        }
    }
    
    return CallWindowProc(oldButtonProc, hwnd, msg, wParam, lParam);
}
    
void EnableVisualStyles2(void)
{
    TCHAR dir[MAX_PATH] = {0};
    GetSystemDirectory(dir, sizeof(dir) / sizeof(*dir));
    
    ACTCTX actCtx = {0};
    actCtx.cbSize = sizeof(ACTCTX);
    actCtx.dwFlags =  ACTCTX_FLAG_RESOURCE_NAME_VALID |
                      ACTCTX_FLAG_SET_PROCESS_DEFAULT |
                      ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;
    actCtx.lpSource = L"shell32.dll";
    actCtx.lpAssemblyDirectory = dir;
    actCtx.lpResourceName = (LPCTSTR) 124;
    ULONG_PTR cookie = FALSE;
    HANDLE h = CreateActCtx(&actCtx);
    assert(h != INVALID_HANDLE_VALUE);
    assert(ActivateActCtx(h, &cookie));
}
    
HFONT getSystemDefaultFont(void)
{
    if(!hDefaultFont)
    {
        // get system default font
        NONCLIENTMETRICS ncm;
        ncm.cbSize = sizeof(NONCLIENTMETRICS);
        SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0);
        hDefaultFont = CreateFontIndirect(&ncm.lfMessageFont);
    }
    return hDefaultFont;
}
    
void SetDefaultFont(HWND hwnd)
{
    SendMessage(hwnd, WM_SETFONT, (WPARAM)getSystemDefaultFont(), FALSE);
}
    
void DrawLine(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2)
{
    MoveToEx(hdc, x1, y1, NULL);
    LineTo(hdc, x2, y2);
}
    
void DrawBorder(HDC hdc, RECT *rect)
{
    DrawLine(hdc, rect->left, rect->top, rect->left, rect->bottom);
    DrawLine(hdc, rect->left, rect->top, rect->right, rect->top);
    DrawLine(hdc, rect->right, rect->top, rect->right, rect->bottom);
    DrawLine(hdc, rect->left, rect->bottom, rect->right, rect->bottom);
}

您的静态控件没有透明背景,所以当您调用SetWindowText函数时,静态控件会部分覆盖您绘制的外部线条。

您可以在WndProc中添加以下代码:

case WM_CTLCOLORSTATIC:
{
    HDC hdcStatic = (HDC)wParam;
    SetBkMode(hdcStatic, TRANSPARENT);
    return (LRESULT)GetStockObject(NULL_BRUSH);
}

但是这段代码告诉静态控件在没有背景颜色的情况下绘制文本,而不是重新绘制背景。所以新的文字是在旧的文字之上绘制的,而不是在新的文字之上background.You可以参考这个thread尝试解决

当然也可以在画直线的时候把直线的边界延长(不需要处理WM_CTLCOLORSTATIC消息),如下代码所示:

void DrawBorder(HDC hdc, RECT* rect)
{
    DrawLine(hdc, rect->left - 3, rect->top - 3, rect->left - 3, rect->bottom + 3) ;
    DrawLine(hdc, rect->left - 3, rect->top - 3, rect->right + 3, rect->top - 3);
    DrawLine(hdc, rect->right + 3, rect->top - 3, rect->right + 3, rect->bottom + 3);
    DrawLine(hdc, rect->left - 3, rect->bottom + 3, rect->right + 3, rect->bottom + 3);
}

这是修改后的效果: