星期四, 7月 27, 2006

改變DLG的背景和TEXT顏色

How To Change the Background/Text color in a Dialog
If you want to change the background and/or text color for all the dialogs in the application, you should call the CWinApp::SetDialogBkColor method in the InitInstance member function:

#define RGBRED RGB(128,0,0)
#define RGBWHITE RGB(255,255,255)

BOOL CChngDlgBkgndApp::InitInstance()
{

// (...)

// Set default background (red) and text (white) colors
SetDialogBkColor(RGBRED, RGBWHITE);

// (...)
}

Another possibility is to change the background and/or text color of a particular dialog.
In the header file of the dialog, define a CBrush an a handler for the WM_CTLCOLOR message:


// ChngDlgBkgndDlg.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CChngDlgBkgndDlg dialog

class CChngDlgBkgndDlg : public CDialog
{
// Construction
public:
CChngDlgBkgndDlg(CWnd* pParent = NULL); // standard constructor

// To store the new background brush
CBrush m_bkBrush;


// Implementation
protected:

// Generated message map functions
//{{AFX_MSG(CChngDlgBkgndDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};



// ChngDlgBkgndDlg.cpp : implementation file
//

#define RGBBLUE RGB(0,0,128)
#define RGBWHITE RGB(255,255,255)


CChngDlgBkgndDlg::CChngDlgBkgndDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChngDlgBkgndDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChngDlgBkgndDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

// Initialize brush with the desired background color
m_bkBrush.CreateSolidBrush(RGBBLUE);
}


HBRUSH CChngDlgBkgndDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{

switch (nCtlColor) {

case CTLCOLOR_STATIC: // Static control
// Set the static text to white on blue.
pDC->SetTextColor(RGBWHITE);
pDC->SetBkColor(RGBBLUE);
return (HBRUSH)(m_bkBrush.GetSafeHandle());

case CTLCOLOR_DLG: // Dialog box
return (HBRUSH)(m_bkBrush.GetSafeHandle());

default:
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
}