请问如何改变一个static控件的背景颜色?

解决方案 »

  1.   

    MSDN中的例子
    HBRUSH CZilchDlg::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_MYSTATIC)
       {
          // 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.   

    CStatic *st = (CStatic *)GetDlgItem(you static ID);
    st->SetBkColor(RGB(255,0,0));
      

  3.   

    //其中m_brush是必须在头文件中定义,并在此前已创建的CBrush 对象。
    HBRUSH CLBView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor); if(pWnd->GetDlgCtrlID()==IDC_MYSTATIC)
    {
    pDC->SetBkColor(...);
    hbr=m_brush;
    }
    return hbr;
    }
    //上面那个if(pWnd->GetDlgCtrlID() == IDC_MYSTATIC)在VC6下大概只能这样:
    if(pWnd == GetDlgItem( IDC_MYSTATIC ))
      

  4.   

    我用过了以上的办法,好像不对,我发现用以上的办法只能使ID号为IDC_STATIC的变化,而我想使ID号为IDC_GASCON的变化。怎么做?
    我这样做,不行
    HBRUSH CSamplesView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO: Change any attributes of the DC here
    pWnd=GetDlgItem(IDC_GASCON);
    if(pWnd==GetDlgItem(IDC_GASCON))
    {
    pDC->SetBkColor(RGB(255,255,255));
    }
    // TODO: Return a different brush if the default is not desired
    return hbr;
    }
      

  5.   

    定义成员变量
    CBrush brush;
    初始化
    brush.CreateSolidBrush(RGB(0,0,255));
    HBRUSH CSamplesView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO: Change any attributes of the DC here
    pWnd=GetDlgItem(IDC_GASCON);
    if(pWnd==GetDlgItem(IDC_GASCON))
    {
       retrun (HBRUSH)brush.m_hObject ;
    }
    // TODO: Return a different brush if the default is not desired
    return hbr;
    }
      

  6.   

    我上面的不能使有子的部分改变颜色
    补充:用SetBkColor设成与画刷相同的RGB颜色HBRUSH CSamplesView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO: Change any attributes of the DC here
    pWnd=GetDlgItem(IDC_GASCON);
    if(pWnd==GetDlgItem(IDC_GASCON))
    {
                pDC->SetBkColor(RGB(0,0,255));
       retrun (HBRUSH)brush.m_hObject ;
    }
    // TODO: Return a different brush if the default is not desired
    return hbr;
    }
      

  7.   

    在容器里响应WM_CTLCOLOR消息。
    或在CStatic的派生类中响应WM_CTLCOLOR_REFLACT反射消息。