C++ Win32 单选按钮背景颜色
所以首先我使用 Windows API,没有特殊的库。
我使用以下代码创建了一个单选按钮:
g_hRadioButton = CreateWindowEx(0, "BUTTON", "Radio Button",
WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
10, 55, 120, 25, hWnd, (HMENU)RADIOBUTTON, GetModuleHandle(NULL), NULL);
现在我的主窗口背景为黑色,因此我希望文本为白色,背景为透明。
我尝试检查 WM_CTLCOLORBTN
和 WM_CTLCOLORSTATIC
消息。
这是我的代码:
case WM_CTLCOLORBTN:
SetTextColor((HDC)wParam, 0xffffff);
SetBkMode((HDC)wParam, TRANSPARENT);
return (LRESULT)GetStockObject(BLACK_BRUSH);
case WM_CTLCOLORSTATIC:
SetTextColor((HDC)wParam, 0xffffff);
SetBkMode((HDC)wParam, TRANSPARENT);
return (LRESULT)GetStockObject(NULL_BRUSH);
这不起作用,背景仍然是白色的,文本是黑色的。
此外,我还通过链接到 ComCtl32.lib、创建清单等来启用视觉样式。
编辑:
现在尝试处理NM_CUSTOMDRAW
消息。 这是我的代码,但它没有效果,而且我很确定我做错了什么。
case WM_NOTIFY:
{
if (((LPNMHDR)lParam)->code == NM_CUSTOMDRAW)
{
LPNMCUSTOMDRAW nmCD = (LPNMCUSTOMDRAW)lParam;
switch(nmCD->dwDrawStage)
{
case CDDS_PREPAINT:
return CDRF_NOTIFYITEMDRAW;
case CDDS_ITEMPREPAINT:
SetTextColor(nmCD->hdc, 0xffffff);
SetBkColor(nmCD->hdc, 0x000000);
return CDRF_DODEFAULT;
}
}
break;
}
有人至少能指出我正确的方向吗?
So first i'm using the windows API, no special libraries.
I've created a radio button with this code:
g_hRadioButton = CreateWindowEx(0, "BUTTON", "Radio Button",
WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
10, 55, 120, 25, hWnd, (HMENU)RADIOBUTTON, GetModuleHandle(NULL), NULL);
Now I have a black background for the main window, so I would like the text to be white, and the background to be transparent.
I've tried checking both the WM_CTLCOLORBTN
and WM_CTLCOLORSTATIC
messages.
Here's my code:
case WM_CTLCOLORBTN:
SetTextColor((HDC)wParam, 0xffffff);
SetBkMode((HDC)wParam, TRANSPARENT);
return (LRESULT)GetStockObject(BLACK_BRUSH);
case WM_CTLCOLORSTATIC:
SetTextColor((HDC)wParam, 0xffffff);
SetBkMode((HDC)wParam, TRANSPARENT);
return (LRESULT)GetStockObject(NULL_BRUSH);
This doesn't work, the backgrounds still white and the text is black.
Also i've enabled visual styles by linking to ComCtl32.lib, creating the manifest and all that.
EDIT:
Trying to process the NM_CUSTOMDRAW
message now instead.
Here's my code but it's having no effect, and i'm pretty sure im doing something wrong.
case WM_NOTIFY:
{
if (((LPNMHDR)lParam)->code == NM_CUSTOMDRAW)
{
LPNMCUSTOMDRAW nmCD = (LPNMCUSTOMDRAW)lParam;
switch(nmCD->dwDrawStage)
{
case CDDS_PREPAINT:
return CDRF_NOTIFYITEMDRAW;
case CDDS_ITEMPREPAINT:
SetTextColor(nmCD->hdc, 0xffffff);
SetBkColor(nmCD->hdc, 0x000000);
return CDRF_DODEFAULT;
}
}
break;
}
Could someone at least point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许一旦您的应用程序以视觉样式运行,您最好处理 NM_CUSTOMDRAW 按钮控制通知。最初,这些仅适用于常见控件,但相当多的版本也已经以相同的方式扩展了按钮行为。
Perhaps as soon as your application is running with visual styles, you will be better off handling NM_CUSTOMDRAW notification for button control. Originally, these were for common controls only, but quite a few versions are already extending button behavior the same way too.