我想使用 win32 创建一个自定义按钮。经过搜索,我找到了一组方法,包括使用 BS_OWNERDRAW ,我得到了想要的结果,这是当按钮是矩形时,但是当我想创建一个圆形按钮时,我遇到了一个问题,就是出现白色按钮后面的背景,尽管进行了大量研究,但我没有找到摆脱白色背景的正确方法。 我希望你可以帮助我。 这是我使用的代码:
#define UNICODE
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Define window properties variables
const wchar_t g_szClassName[] = L"myWindowClass";
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
// Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, L"Failed to register window class", L"Error", MB_OK);
return 1;
}
// Creating the window
HWND hWnd = CreateWindowEx(0, L"myWindowClass", L"New Window",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
500, 350, NULL, NULL, GetModuleHandle(NULL), NULL);
if (hWnd == NULL)
{
MessageBox(NULL, L"Failed to create window", L"Error", MB_OK);
return 1;
}
// Show the window
ShowWindow(hWnd, SW_SHOW);
// Message loop
while (GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HWND buttonHandle;
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
{
// cretae the button
buttonHandle = CreateWindowEx(0, L"BUTTON", L"OK",
WS_VISIBLE | WS_CHILD | BS_OWNERDRAW,
50, 50, 200, 100, hwnd, (HMENU)1, NULL, NULL);
break;
}
case WM_PAINT:
{
// fill the window with a solid color
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HBRUSH hBrush = CreateSolidBrush(RGB(30, 30, 30));
FillRect(hdc, &ps.rcPaint, hBrush);
DeleteObject(hBrush);
EndPaint(hwnd, &ps);
return 0;
}
case WM_DRAWITEM:
{
LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
// Get button rectangle
RECT buttonRect = lpdis->rcItem;
// Create Gdiplus::Graphics object from device context
Gdiplus::Graphics graphics(lpdis->hDC);
graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias); // Set anti-alias mode for smoother edges
// Define button color with alpha channel (transparency)
Gdiplus::Color buttonColor = Gdiplus::Color(255, 170, 70, 70);
// Create solid brush for button color
Gdiplus::SolidBrush brush(buttonColor);
int x = buttonRect.left;
int y = buttonRect.top;
int width = buttonRect.right - buttonRect.left;
int height = buttonRect.bottom - buttonRect.top;
// Draw a rounded rectangle
int cornerRadius = 40;
Gdiplus::GraphicsPath path;
path.AddArc(x, y, cornerRadius, cornerRadius, 180, 90);
path.AddArc(x + width - cornerRadius, y, cornerRadius, cornerRadius, 270, 90);
path.AddArc(x + width - cornerRadius, y + height - cornerRadius, cornerRadius, cornerRadius, 0, 90);
path.AddArc(x, y + height - cornerRadius, cornerRadius, cornerRadius, 90, 90);
path.CloseFigure();
graphics.FillPath(&brush, &path);
break;
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
结果如下
您的按钮仍然具有默认的矩形形状,只是没有将其绘制为矩形。
您可以使用 CreateRoundRectRgn() 和 SetWindowRgn() 为按钮提供真正的非矩形形状。或者,在 Windows 8+ 上,您可以为按钮指定 WS_ES_LAYERED 样式,然后使用 SetLayeredWindowAttributes() 或 UpdateLayeredWindow() 使按钮在您不想绘制的区域具有透明度。