1.设置Button的颜色,正常为白色,鼠标在上面时为绿色
#pragma once
#include "afxwin.h"
class CMyButton :
public CButton
{
public:
CMyButton();
~CMyButton();
virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
DECLARE_MESSAGE_MAP()
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnMouseLeave();
private:
bool m_flag;
};
/
#include "stdafx.h"
#include "CMyButton.h"
CMyButton::CMyButton()
{
m_flag = false;
}
CMyButton::~CMyButton()
{
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: 添加您的代码以绘制指定项
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(255, 255, 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);
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
ON_WM_MOUSEMOVE()
ON_WM_MOUSELEAVE()
END_MESSAGE_MAP()
void CMyButton::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 CMyButton::OnMouseLeave()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_flag = false;
CButton::OnMouseLeave();
Invalidate();
UpdateWindow();
}
//
2.设置TabCtrl的背景色:
添加WM_ERASEBKGND消息
BOOL CMyTabCtrl::OnEraseBkgnd(CDC* pDC)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//return CTabCtrl::OnEraseBkgnd(pDC);
// TODO: Add your message handler code here and/or call default
//获取控件矩形
CRect rect;
GetClientRect(&rect);
//创建画刷
CBrush brush;
brush.CreateSolidBrush(RGB(255, 255, 255));
//填充控件背景
pDC->FillRect(&rect, &brush);
//return CTabCtrl::OnEraseBkgnd(pDC);
return true;
return CTabCtrl::OnEraseBkgnd(pDC);
}