使用自定义绘图在 Win32 API 中设置按钮文本颜色
Set button text color in Win32 API using custom drawing
我想使用通用控件和 Win32 创建某种暗模式按钮 API。
我想使用自定义绘图来设置按钮的背景和文本颜色。
至于背景,它似乎工作正常,但我不知道如何设置文本颜色。
这是我所做的(在 window 处理函数中):
LRESULT CALLBACK WindowHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HBRUSH defaultbrush = NULL;
static HBRUSH hotbrush = NULL;
static HBRUSH selectbrush = NULL;
switch (msg)
{
case WM_CREATE:
{
HWND button = CreateWindowA("Button", "Click Me", WS_VISIBLE | WS_CHILD,
10, 10, 80, 30, hwnd, (HMENU)1, NULL, NULL);
if (!button)
{
MessageBoxA(NULL, "Button Creation Failed!", "Error!", MB_ICONEXCLAMATION);
exit(EXIT_FAILURE);
}
break;
}
case WM_NOTIFY:
{
LPNMHDR some_item = (LPNMHDR)lParam;
if (some_item->idFrom == 1 && some_item->code == NM_CUSTOMDRAW)
{
LPNMCUSTOMDRAW item = (LPNMCUSTOMDRAW)some_item;
if (item->uItemState & CDIS_SELECTED)
{
//Select our color when the button is selected
if (selectbrush == NULL)
selectbrush = CreateSolidBrush(0x383838);
//Create pen for button border
HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
//Select our brush into hDC
HGDIOBJ old_pen = SelectObject(item->hdc, pen);
HGDIOBJ old_brush = SelectObject(item->hdc, selectbrush);
Rectangle(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom);
//Clean up
SelectObject(item->hdc, old_pen);
SelectObject(item->hdc, old_brush);
DeleteObject(pen);
//Here is the problem:
DrawTextA(item->hdc, "Click Me", -1, &item->rc, DT_CENTER | DT_VCENTER);
return CDRF_SKIPDEFAULT;
}
else
{
if (item->uItemState & CDIS_HOT) //Our mouse is over the button
{
//Select our color when the mouse hovers our button
if (hotbrush == NULL)
hotbrush = CreateSolidBrush(0x474747);
HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
HGDIOBJ old_pen = SelectObject(item->hdc, pen);
HGDIOBJ old_brush = SelectObject(item->hdc, hotbrush);
Rectangle(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom);
SelectObject(item->hdc, old_pen);
SelectObject(item->hdc, old_brush);
DeleteObject(pen);
//Here too:
DrawTextA(item->hdc, "Click Me", -1, &item->rc, DT_CENTER | DT_VCENTER);
return CDRF_SKIPDEFAULT;
}
//Select our color when our button is doing nothing
if (defaultbrush == NULL)
defaultbrush = CreateSolidBrush(0x383838);
HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
HGDIOBJ old_pen = SelectObject(item->hdc, pen);
HGDIOBJ old_brush = SelectObject(item->hdc, defaultbrush);
Rectangle(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom);
SelectObject(item->hdc, old_pen);
SelectObject(item->hdc, old_brush);
DeleteObject(pen);
//And also here:
DrawTextA(item->hdc, "Click Me", -1, &item->rc, DT_CENTER | DT_VCENTER);
return CDRF_SKIPDEFAULT;
}
}
return CDRF_DODEFAULT;
break;
}
case WM_DESTROY:
DeleteObject(defaultbrush);
DeleteObject(selectbrush);
DeleteObject(hotbrush);
PostQuitMessage(0);
break;
}
return DefWindowProcA(hwnd, msg, wParam, lParam);
}
我尝试遵循另一个 post 我在 Stack Overflow 上找到的是这个:
How can I change the background color of a button WinAPI C++
但是他们只展示了如何设置背景颜色,而不是文本颜色。
谁能帮我解决这个问题?
您只需要在调用 DrawText()
之前的某处使用 SetTextColor
函数设置文本颜色:
//...
SetTextColor(item->hdc, RGB(0,255,0)); // For green text, but use any COLORREF value.
DrawTextA(item->hdc, "Click Me", -1, &item->rc, DT_CENTER | DT_VCENTER);
return CDRF_SKIPDEFAULT;
您可能还需要将background mode设置为TRANSPARENT
,以保持之前绘制的颜色:
SetBkMode(item-hdc, TRANSPARENT);
我想使用通用控件和 Win32 创建某种暗模式按钮 API。
我想使用自定义绘图来设置按钮的背景和文本颜色。
至于背景,它似乎工作正常,但我不知道如何设置文本颜色。
这是我所做的(在 window 处理函数中):
LRESULT CALLBACK WindowHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HBRUSH defaultbrush = NULL;
static HBRUSH hotbrush = NULL;
static HBRUSH selectbrush = NULL;
switch (msg)
{
case WM_CREATE:
{
HWND button = CreateWindowA("Button", "Click Me", WS_VISIBLE | WS_CHILD,
10, 10, 80, 30, hwnd, (HMENU)1, NULL, NULL);
if (!button)
{
MessageBoxA(NULL, "Button Creation Failed!", "Error!", MB_ICONEXCLAMATION);
exit(EXIT_FAILURE);
}
break;
}
case WM_NOTIFY:
{
LPNMHDR some_item = (LPNMHDR)lParam;
if (some_item->idFrom == 1 && some_item->code == NM_CUSTOMDRAW)
{
LPNMCUSTOMDRAW item = (LPNMCUSTOMDRAW)some_item;
if (item->uItemState & CDIS_SELECTED)
{
//Select our color when the button is selected
if (selectbrush == NULL)
selectbrush = CreateSolidBrush(0x383838);
//Create pen for button border
HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
//Select our brush into hDC
HGDIOBJ old_pen = SelectObject(item->hdc, pen);
HGDIOBJ old_brush = SelectObject(item->hdc, selectbrush);
Rectangle(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom);
//Clean up
SelectObject(item->hdc, old_pen);
SelectObject(item->hdc, old_brush);
DeleteObject(pen);
//Here is the problem:
DrawTextA(item->hdc, "Click Me", -1, &item->rc, DT_CENTER | DT_VCENTER);
return CDRF_SKIPDEFAULT;
}
else
{
if (item->uItemState & CDIS_HOT) //Our mouse is over the button
{
//Select our color when the mouse hovers our button
if (hotbrush == NULL)
hotbrush = CreateSolidBrush(0x474747);
HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
HGDIOBJ old_pen = SelectObject(item->hdc, pen);
HGDIOBJ old_brush = SelectObject(item->hdc, hotbrush);
Rectangle(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom);
SelectObject(item->hdc, old_pen);
SelectObject(item->hdc, old_brush);
DeleteObject(pen);
//Here too:
DrawTextA(item->hdc, "Click Me", -1, &item->rc, DT_CENTER | DT_VCENTER);
return CDRF_SKIPDEFAULT;
}
//Select our color when our button is doing nothing
if (defaultbrush == NULL)
defaultbrush = CreateSolidBrush(0x383838);
HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
HGDIOBJ old_pen = SelectObject(item->hdc, pen);
HGDIOBJ old_brush = SelectObject(item->hdc, defaultbrush);
Rectangle(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom);
SelectObject(item->hdc, old_pen);
SelectObject(item->hdc, old_brush);
DeleteObject(pen);
//And also here:
DrawTextA(item->hdc, "Click Me", -1, &item->rc, DT_CENTER | DT_VCENTER);
return CDRF_SKIPDEFAULT;
}
}
return CDRF_DODEFAULT;
break;
}
case WM_DESTROY:
DeleteObject(defaultbrush);
DeleteObject(selectbrush);
DeleteObject(hotbrush);
PostQuitMessage(0);
break;
}
return DefWindowProcA(hwnd, msg, wParam, lParam);
}
我尝试遵循另一个 post 我在 Stack Overflow 上找到的是这个: How can I change the background color of a button WinAPI C++ 但是他们只展示了如何设置背景颜色,而不是文本颜色。
谁能帮我解决这个问题?
您只需要在调用 DrawText()
之前的某处使用 SetTextColor
函数设置文本颜色:
//...
SetTextColor(item->hdc, RGB(0,255,0)); // For green text, but use any COLORREF value.
DrawTextA(item->hdc, "Click Me", -1, &item->rc, DT_CENTER | DT_VCENTER);
return CDRF_SKIPDEFAULT;
您可能还需要将background mode设置为TRANSPARENT
,以保持之前绘制的颜色:
SetBkMode(item-hdc, TRANSPARENT);