/**
* Copyright(c) 2008 Yu Junheng,
* All Rights Reserved.
*
* Filename: IconButtonEx.cpp
*
*
* @Author Yu Junheng
* @Version 1.00 Date: 2008-10-18
*/
#include "stdafx.h"
#include "IconButtonEx.h"
#include "MemDC.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
namespace NIconButtonEx
{
const int nInitZero = 0;
const int nLGFontRatio = 10;
const LPCSTR lpszEmptyString = _T( "" );
const LPCTSTR lpszReturnFlag = _T( "\r\n" );
const LPCSTR lpszFontName = _T( "Arial" );
const int nFontSize = 13;
/**
* Constructor
* @return : none
* @since 1.00
*/
CIconButtonEx::CIconButtonEx()
: m_bHoverEffect( true ),
m_bMouseHover( false ),
m_eCaptionPos( DRAWPOS_MIDDLE ),
m_ftTextFont(),
m_hLeftIcon( NULL )
{
}
/**
* Destructor
* @return : none
* @since 1.00
*/
CIconButtonEx::~CIconButtonEx()
{
m_ftTextFont.DeleteObject();
if ( NULL != m_hLeftIcon )
{
::DestroyIcon( m_hLeftIcon );
m_hLeftIcon = NULL;
}
}
BEGIN_MESSAGE_MAP(CIconButtonEx, CButton)
//{{AFX_MSG_MAP(CIconButtonEx)
ON_WM_MOUSEMOVE()
ON_MESSAGE( WM_MOUSELEAVE, OnMouseLeave )
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// }
/**
* Add BS_OWNERDRAW style
* @return void
* @since 1.00
*/
void CIconButtonEx::PreSubclassWindow()
{
ModifyStyle( NULL, BS_OWNERDRAW );
CButton::PreSubclassWindow();
m_ftTextFont.CreatePointFont( nFontSize * 10, lpszFontName );
}
/**
* Draw it
* @Param lpDrawItemStruct: [in]
* @return void
* @since 1.00
*/
void CIconButtonEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pCurDC = CDC::FromHandle( lpDrawItemStruct->hDC );
CRect rcWndRect( lpDrawItemStruct->rcItem );
CMemDC dc( pCurDC, rcWndRect );
dc.SetBkMode( TRANSPARENT );
bool bDisabled = false;
if ( lpDrawItemStruct->itemState & ODS_SELECTED )
{
NGDIDraw::GDIDraw::DrawRadiantRectFace( &dc, &rcWndRect, RGB( 38, 85, 140 ),
RGB( 12, 28, 49 ), ( rcWndRect.Height() + 3 ) / 4 );
dc.Draw3dRect( &rcWndRect, RGB( 51, 153, 255 ), RGB( 51, 153, 255 ));
}
else if ( lpDrawItemStruct->itemState & ODS_DISABLED )
{
NGDIDraw::GDIDraw::DrawGradientRectFace( &dc, &rcWndRect, RGB( 29, 33, 43 ),
RGB( 4, 9, 19 ));
dc.Draw3dRect( &rcWndRect, RGB( 155, 155, 155 ), RGB( 45, 55, 75 ));
}
else if (( lpDrawItemStruct->itemState & ODS_FOCUS ) ||
( m_bMouseHover && m_bHoverEffect ))
{
NGDIDraw::GDIDraw::DrawGradientRectFace( &dc, &rcWndRect, RGB( 32, 44, 69 ),
RGB( 4, 9, 19 ));
dc.Draw3dRect( &rcWndRect, RGB( 51, 153, 255 ), RGB( 51, 153, 255 ));
}
else
{
NGDIDraw::GDIDraw::DrawGradientRectFace( &dc, &rcWndRect, RGB( 32, 44, 69 ),
RGB( 4, 9, 19 ));
dc.Draw3dRect( &rcWndRect, RGB( 155, 155, 155 ), RGB( 45, 55, 75 ));
}
NGDIDraw::GDIDraw::DrawIconState( &dc, m_hLeftIcon, rcWndRect );
CString csCaption( lpszEmptyString );
GetWindowText( csCaption );
if ( lpDrawItemStruct->itemState & ODS_DISABLED )
{
NGDIDraw::GDIDraw::DrawCaption( &dc, &m_ftTextFont, csCaption,
RGB( 70, 74, 81 ), rcWndRect, m_eCaptionPos );
}
else if ( lpDrawItemStruct->itemState & ODS_SELECTED )
{
NGDIDraw::GDIDraw::DrawCaption( &dc, &m_ftTextFont, csCaption,
RGB( 102, 139, 200 ), rcWndRect, m_eCaptionPos );
}
else
{
NGDIDraw::GDIDraw::DrawCaption( &dc, &m_ftTextFont, csCaption,
RGB( 190, 209, 255 ), rcWndRect, m_eCaptionPos );
}
}
/**
* When mouse move, redraw the button if necessary
* @Param nFlags: [in]
* @Param point: [in]
* @return void
* @since 1.00
*/
void CIconButtonEx::OnMouseMove(UINT nFlags, CPoint point)
{
if ( m_bHoverEffect )
{
TRACKMOUSEEVENT tme = { nInitZero };
tme.cbSize = sizeof( TRACKMOUSEEVENT );
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = m_hWnd;
if ( !m_bMouseHover )
{
if( ::TrackMouseEvent( &tme ))
{
m_bMouseHover = true;
RedrawWindow();
}
}
}
CButton::OnMouseMove(nFlags, point);
}
/**
* When mouse leave, invoke it
* @Param : [in] The message parameter
* @Param : [in] The message parameter
* @return void
* @since 1.00
*/
void CIconButtonEx::OnMouseLeave( WPARAM, LPARAM )
{
if ( m_bHoverEffect && m_bMouseHover )
{
m_bMouseHover = false;
RedrawWindow();
}
}
/**
* Set text font
* @Param lpszFontName: [in] The font name
* @Param nSize: [in] The font size
* @return void
* @since 1.00
*/
void CIconButtonEx::SetTextFont( LPCSTR lpszFontName, int nSize )
{
if ( nInitZero >= nSize )
{
nSize = nFontSize;
}
m_ftTextFont.DeleteObject();
m_ftTextFont.CreatePointFont( nSize * nLGFontRatio, lpszFontName );
}
/**
* Load title icon from resource
* @Param nPathName: [in] The icon resource name
* @Param bRedraw: true if it need redraw
* @return bool: true if success
* @since 1.00
*/
bool CIconButtonEx::LoadIcon( UINT nPathName, bool bRedraw )
{
m_hLeftIcon = AfxGetApp()->LoadIcon( nPathName );
if ( NULL == m_hLeftIcon )
{
return false;
}
if ( bRedraw )
{
RedrawWindow();
}
return true;
}
/**
* Load title icon from file
* @Param lpszPathName: [in] The icon path name
* @Param bRedraw: true if it need redraw
* @return bool: true if success
* @since 1.00
*/
bool CIconButtonEx::LoadIcon( LPCSTR lpszPathName, bool bRedraw )
{
m_hLeftIcon = AfxGetApp()->LoadIcon( lpszPathName );
if ( NULL == m_hLeftIcon )
{
return false;
}
if ( bRedraw )
{
RedrawWindow();
}
return true;
}
} // namespace
评论11
最新资源