WTL之修改对话框背景和文字颜色的类
/* * 编写: GUO * QQ:349384061 * 用法 * 1.让对话框类公有继承本类 * 2.在对话框类的消息映射宏后面添加消映射链 CHAIN_MSG_MAP(CPaintColor) * 3.本类只是简单的演示,可在此基础上扩展 */ template <class T> //T 为派生的对话框类 class CPaintColor : public CMessageMap { public: CBrush m_brushBkgnd; //背景色, COLORREF m_clrText; //文字颜色 public: CPaintColor(COLORREF clrText = RGB(0,0,255)) { //默认颜色 m_brushBkgnd.CreateSolidBrush(::GetSysColor(COLOR_BTNFACE)); m_clrText = clrText; } ~CPaintColor() { if (m_brushBkgnd.m_hBrush != NULL) m_brushBkgnd.DeleteObject(); } BEGIN_MSG_MAP(CPaintColor) MESSAGE_HANDLER(WM_ERASEBKGND,OnEraseBkgnd) MESSAGE_HANDLER(WM_CTLCOLORSTATIC, OnCtlColorDlg) MESSAGE_HANDLER(WM_CTLCOLORDLG, OnCtlColorDlg) MESSAGE_HANDLER(WM_CTLCOLOREDIT, OnCtlColorDlg) MESSAGE_HANDLER(WM_CTLCOLORBTN, OnCtlColorDlg) MESSAGE_HANDLER(WM_CTLCOLORMSGBOX, OnCtlColorDlg) MESSAGE_HANDLER(WM_CTLCOLORLISTBOX, OnCtlColorDlg) END_MSG_MAP() //创建背景颜色画刷,也可以另外扩展以位图的方式 HBRUSH CreateSolidBrush(COLORREF crColor) { if (m_brushBkgnd.m_hBrush != NULL) m_brushBkgnd.DeleteObject(); m_brushBkgnd.CreateSolidBrush(crColor); return m_brushBkgnd.m_hBrush; } //设置文字颜色,并返回旧的颜色 COLORREF SetTextColor(COLORREF clrText) { COLORREF clrTextOld = m_clrText; m_clrText = clrText; return clrTextOld; } protected: LRESULT OnEraseBkgnd(UINT uMsg,WPARAM wParam,LPARAM lParam,BOOL &bHandled) { T* pThis = static_cast<T*>(this); HDC hDc = (HDC)wParam; RECT rcRect; pThis->GetClientRect(&rcRect); FillRect(hDc,&rcRect,m_brushBkgnd); return 1; } LRESULT OnCtlColorDlg(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { // TODO: 在此添加消息处理程序代码和/或调用默认值 HDC hDc = (HDC)wParam; switch (uMsg) { case WM_CTLCOLORSTATIC: case WM_CTLCOLORBTN: case WM_CTLCOLORMSGBOX: case WM_CTLCOLORLISTBOX: { ::SetBkMode(hDc,TRANSPARENT); ::SetTextColor(hDc,m_clrText); bHandled = TRUE; return (LRESULT)m_brushBkgnd.m_hBrush; } break; case WM_CTLCOLORDLG: case WM_CTLCOLOREDIT: default: break; } return 0; } };
好容易弄明白,留着以后使用