C++ Win32 单选按钮背景颜色

发布于 2024-12-06 10:42:05 字数 1430 浏览 5 评论 0原文

所以首先我使用 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_CTLCOLORBTNWM_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你列表最软的妹 2024-12-13 10:42:05

也许一旦您的应用程序以视觉样式运行,您最好处理 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文