how to change background color of a static text control (when a button is pushed or in a timer) in mfc?

前端 未结 1 1259
盖世英雄少女心
盖世英雄少女心 2020-12-16 19:08

I know it can be done with OnCtlColor(), but it changes colors when the form is being loaded and the static texts are to be drawn, I want to do it after form is loaded, with

相关标签:
1条回答
  • 2020-12-16 19:52

    No timer is needed. Here I have a bool m_coloured member of the class initialized to false, and toggled in the button press. The OnCtlColor will draw in red or in the system colour depending on the value of m_coloured. Works nicely.

    HBRUSH Cmfcvs2010Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
    
        if (nCtlColor == CTLCOLOR_STATIC && pWnd->GetDlgCtrlID() == IDC_LABEL)
        {
            DWORD d = GetSysColor(COLOR_BTNFACE);
    
            COLORREF normal = RGB(GetRValue(d), GetGValue(d), GetBValue(d));
            COLORREF red = RGB(255, 0, 0);
    
            pDC->SetBkColor(m_coloured ? red : normal);
    
        }
        return hbr;
    }
    
    
    void Cmfcvs2010Dlg::OnBnClickedButton1()
    {
        m_coloured = !m_coloured;
        Invalidate();
    }
    
    0 讨论(0)
提交回复
热议问题