使用GDI+在Win32对话框上绘制自定义控件

本教程演示如何使用GDI+在Win32对话框上绘制自定义控件。我们将创建一个简单的对话框应用程序,并在其中绘制一个带有圆角边框和自定义背景色的按钮。

**代码:**cpp#include <windows.h>#include <gdiplus.h>#include 'resource.h'#pragma comment(lib, 'gdiplus.lib')

using namespace Gdiplus;

HWND ghwnd = GetForegroundWindow();

void DrawCustomControl(HWND hwnd, COLORREF borderColor = RGB(125, 191, 80), COLORREF buttonColor = RGB(126, 70, 20)){ HDC hdc = GetDC(hwnd); RECT rect; GetClientRect(hwnd, &rect);

// 创建内存绘图上下文    HDC memDC = CreateCompatibleDC(hdc);    HBITMAP memBitmap = CreateCompatibleBitmap(hdc, rect.right - rect.left, rect.bottom - rect.top);    HBITMAP hOldBitmap = (HBITMAP)SelectObject(memDC, memBitmap);

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

// 绘制圆角矩形边框    HPEN hPen = CreatePen(PS_SOLID, 2, borderColor);    SelectObject(memDC, hPen);    RoundRect(memDC, rect.left, rect.top, rect.right, rect.bottom, 10, 10);    DeleteObject(hPen);

// 将绘制结果复制到窗口的设备上下文    BitBlt(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, memDC, 0, 0, SRCCOPY);

// 清理资源    SelectObject(memDC, hOldBitmap);    DeleteObject(memBitmap);    DeleteDC(memDC);

ReleaseDC(hwnd, hdc);}

INT_PTR CALLBACK DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE;

case WM_COMMAND:                break;

case WM_CLOSE:        EndDialog(hWnd, IDCANCEL);        return (INT_PTR)TRUE;    }    return (INT_PTR)FALSE;}

BOOL InitInstance(HINSTANCE hInstance){ // 弹出IDC_GC对话框 ghwnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), nullptr, DialogProc); if (!ghwnd) { // 对话框创建失败 return FALSE; } ShowWindow(ghwnd, true);

// 初始化 GDI+    GdiplusStartupInput gdiplusStartupInput;    ULONG_PTR gdiplusToken;    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

DrawCustomControl(GetDlgItem(ghwnd, IDOK));

GdiplusShutdown(gdiplusToken);    MSG msg;    while (GetMessage(&msg, nullptr, 0, 0))    {        if (!IsDialogMessage(ghwnd, &msg))        {            TranslateMessage(&msg);            DispatchMessage(&msg);        }    }

return FALSE;}

ATOM MyRegisterClass(HINSTANCE hInstance){ WNDCLASSEXW wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;    wcex.lpfnWndProc = DefWindowProc;    wcex.cbClsExtra = 0;    wcex.cbWndExtra = 0;    wcex.hInstance = hInstance;    wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);    wcex.lpszMenuName = nullptr;    wcex.lpszClassName = L'windows';    wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_ICON1));

return RegisterClassExW(&wcex);}

int main(){ // 初始化 GDI+ GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(ghwnd, GWLP_HINSTANCE);    MyRegisterClass(hInstance);    if (InitInstance(hInstance))    {        return FALSE;    }    return 0;}

步骤:

  1. 创建Win32项目: 在Visual Studio中创建一个新的Win32项目。2. 添加GDI+依赖项: 在项目属性中链接'gdiplus.lib'库。3. 创建对话框资源: 在资源视图中创建一个新的对话框资源,并为其分配一个ID(例如,IDD_DIALOG1)。4. 添加控件: 将一个按钮控件添加到对话框资源中,并为其分配一个ID(例如,IDOK)。5. 编写代码: 使用提供的代码作为起点,并根据需要进行修改。6. 构建和运行: 构建并运行应用程序,您应该会看到一个带有自定义控件的对话框。

注意:

  • 确保在使用GDI+函数之前初始化GDI+。* 使用完GDI+对象后,请务必释放它们以防止内存泄漏。* 可以通过修改'DrawCustomControl'函数来自定义控件的外观。

希望本教程对您有所帮助!

使用GDI+在Win32对话框上绘制自定义控件

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

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