怎样用GDI中的函数来设置窗体中的某一控件背景颜色?

解决方案 »

  1.   

    HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    // TODO: Change any attributes of the DC here
    if(m_hbBkgnd)
    DeleteObject(m_hbBkgnd);
    srand( (unsigned)time( NULL ) );
    int color1 = rand()%256;
    int colorg = rand()%256;
    int colorb = rand()%256;
    //The WM_CTLCOLORBTN message will affect radio buttons and check boxes, but not pushbuttons. 
    //To change the color of a pushbutton you must make it an owner-drawn pushbutton and take 
    //over responsibility for drawing every aspect of the button. 
    pDC -> SetTextColor (RGB(255,255,colorb) ) ; 
    pDC->SetBkColor(m_clrBkgnd);
    m_hbBkgnd=::CreateSolidBrush(m_clrBkgnd);
    return m_hbBkgnd;
    return hbr;
    }
      

  2.   

    有两种方法。其一,可以在父类中指定控件的颜色,或者利用MFC新的消息反       
    射在控件类中指定颜色。                                                           
        当控件需要重新着色时,工作框调用父窗口(通常是对话框)的                     
    CWnd: : OnCrtlColor,可以在父窗口类中重置该函数并指定控件的新的绘画属             
    性。例如,下述代码将对话中的所有编辑控件文本颜色改为红色:                       
    HBRUSH CAboutDig : : OnCtlColor (CDC * pDCM , CWnd * pWnd , UINT nCtlColor)      
                                                                                     
    {                                                                                
        HBRUSH hbr = CDialog : : OnCtlColor (pDC, pWnd , nCtlColor );                
                                                                                     
        //Draw red text for all edit controls .                                      
        if (nCtlColor= = CTLCOLOR_EDIT )                                             
           pDC —> SetTextColor (RGB (255 , 0 , 0 , ) ) ;                            
                                                                                     
        return hbr ;                                                                 
    }                                                                                
        然而,由于每个父窗口必须处理通知消息并指定每个控件的绘画属性,所以,         
    这种方法不是完全的面向对象的方法。控件处理该消息并指定绘画属性更合情合理。       
        消息反射允许用户这样做。通知消息首先发送给父窗口,如果父窗口没有处理         
    则发送给控件。创建一个定制彩色列表框控件必须遵循下述步骤。                       
        首先,使用ClassWizard 创建一个CListBox 的派生类并为该类添加下述数据          
    成员。                                                                           
    class CMyListBox ; publilc CListBox                                              
    {                                                                                
    …                                                                               
                                                                                     
    private;                                                                         
        COLORREF m_clrFor ;     // foreground color                                  
        COLORREF m_clrBack ;   //background color                                    
        Cbrush m_brush ;         //background brush                                  
                                                                                     
    …                                                                               
    } ;                                                                              
        其次,在类的构造函数中,初始化数据中。                                       
    CMyListBox : : CMyListBox ()                                                     
    {                                                                                
        //Initialize data members .                                                  
        m_clrFore =RGB (255 , 255 , 0) ;   // yellow text                            
        m_clrBack=RGB (0 , 0 , 255) ;    // blue background                          
        m_brush . CreateSolidBrush  (m _clrBack );                                   
    }                                                                                
        最后,使用ClassWizard处理反射的WM_CTLCOLOR(=WM_CTLCOLOR)消息并指定新         
    的绘画属性。                                                                     
    HBRUSH CMyListBox : : CtlColor (CDC* pDC, UINT nCtlColor )                       
    {                                                                                
        pDC—>SetTextColor (m_clrFore);                                              
        pDC—>SetBkColor  (m_clrBack);                                               
                                                                                     
        return (HBRUSH) m_brush.GetSafeHandle ()                                     
    }                                                                                
        现在,控件可以自己决定如何绘画,与父窗口无关。