// ZBitmapButton.cpp : implementation file
//

#include "stdafx.h"
#include "ZBitmapButton.h"
#include "MemDC.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CZBitmapButton

CZBitmapButton::CZBitmapButton()
{
	m_State=0;
	m_bMouseCaptured=FALSE;
}

CZBitmapButton::~CZBitmapButton()
{
}


BEGIN_MESSAGE_MAP(CZBitmapButton, CButton)
	//{{AFX_MSG_MAP(CZBitmapButton)
	ON_WM_MOUSEMOVE()
	ON_WM_DRAWITEM()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CZBitmapButton message handlers

void CZBitmapButton::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	if (!m_bMouseCaptured || GetCapture()!=this)
	{
		SetCapture();
		m_bMouseCaptured=TRUE;		
		OnMouseEnter(nFlags,point);
		
	} else
	{
		CRect rc;
		this->GetClientRect(&rc);
		if (!rc.PtInRect(point))
		{
			OnMouseLeave(nFlags,point);			
			m_bMouseCaptured=FALSE;			
			ReleaseCapture();
		}
	}
	Invalidate( FALSE );
	//CButton::OnMouseMove(nFlags, point);
}

void CZBitmapButton::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your message handler code here and/or call default	
	CButton::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

void CZBitmapButton::SetBitmap(UINT iNormal, UINT iOnMouse, UINT iClick)
{
	m_bmNormal.LoadBitmap(iNormal);
	m_bmOnMouse.LoadBitmap(iOnMouse);
	m_bmClick.LoadBitmap(iClick);	
}

void CZBitmapButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your code to draw the specified item
	CDC* pDC;	
	pDC=CDC::FromHandle(lpDrawItemStruct->hDC);

	CMemDC	mDC( pDC, lpDrawItemStruct->rcItem );

	switch(m_State)
	{
		case 0:
			RenderBitmap(mDC,m_bmNormal);
			break;
		case 1:
			RenderBitmap(mDC,m_bmOnMouse);
			break;
		case 2:
			RenderBitmap(mDC,m_bmClick);
			break;
	}
	
}

void CZBitmapButton::RenderBitmap(CDC &rDC, CBitmap &rBitmap)
{
	CBitmap *pOldBitmap;
	CDC memDC;
	memDC.CreateCompatibleDC(&rDC);
	pOldBitmap=memDC.SelectObject(&rBitmap);
	CSize sBitmap=rBitmap.GetBitmapDimension();

	BITMAP bmInfo;
	rBitmap.GetBitmap(&bmInfo);	
	rDC.BitBlt(0,0,bmInfo.bmWidth,bmInfo.bmHeight,&memDC,0,0,SRCCOPY);
	memDC.SelectObject(pOldBitmap);
}

BOOL CZBitmapButton::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class	
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

void CZBitmapButton::OnMouseEnter(UINT nFlags, CPoint point)
{
	m_State=1;	
	Invalidate(FALSE);
}

void CZBitmapButton::OnMouseLeave(UINT nFlags, CPoint point)
{	
	m_State=0;
	Invalidate(FALSE);
}

void CZBitmapButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_State=2;
	Invalidate(FALSE);
	CButton::OnLButtonDown(nFlags, point);
}

void CZBitmapButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_State=1;	
	Invalidate(FALSE);
	CButton::OnLButtonUp(nFlags, point);
}

BOOL CZBitmapButton::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	return TRUE;
	//return CButton::OnEraseBkgnd(pDC);
}
