处理 WM_CTLCOLOR 消息
HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (nCtlColor==CTLCOLOR_STATIC)
{
pDC-> SetBkMode(TRANSPARENT);
return m_backHbrush;
}
return hbr;
}
m_backHbrush定义为成员变量,HBRUSH类型,在OnInitDialog中初始化:
m_backHbrush=CreateSolidBrush(RGB(0,255,255));
void CXXXDlg::OnBnClickedBtnChange(){ // get window handle of your static control CStatic* pStatic = (CStatic*)(GetDlgItem(IDC_STATIC_VARIETY)); // init random generator srand(unsigned(time(NULL))); // release HBRUSH object last time ::DeleteObject(m_hbrMyBK); // create new brush and text color m_hbrMyBK = CreateSolidBrush(RGB(rand() % 256, rand() % 256, rand() % 256)); m_crlMyText = RGB(rand() % 256, rand() % 256, rand() % 256); // force to repaint your static control pStatic->Invalidate(TRUE);}HBRUSH CXXXDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor){ HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // if your static control... if(pWnd->GetDlgCtrlID() == IDC_STATIC_VARIETY){ pDC->SetBkMode(TRANSPARENT); pDC->SetTextColor(m_crlMyText); hbr = m_hbrMyBK; } return hbr;}