要对控件进行绘制,实现自定义的圆角边框和按钮颜色,你可以创建一个函数,并提供两个颜色参数来设置边框颜色和按钮颜色。以下是一个示例函数的代码:

#include <windows.h>
#include <gdiplus.h>

using namespace Gdiplus;

void DrawCustomControl(HWND hwnd, COLORREF borderColor = RGB(0, 0, 0), COLORREF buttonColor = RGB(255, 255, 255)) {
    HDC hdc = GetDC(hwnd);
    RECT rect;
    GetClientRect(hwnd, &rect);

    // 绘制背景色
    HBRUSH hBrush = CreateSolidBrush(buttonColor);
    FillRect(hdc, &rect, hBrush);
    DeleteObject(hBrush);

    // 绘制圆角边框
    Graphics graphics(hdc);
    Pen pen(borderColor, 2);
    graphics.SetSmoothingMode(SmoothingModeAntiAlias);
    graphics.DrawRoundRect(&pen, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 10, 10);

    ReleaseDC(hwnd, hdc);
}

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

            // 调用自定义绘制函数
            DrawCustomControl(hwnd, RGB(255, 0, 0), RGB(0, 0, 255));

            EndPaint(hwnd, &ps);
            return 0;
        }
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

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

    // 创建窗口
    WNDCLASSEX wc = {0};
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszClassName = L"WindowClass";
    RegisterClassEx(&wc);

    HWND hwnd = CreateWindowEx(0, L"WindowClass", L"Window", WS_OVERLAPPEDWINDOW,
                               CW_USEDEFAULT, CW_USEDEFAULT, 500, 300,
                               NULL, NULL, hInstance, NULL);
    ShowWindow(hwnd, nCmdShow);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // 释放 GDI+
    GdiplusShutdown(gdiplusToken);

    return (int)msg.wParam;
}

在上述代码中,DrawCustomControl 函数使用 GDI+ 绘制自定义的控件外观。在函数中,我们首先获取控件的设备上下文(HDC),然后利用 GDI 函数和 GDI+ 对象进行绘制。

在示例代码的 WM_PAINT 消息中,我们调用了 DrawCustomControl 函数,并指定了边框颜色和按钮颜色。你可以根据需要修改这些颜色参数。

你可以将 DrawCustomControl 函数放在你的程序中,然后在需要绘制自定义控件的地方调用该函数,并传入相应的颜色参数。

希望这个示例能帮助到你!如果你有任何问题,请随时提问。

C++ 自定义控件外观:圆角边框和按钮颜色

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

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