看看下面msdn2008中的一个代码段,CMyDlg::OnCtlColor函数传递进来的参数nctlcolor好像没有用到呀,另外return的HBRUSH)m_RedBrush.GetSafeHandle()或(HBRUSH)m_BlueBrush.GetSafeHandle()跟hbr这个句柄怎么联系起来的??
//change the background color of an edit control on the dialog
HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
   HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);   if (pWnd->GetDlgCtrlID() == IDC_MYEDIT)
   {
      if (pWnd->IsWindowEnabled())
      {
         // Red brush for the background...
         pDC->SetBkColor(RGB(255, 0, 0));
         // m_pRedBrush is the CBrush object initialized with a red brush 
         // using CreateSolidBrush
         return(HBRUSH)m_RedBrush.GetSafeHandle();
      }
      else
      {
         // Blue brush for the background...
         pDC->SetBkColor(RGB(0, 0, 255));
         // m_pBlueBrush is the CBrush object initialized with a blue 
         // brush using CreateSolidBrush
         return (HBRUSH)m_BlueBrush.GetSafeHandle();
      }
   }   return hbr;
}

解决方案 »

  1.   

    再看看下面的代码,里面把m_brush赋值给hbr。
    // This OnCtlColor handler will change the color of a static control
    // with the ID of IDC_MYSTATIC. The code assumes that the CPenWidthsDlg
    // class has an initialized and created CBrush member named m_brush.
    // The control will be painted with red text and a background
    // color of m_brush.
    HBRUSH CPenWidthsDlg::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;
    }
    那么是否是应该这样:
    hbr=(HBRUSH)m_RedBrush.GetSafeHandle();hbr=(HBRUSH)m_BlueBrush.GetSafeHandle();
    然后再return hbr!??
      

  2.   

    从来没有说过形式参数必须在每次调用时都必须用上,消息处理函数是必须满足一定函数原型的,不是每个参数都必须被用到的假如你有多个control需要控制颜色,那么你可能需要根据最后一个参数决定到底设置什么刷子,这时候就需要用到他