弱弱的问:怎么设置对话框的背景色?
谢谢!

解决方案 »

  1.   

    用CWinApp::SetDialogBkColor
    一般在InitInstance中调用.
    不过这个功能很弱.
    建议你重新写一Dialog类,把它做为对话框的基类.
      

  2.   

    1.重载OnCtlColor
    HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {

    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    // TODO: Change any attributes of the DC here

    if(nCtlColor == CTLCOLOR_DLG)
    {
    // pDC->SetBkColor(RGB(68,192,128));
    hbr =CreateSolidBrush(RGB(68,192,128));
    return hbr;
    } // TODO: Return a different brush if the default is not desired
    return hbr;
    }
      

  3.   

    调用CWinApp : : SetDialogBkColor可以改变所有应用程序的背景颜色。第一个参数指定了背景颜色,第二个参数指定了文本颜色。下例将应用程序对话设置为蓝色背景和黄色文本。
    BOOL CSampleApp : : InitInstance ( )
    {
     …
     //use blue dialog with yellow text .
     SetDialogBkColor (RGB (0, 0, 255 ), RGB ( 255 ,255 , 0 ) )
     …
    }
    需要重画对话(或对话的子控件)时,Windows向对话发送消息WM_CTLCOLOR,通常用户可以让Windows选择绘画背景的刷子,也可重置该消息指定刷子。下例说明了创建一个红色背景对话的步骤。
    首先,给对话基类增加一人成员变量
    CBursh :class CMyFormView : public CFormView
    {
     …
     private :
     CBrush m_ brush // background brush
     …
    }
    其次, 在类的构造函数中将刷子初始化为所需要的背景颜色。
    CMyFormView : : CMyFormView ( )
    {
     // Initialize background brush .
     m_brush .CreateSolidBrush (RGB ( 0, 0, 255) )
    }
    最后,使用ClassWizard处理WM_CTLCOLOR消息并返回一个用来绘画对话背景的刷子句柄。注意:由于当重画对话控件时也要调用该函数,所以要检测nCtlColor参量。
    HBRUSH CMyFormView : : OnCtlColor (CDC* pDC , CWnd*pWnd , UINT nCtlColor
    )
    {
     // Determine if drawing a dialog box . If we are, return +handle to
     //our own background brush . Otherwise let windows handle it .
     if (nCtlColor = = CTLCOLOR _ DLG )
     return (HBRUSH) m_brush.GetSafeHandle ( )
     return CFormView : : OnCtlColor (pDC, pWnd , nCtlColor
    )
    }
      

  4.   

    唉,那有那么麻烦阿,直接再WM_EARseBACKGROUND里填充就可以了