www.pudn.com > BKColor.rar > MyButton.cpp
// MyButton.cpp : implementation file
//
#include "stdafx.h"
#include "BKColor.h"
#include "MyButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyButton
CMyButton::CMyButton()
{
m_clrFore = RGB(255, 0, 255); // »ÆÉ«
m_clrBack = RGB(0, 0, 255); // À¼É«
m_brush.CreateSolidBrush(m_clrBack);
m_IsMouseOnButton = FALSE;
// m_IsCreated = FALSE;
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
//{{AFX_MSG_MAP(CMyButton)
ON_WM_MOUSEMOVE()
ON_WM_KILLFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
UINT uStyle = DFCS_BUTTONPUSH;
//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);
//Get the Button's text
CString strText;
GetWindowText(strText);
//Set the background
// CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect;
// CRect focusRect;
// focusRect.CopyRect(&lpDrawItemStruct->rcItem);
// DrawFocusRect(lpDrawItemStruct->hDC, (LPRECT)&focusRect);
// focusRect.left += 4;
// focusRect.top += 4;
// focusRect.right -= 4;
// focusRect.bottom -= 4;
rect.CopyRect(&lpDrawItemStruct->rcItem);
// pDC->Draw3dRect(rect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHIGHLIGHT));
::FillRect(lpDrawItemStruct->hDC, &rect, (HBRUSH)m_brush.m_hObject);
::SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);
//Draw the button text.
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, m_clrFore);
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
COLORREF bkOldColor = ::SetBkColor(lpDrawItemStruct->hDC, m_clrBack);
::SetBkColor(lpDrawItemStruct->hDC, bkOldColor);
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CBrush brush;
brush.CreateSolidBrush(RGB(0, 255, 0));
CRect rect;
GetClientRect(&rect);
HDC dc = ::GetDC(this->m_hWnd);
CString strText;
GetWindowText(strText);
if(rect.PtInRect(point))
{
::FillRect(dc, &rect, (HBRUSH)brush.m_hObject);
::SetBkMode(dc, TRANSPARENT);
COLORREF crOldColor = ::SetTextColor(dc, m_clrFore);
::DrawText(dc, strText, strText.GetLength(),
&rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
::SetTextColor(dc, crOldColor);
COLORREF bkOldColor = ::SetBkColor(dc, m_clrBack);
::SetBkColor(dc, bkOldColor);
}
::ReleaseDC(this->m_hWnd, dc);
CButton::OnMouseMove(nFlags, point);
}
void CMyButton::OnKillFocus(CWnd* pNewWnd)
{
CButton::OnKillFocus(pNewWnd);
// TODO: Add your message handler code here
}