4.4--如何控制对话框的背景颜色?

      与如何改变程序清单视图的背景颜色FAQ3.31中给出的解决方案几乎相同,这是因为程序清单视图是一种特殊的对话框。

 

第一步:在对话框类里定义一个HBRUSH类型的成员变量

CBrush m_brBack;//背景颜色画刷

第二步:在对话框类的构造函数中初始化背景颜色画刷

CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/) : CDialog(CDemoDlg::IDD, pParent) { //...... //...... // Set initial background color to cyan m_brBack.CreateSolidBrush( RGB( 0, 255, 255 ) ); }  

第三步:使用ClassWizard为对话框增加一个WM_CTLCOLOR消息处理函数,代码如下:

HBRUSH CDemoDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { switch( nCtlColor ) { case CTLCOLOR_BTN: case CTLCOLOR_STATIC: pDC->SetBkMode( TRANSPARENT ); case CTLCOLOR_DLG: return static_cast<HBRUSH>( m_brBack.GetSafeHandle() ); } return CDialog::OnCtlColor(pDC, pWnd, nCtlColor); } 

MFC为改变所有对话框的颜色提供了一种简单的方式,在应用程序的InitInstance()函数中调用SetDialogBkColor()CWinApp::SetDialogBkColor

void SetDialogBkColor( COLORREF clrCtlBk = RGB(192, 192, 192), COLORREF clrCtlText = RGB(0, 0, 0) );

Parameters

clrCtlBk

The dialog background color for the application.

clrCtlText

The dialog control color for the application.

Remarks

Call this member function from within the InitInstance member function to set the default background and text color for dialog boxes and message boxes within your application.

Example

BOOL CWinApp::InitInatance()

{

      COLORREF crBackground=RGB(255,255,0);

      COLORREF crText=RGB(255,0,0);

 

      //设置对话框颜色为黄色背景和红色文本

      SetDialogBkColor(crBackground,crText);

 

}

 

你可能感兴趣的:(function,null,mfc,dialog)