// MyButtonTV.cpp : implementation file
//

#include "stdafx.h"
#include "main.h"
#include "MyButtonTV.h"

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

#include "mmsystem.h"

/////////////////////////////////////////////////////////////////////////////
// CMyButtonTV

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

CMyButtonTV::~CMyButtonTV()
{
}


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

/////////////////////////////////////////////////////////////////////////////
// CMyButtonTV message handlers

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

void CMyButtonTV::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	DisplayBitmap(DOWN,0,0);	
	sndPlaySound("sound\\testsnd.wav",SND_ASYNC);	
	SetTimer(2,300,NULL);
	CButton::OnLButtonDown(nFlags, point);
}

void CMyButtonTV::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 CMyButtonTV::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 CMyButtonTV::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\\camera.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 CMyButtonTV::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your code to draw the specified item
}

void CMyButtonTV::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CRect	rect;
	GetWindowRect(&rect);	
	CPoint	point;
	GetCursorPos(&point);
	static CString str("处理中");
	static num = 0;
	// If the mouse left our button,redraw it.
	if(nIDEvent == 1)
	{
		if (!rect.PtInRect(point) && m_bMove)
		{
				m_bMove=false;
				KillTimer (nIDEvent);
				DisplayBitmap(MAIN,0,0);
				DrawText(0);//normal
		}
	}
	else if(nIDEvent == 2)
	{
		str +=".";
		num++;
		if(rect.PtInRect(point) && num < 4)
		{
			DisplayBitmap(DOWN,0,0);
			DrawTextEx(str,0);
		}
		else
		{
			num = 0;
			str = "处理中";
			KillTimer (nIDEvent);
		}
	}


	CButton::OnTimer(nIDEvent);
}


void CMyButtonTV::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 CMyButtonTV::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 CMyButtonTV::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
}


void CMyButtonTV::DrawTextEx(CString str2, int flag)
{
	CRect rect;
	GetClientRect(&rect);
	CClientDC	dc(this);
	CString str;
 	GetWindowText(str);

	CFont font,*oldFont;
	font.CreatePointFont(100,"宋体");
	oldFont = dc.SelectObject(&font);
	COLORREF oldColor;
	oldColor = dc.SetTextColor(RGB(230,200,200));

	dc.SetBkMode(TRANSPARENT);

	rect.top +=25;
	dc.DrawText(str+"\r\n"+str2,rect,DT_CENTER|DT_VCENTER);

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