HBRUSH
CXXXDlg::OnCtlColor(CDC* pDC, CWnd* pWnd,
UINT
nCtlColor)
{
HBRUSH
hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何属性
if
(nCtlColor==CTLCOLOR_BTN)
//更改按钮颜色
{
//pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(121,121,255));
HBRUSH
b=CreateSolidBrush(RGB(121,121,255));
return
b;
}
else
if
(nCtlColor==CTLCOLOR_SCROLLBAR)
//
{
//pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(233,233,220));
HBRUSH
b=CreateSolidBrush(RGB(233,233,220));
return
b;
}
else
if
(nCtlColor==CTLCOLOR_EDIT)
//更改编辑框
{
//pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(165,254,236));
HBRUSH
b=CreateSolidBrush(RGB(165,254,236));
return
b;
}
else
if
(nCtlColor==CTLCOLOR_STATIC)
//更改静态文本
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(166,254,1));
HBRUSH
b=CreateSolidBrush(RGB(166,254,1));
return
b;
}
else
if
(nCtlColor==CTLCOLOR_DLG)
//更改对话框背景色
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(166,254,1));
HBRUSH
b=CreateSolidBrush(RGB(166,254,1));
return
b;
}
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return
hbr;
}
对Button按钮修改需要通过重写DrawItem方法,所以写一个类CSXBtn,继承于CButton类。CSXBtn类实现了鼠标在button和不在button按钮时变换背景色功能
void CSXBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
static int i=0;
UINT uStyle = BS_DEFPUSHBUTTON;
// This code only works with buttons.
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
// If drawing selected, add the pushed style to DrawFrameControl.
if (lpDrawItemStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;
// Draw the button frame.
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,
DFC_BUTTON, uStyle);
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
// Get the button's text.
CString strText;
GetWindowText(strText);
// Draw the button text using the text color red.
CBrush B;
CRect focusRect;
focusRect.CopyRect(&lpDrawItemStruct->rcItem);
DrawFocusRect(lpDrawItemStruct->hDC, (LPRECT)&focusRect);
pDC->Draw3dRect(focusRect, ::GetSysColor(COLOR_BTNHILIGHT), ::GetSysColor(COLOR_BTNSHADOW));
if(m_flag)//判断鼠标是否在button按钮上
{
B.CreateSolidBrush(RGB(0,255,0));
}
else
{
B.CreateSolidBrush(RGB(0,0,255));
}
::FillRect(lpDrawItemStruct->hDC,&focusRect, (HBRUSH)B.m_hObject);
::SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}
void CSXBtn::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_flag=true;
TRACKMOUSEEVENT tme;
tme.cbSize=sizeof(tme);
tme.dwFlags=TME_LEAVE;
tme.hwndTrack=this->m_hWnd;
::_TrackMouseEvent(&tme);
CButton::OnMouseMove(nFlags, point);
Invalidate();
}
void CSXBtn::OnMouseLeave()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_flag=false;
CButton::OnMouseLeave();
Invalidate();
UpdateWindow();
}