// MyButton.cpp : implementation file
//

#include "stdafx.h"
#include "main.h"
#include "MyButton.h"
#include "mmsystem.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyButton

CMyButton::CMyButton()
{
	m_bMove = false;
}

CMyButton::~CMyButton()
{
}


BEGIN_MESSAGE_MAP(CMyButton, CButton)
	//{{AFX_MSG_MAP(CMyButton)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_TIMER()
	ON_WM_PAINT()
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers

void CMyButton::PreSubclassWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CButton::PreSubclassWindow();
}

void CMyButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	DisplayBitmap(DOWN,0,0);	
	DrawText(1);//down
	sndPlaySound("sound\\cashreg.wav",SND_ASYNC);	
	CButton::OnLButtonDown(nFlags, point);
}

void CMyButton::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	DisplayBitmap(DOWN,0,0);	
	DrawText(1);//down
	CButton::OnLButtonDblClk(nFlags, point);
}
void CMyButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	DisplayBitmap(MOVE,0,0);	
	DrawText(0);//normal
	CButton::OnLButtonUp(nFlags, point);
}

void CMyButton::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(!m_bMove)
	{
		m_bMove=true;
		SetTimer(1,10,NULL);
		DisplayBitmap(MOVE,0,0);
		DrawText(0);//normal
		sndPlaySound("sound\\whoosh.wav",SND_ASYNC);	
//		HRSRC hRsrc=FindResource(AfxGetResourceHandle(),MAKEINTRESOURCE(IDR_OVER_WAV),"WAVE");
//		HGLOBAL hglb=LoadResource(AfxGetResourceHandle(),hRsrc);
//		::sndPlaySound((LPCTSTR)::LockResource(hglb),SND_MEMORY|SND_ASYNC|SND_NODEFAULT);
//		FreeResource(hglb);
	}
	CButton::OnMouseMove(nFlags, point);
}

void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your code to draw the specified item
}

void CMyButton::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CRect	rect;
	GetWindowRect(&rect);	
	CPoint	point;
	GetCursorPos(&point);
	// If the mouse left our button,redraw it.
	if (!rect.PtInRect(point) && m_bMove)
	{
		KillTimer (nIDEvent);
		m_bMove=false;
		DisplayBitmap(MAIN,0,0);
		DrawText(0);//normal
	}
	CButton::OnTimer(nIDEvent);
}


void CMyButton::DrawText(int flag)
{
	CRect rect;
	GetClientRect(&rect);
//	GetWindowRect(&rect);
	CClientDC	dc(this);
	CString str;
 	GetWindowText(str);

	CFont font,*oldFont;
	font.CreatePointFont(120,"ËÎÌå");
	oldFont = dc.SelectObject(&font);
	COLORREF oldColor;
	oldColor = dc.SetTextColor(RGB(200,200,230));

	dc.SetBkMode(TRANSPARENT);

	if(flag == 0)
		dc.DrawText(str,rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	else if(flag == 1)
	{
		rect.top += 2;
		rect.left += 2;
		dc.DrawText(str,rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	}


	dc.SetTextColor(oldColor);
	dc.SelectObject(oldFont);
}


void CMyButton::DisplayBitmap(UINT ID,int x,int y)
{
	CBitmap Bitmap;
	Bitmap.LoadBitmap(ID);
	CDC MemDC;
	MemDC.CreateCompatibleDC(NULL);
	MemDC.SelectObject(&Bitmap);
	BITMAP BM;
	Bitmap.GetObject(sizeof(BM),&BM);
	CDC *pDC = GetDC();
	pDC->BitBlt(x,y,BM.bmWidth,BM.bmHeight,&MemDC,0,0,SRCCOPY);
	ReleaseDC(pDC);
}

void CMyButton::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	// TODO: Add your message handler code here
	DisplayBitmap(MAIN,0,0);
	DrawText(0);//normal
	// Do not call CButton::OnPaint() for painting messages
}

