我在《vc技术内幕》中看到说
只要在OnCtlColor()中添加如下代码
if(nCtlColor == CTLCOLOR_BTN)
{
   pDC->SetBkColor(SetBkColor(255,0,0));
   return m_hRedBrush;  //已在OnInitDialog()中初始化
}可就是不行啊,CTLCOLOR_DLG就可以修改dialog底色,这是为什么?
请高手指点!

解决方案 »

  1.   

    //举个例子,本例子可以将一个被禁止的Edit框的背景改为白色(本为灰色),注意这个函数是重载WM_CTLCOLOR消息时的处理函数
    HBRUSH CMyDataView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CRecordView::OnCtlColor(pDC, pWnd, nCtlColor);
    // TODO: Change any attributes of the DC here
    if(pWnd->GetDlgCtrlID()==IDC_MYDATA_PDNUM)
    {
    pDC->SetBkMode(TRANSPARENT);//这样还可把文字背景设为透明!
    hbr = CreateSolidBrush(RGB(255,255,255));
    }
    if(pWnd->GetDlgCtrlID()==IDC_MYDATA_JSNAME)
    {
    pDC->SetBkMode(TRANSPARENT);//这样还可把文字背景设为透明!
    hbr = CreateSolidBrush(RGB(255,255,255));
    }
    if(pWnd->GetDlgCtrlID()==IDC_MYDATA_YNAME)
    {
    pDC->SetBkMode(TRANSPARENT);//这样还可把文字背景设为透明!
    hbr = CreateSolidBrush(RGB(255,255,255));
    }
    if(pWnd->GetDlgCtrlID()==IDC_MYDATA_BNUM)
    {
    pDC->SetBkMode(TRANSPARENT);//这样还可把文字背景设为透明!
    hbr = CreateSolidBrush(RGB(255,255,255));
    }
    if(pWnd->GetDlgCtrlID()==IDC_MYDATA_GRADE)
    {
    pDC->SetBkMode(TRANSPARENT);//这样还可把文字背景设为透明!
    hbr = CreateSolidBrush(RGB(255,255,255));
    }
    if(pWnd->GetDlgCtrlID()==IDC_MYDATA_BJ)
    {
    pDC->SetBkMode(TRANSPARENT);//这样还可把文字背景设为透明!
    hbr = CreateSolidBrush(RGB(255,255,255));
    }
    if(pWnd->GetDlgCtrlID()==IDC_KCNAME_KCNAME)
    {
    pDC->SetBkMode(TRANSPARENT);//这样还可把文字背景设为透明!
    hbr = CreateSolidBrush(RGB(255,255,255));
    }
    if(pWnd->GetDlgCtrlID()==IDC_MYDATA_KCNUM)
    {
    pDC->SetBkMode(TRANSPARENT);//这样还可把文字背景设为透明!
    hbr = CreateSolidBrush(RGB(255,255,255));
    }
    // TODO: Return a different brush if the default is not desired
    return hbr;
    }
      

  2.   

    我试了一下,还是不行啊!!!程序是base dialog的
    按钮的颜色没变化的
      

  3.   

    最彻底的方法,是用Subclass的方法。
    http://www.bestpim.net/ebook/bbs/showthread.php?s=b8c660b21ff38c5fbaffa76d8871f825&threadid=46
      

  4.   

    响应WM_CTLCOLOR消息,返回一个绿色的brush,就行了
      

  5.   

    把button改成ownerdraw的。重载DrawItem函数。
    <
    void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {
       UINT uStyle = DFCS_BUTTONPUSH;<br />   // This code only works with buttons.
    <br />   ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
    <br />
    <br />   // If drawing selected, add the pushed style to DrawFrameControl.
    <br />   if (lpDrawItemStruct->itemState & ODS_SELECTED)
          uStyle |= DFCS_PUSHED;
       // Draw the button frame.
    <br />   ::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, 
          DFC_BUTTON, uStyle);   // Get the button's text.
       CString strText;   GetWindowText(strText);   // Draw the button text using the text color red.
       COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
       ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), 
          &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
    <br />   ::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
    }