Changing win32's radio button text color

依然范特西╮ 提交于 2019-12-11 19:38:26

问题


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 - doesn't receive any events
  2. WM_CTLCOLORBTN - receive events only for regular push buttons( OK / CANCEL)
  3. WM_CTLCOLOREDIT - doesn't receive any events.

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


回答1:


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

First the result.

Code:

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;
}



回答2:


If you are interested to draw the full radio button, what you have to do is set a custom window proc for the radio button.In that proc you get the WM_PAINT and WM_ERASEBKGND messages. In the erase background you can just fill the background color for the control.

In WM_PAINT you do the drawing of the control , get the window text of the control and do a DrawText with color set to whatever you need using SetTextColor

Draw an image of radio (circle and a dot inside).You can get all the state variables of the control and draw according to it, like whether its selected it or not. Whenever you need a repaint, ie new text is set, just call invalidateRect to force a repaint..



来源:https://stackoverflow.com/questions/31854015/changing-win32s-radio-button-text-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!