更改win32的单选按钮文本颜色 [英] Changing win32's radio button text color

查看:101
本文介绍了更改win32的单选按钮文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更改颜色时,我听WM_CTLCOLORSTATIC并采取相应的行动:

Upon color change, i listen to WM_CTLCOLORSTATIC and act accordingly:

LRESULT ProcessWindowMessage(_In_ HWND hWnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam)
    {
        switch (uMsg)
        {
        case WM_CTLCOLORSTATIC:
            LRESULT lBrush = ::DefWindowProc(hWnd, uMsg, wParam, lParam); // get default brush used so far
            ::SetBkMode((HDC)wParam, TRANSPARENT);
            ::SetTextColor((HDC)wParam, RGB(m_color.red, m_color.green, m_color.blue));
            return lBrush;
        }
    }

这对于常规的静态文本(如标签等)效果很好,但对常规的单选按钮无效.

This works well with regular static-texts: labels and so on, but has no effect on regular radio buttons.

在调试过程中,我尝试收听:

During my debugging attempts, I've tried listening to:

  1. WM_DRAWITEM-没有收到任何事件
  2. WM_CTLCOLORBTN-仅接收常规按钮的事件(确定/取消)
  3. WM_CTLCOLOREDIT-没有收到任何事件.

我将子类化到另一个窗口,该窗口不是由我生成/创建的,而是由我的过程构造的.

I'm subclassing to another window not generated / created by me, but constructed by my process.

推荐答案

@igal k表示SetWindowTheme不起作用.由于注释不足以提供示例代码.我将其发布为答案.

@igal k said SetWindowTheme does not work. Since the comment is not enough for the sample code. I post it as an answer.

首先是结果.

代码:

OnInitDialog:
    ::SetWindowTheme(GetDlgItem(IDC_RADIO1)->GetSafeHwnd(), L"wstr", L"wstr");

HBRUSH CMFCApplication1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    // Call the base class implementation first! Otherwise, it may 
    // undo what we're trying to accomplish here.
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // Are we painting the IDC_MYSTATIC control? We can use 
    // CWnd::GetDlgCtrlID() to perform the most efficient test. 
    if (pWnd->GetDlgCtrlID() == IDC_RADIO1)
    {
        // Set the text color to red
        pDC->SetTextColor(RGB(255, 0, 0));

        // Set the background mode for text to transparent  
        // so background will show thru.
        pDC->SetBkMode(TRANSPARENT);

        // Return handle to our CBrush object
        hbr = m_brush;
    }

    return hbr;
}

这篇关于更改win32的单选按钮文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆