www.pudn.com > GGBT.rar > ButtonEx.cpp
// ButtonEx.cpp : implementation file
//
#include "stdafx.h"
#include "ButtonEx.h"
#include "testBT.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CButtonEx
CButtonEx::CButtonEx()
{
m_bOver = m_bSelected = m_bTracking = m_bFocus = FALSE;
m_cyIcon = m_cxIcon = 16;
m_bIconOnly = false;
m_bTiping = FALSE;
m_hIcon = 0;
}
CButtonEx::~CButtonEx()
{
if (m_hIcon)
{
BOOL bRet = DestroyIcon(m_hIcon);
ASSERT(bRet);
}
}
BEGIN_MESSAGE_MAP(CButtonEx, CButton)
//{{AFX_MSG_MAP(CButtonEx)
ON_WM_CREATE()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
ON_WM_MOUSEMOVE()
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButtonEx message handlers
bool CButtonEx::Create(HICON hIcon, bool bIconOnly, CString strTipText)
{
m_bIconOnly = bIconOnly;
if (bIconOnly)
{
m_tip.Create(this, TTS_ALWAYSTIP);
m_tip.AddTool(this);
m_strTipText = strTipText;
ASSERT(!m_strTipText.IsEmpty());
}
// if (uResourceID)
// m_hIcon = AfxGetApp()->LoadIcon(uResourceID);
if (hIcon)
m_hIcon = hIcon;
else
{
ASSERT(FALSE);
}
return true;
}
void CButtonEx::RoundRectExK(CDC& dc, const CRect rc, const CPoint& pt, COLORREF itl, COLORREF ibr)
{
CPen pentl, penbr;
BOOL bRet = pentl.CreatePen(PS_SOLID, 1, itl);
ASSERT(bRet);
bRet = penbr.CreatePen(PS_SOLID, 1, ibr);
ASSERT(bRet);
CGdiObject* pold = dc.SelectObject(&pentl);
dc.MoveTo(rc.left, rc.bottom - pt.y);
dc.LineTo(rc.left, rc.top + pt.y);
dc.LineTo(rc.left + pt.x, rc.top );
dc.LineTo(rc.right - pt.x, rc.top);
dc.LineTo(rc.right, rc.top + pt.y);
dc.SelectObject(&penbr);
dc.LineTo(rc.right, rc.bottom - pt.y);
dc.LineTo(rc.right - pt.x, rc.bottom);
dc.LineTo(rc.left + pt.x, rc.bottom);
dc.LineTo(rc.left, rc.bottom - pt.y);
//*/
dc.SelectObject(pold);
pentl.DeleteObject();
penbr.DeleteObject();
}
int CButtonEx::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CButton::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
void CButtonEx::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_bTracking)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.hwndTrack = m_hWnd;
tme.dwFlags = TME_LEAVE ;
tme.dwHoverTime = 1;
m_bTracking = _TrackMouseEvent(&tme);
}
CPoint pt;
GetCursorPos(&pt);
ScreenToClient(&pt);
CRect rc;
GetClientRect(rc);
if (rc.PtInRect(pt))
{
if (!m_bOver)
{
m_bOver = true;
InvalidateRect(NULL, FALSE);
}
if (m_bIconOnly && !m_bTiping)
{
m_bTiping = true;
m_tip.UpdateTipText(m_strTipText, this);
}
}
else
{
if (m_bOver)
{
m_bOver = false;
InvalidateRect(NULL, FALSE);
}
}
CButton::OnMouseMove(nFlags, point);
}
LRESULT CButtonEx::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_bTiping = FALSE;
m_bOver = FALSE;
m_bTracking = FALSE;
InvalidateRect(NULL, FALSE);
return 0;
}
LRESULT CButtonEx::OnMouseHover(WPARAM wParam, LPARAM lParam)
{
m_bOver = TRUE;
InvalidateRect(NULL);
return 0;
}
void CButtonEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
_DrawItem(lpDrawItemStruct);
}
void CButtonEx::_DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
HDC dc = lpDrawItemStruct->hDC;
CRect &rect = (CRect&) lpDrawItemStruct->rcItem;
CMemDC pDC(dc, NULL);
UINT state = lpDrawItemStruct->itemState;
TCHAR szText[MAX_PATH + 1];
::GetWindowText(m_hWnd, szText, MAX_PATH);
POINT pt ;
pt.x = 2;
pt.y = 2;
if (state & ODS_FOCUS)
{
m_bFocus = TRUE;
m_bSelected = TRUE;
}
else
{
m_bFocus = FALSE;
m_bSelected = FALSE;
}
if (state & ODS_SELECTED || state & ODS_DEFAULT)
{
m_bFocus = TRUE;
}
// pDC.SelectObject(hOldPen);
// pDC.FillRect(rect, &CBrush(::GetSysColor(COLOR_BTNFACE)));
pDC.FillSolidRect(rect, ::GetSysColor(COLOR_BTNFACE));
CRect rcSel = rect;
rcSel.DeflateRect(1, 1);
if (m_bIconOnly)
{
if (state & ODS_SELECTED)
{
pDC.Draw3dRect(rcSel, RGB(128, 128, 128), RGB(255, 255, 255));
}
else if (m_bOver)
{
pDC.Draw3dRect(rcSel, RGB(255, 255, 255), RGB(128, 128, 128));
}
if (m_hIcon)
DrawTheIcon(&pDC, &CString(szText), rect, state & ODS_SELECTED, false);
return;
}
{
// DbgGuiLeak m_dbg;
if (state & ODS_SELECTED)
{
CRect rclt = rcSel;
rclt.DeflateRect(1, 1);
RoundRectExK(pDC, rclt, pt, RGB(128, 128, 128), RGB(10, 36, 106));
RoundRectExK(pDC, rcSel, CPoint(3, 3) , RGB(10, 36, 106), RGB(255, 255, 255));
}
else if (m_bOver)
{
CRect rclt = rcSel;
rclt.DeflateRect(1, 1);
RoundRectExK(pDC, rclt, pt, RGB(255, 255, 255), RGB(128, 128, 128));
RoundRectExK(pDC, rcSel, CPoint(3, 3),RGB(10, 36, 106) , RGB(10, 36, 106));
}
else
{
CRect rclt = rcSel;
rclt.DeflateRect(1, 1);
RoundRectExK(pDC, rclt, pt, RGB(128, 128, 128) , RGB(128, 128, 128));
}
}
rect.DeflateRect(CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
if (m_hIcon)
{
DrawTheIcon(&pDC, &CString(szText), rect, state & ODS_SELECTED, false);
}
if (szText!=NULL)
{
CFont* hFont = GetFont();
CFont* hOldFont = 0;
CFont fBold;
if (m_bFocus)
{
LOGFONT lf;
hFont->GetLogFont(&lf);
lf.lfWeight = FW_BOLD;
fBold.CreateFontIndirect(&lf);
hOldFont = pDC.SelectObject(&fBold);
}
else
{
hOldFont = pDC.SelectObject(hFont);
}
CSize Extent = pDC.GetTextExtent(szText, lstrlen(szText));
int iTextOffset = Extent.cx / 2;
if (m_hIcon)
iTextOffset = Extent.cx / 3;
CPoint pt( rect.CenterPoint().x - iTextOffset, rect.CenterPoint().y - Extent.cy / 2);
if (state & ODS_SELECTED)
pt.Offset(1, 1);
int nMode = pDC.SetBkMode(TRANSPARENT);
if (state & ODS_DISABLED)
pDC.DrawState(pt, Extent, szText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
else
pDC.DrawState(pt, Extent, szText, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL);
pDC.SelectObject(hOldFont);
pDC.SetBkMode(nMode);
if (m_bFocus)
{
CRect rcFocus(pt.x, pt.y, pt.x + Extent.cx, pt.y + Extent.cy);
rcFocus.InflateRect(2, 1);
pDC.DrawFocusRect(rcFocus);
}
}
}
BOOL CButtonEx::OnEraseBkgnd(CDC* pDC)
{
CRect rc;
pDC->GetClipBox(rc);
GetClientRect(rc);
// pDC->FillRect(rc, &CBrush(::GetSysColor(COLOR_BTNFACE)));
pDC->FillSolidRect(rc, ::GetSysColor(COLOR_BTNFACE));
return CButton::OnEraseBkgnd(pDC);
}
void CButtonEx::DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, BOOL IsPressed, BOOL IsDisabled)
{
CRect iconRect = rcItem;
if (title->IsEmpty())
{
iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
}
else
{
iconRect.left += 3;
}
// Center the icon vertically
iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
iconRect.right = iconRect.left + m_cxIcon;
iconRect.bottom += iconRect.bottom + m_cyIcon;
// If button is pressed then press the icon also
if (IsPressed) iconRect.OffsetRect(1, 1);
// Ole'!
pDC->DrawState(iconRect.TopLeft(),
iconRect.Size(),
m_hIcon,
DSS_NORMAL,
// (IsDisabled ? DSS_DISABLED : DSS_NORMAL),
(CBrush*)NULL);
} // End of DrawTheIcon
BOOL CButtonEx::PreTranslateMessage(MSG* pMsg)
{
if (m_bIconOnly)
{
if(m_tip.m_hWnd != NULL)
m_tip.RelayEvent(pMsg);
}
return CButton::PreTranslateMessage(pMsg);
}