// MyFourButton.cpp : implementation file
//

#include "stdafx.h"
#include "NetPlayX.h"
#include "MyFourButton.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyFourButton

CMyFourButton::CMyFourButton()
{
	m_nWidth = m_nHeight = 0;
	m_hCursor	  = NULL;
	//m_pWnd		  = NULL;
	m_hBitmap	  = NULL;
	m_hBitmapdown = m_hbitmapfocus = m_hbitmapdis = NULL;
	m_bIn		  = FALSE;
	m_bCapture    = FALSE;
	m_pos	      = 0;
	m_state	      = FALSE;
}

CMyFourButton::~CMyFourButton()
{
	DestroyWindow();
}


BEGIN_MESSAGE_MAP(CMyFourButton, CWnd)
	//{{AFX_MSG_MAP(CMyFourButton)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_ENABLE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CMyFourButton message handlers

BOOL CMyFourButton::Create(UINT ID,CWnd *pParentWnd,HJ_BTNMouseEvent pMouseEvent)
{
	m_nID         = ID;
	m_pWnd        = pParentWnd;
	m_MouseEvent  = pMouseEvent;
	
	CRect rect(10,10,10,10);
	if(CWnd::Create(NULL,NULL,WS_VISIBLE|WS_CHILD|CS_HREDRAW|CS_VREDRAW,rect,pParentWnd,ID))
	{
		m_tooltip.Create(this);
		m_tooltip.Activate(TRUE);
		m_tooltip.SetDelayTime(250);
		m_tooltip.AddTool(this);
		return TRUE;
	}
	return FALSE;
}

BOOL CMyFourButton::LoadBitmaps(LPCTSTR lpnomal, LPCTSTR lpdown, LPCTSTR lpfocus, LPCTSTR lpdis)
{
	RECT rect;
	BITMAP bm     = {0};
	m_hBitmap     = (HBITMAP)::LoadImage(NULL,lpnomal,
		IMAGE_BITMAP,
		0,0,
		LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
	if(m_hBitmap == NULL) goto err_exit;
	GetObject(m_hBitmap,sizeof(BITMAP),&bm);
	m_nWidth   = bm.bmWidth;
	m_nHeight  = bm.bmHeight;

	if(lpdown)
	{
		m_hBitmapdown = (HBITMAP)::LoadImage(NULL,lpdown,
			IMAGE_BITMAP,
			0,0,
			LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
		if(m_hBitmapdown == NULL) goto err_exit;
	}
	if(lpfocus)
	{
		m_hbitmapfocus = (HBITMAP)::LoadImage(NULL,lpfocus,
			IMAGE_BITMAP,
			0,0,
			LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
		if(m_hbitmapfocus == NULL) goto err_exit;
	}
	if(lpdis)
	{
		m_hbitmapdis = (HBITMAP)::LoadImage(NULL,lpdis,
			IMAGE_BITMAP,
			0,0,
			LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
		if(m_hbitmapdis == NULL) goto err_exit;
	}
	GetWindowRect(&rect);
	rect.right  = rect.left + m_nWidth;
	rect.bottom = rect.top  + m_nHeight;
	MoveWindow(&rect);
	return TRUE;
err_exit:
	DEL_BMPOBJECT(m_hBitmap);
	DEL_BMPOBJECT(m_hBitmapdown);
	DEL_BMPOBJECT(m_hbitmapfocus);
	DEL_BMPOBJECT(m_hbitmapdis);
	return FALSE;
}

BOOL CMyFourButton::LoadBitmaps(LPCTSTR lpnomal, LPCTSTR lpdown, LPCTSTR lpfocus, LPCTSTR lpdis,int width,int height)
{
	RECT rect;
	BITMAP bm     = {0};
	m_hBitmap     = (HBITMAP)::LoadImage(NULL,lpnomal,
		IMAGE_BITMAP,
		width,height,
		LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
	if(m_hBitmap == NULL) goto err_exit;
	GetObject(m_hBitmap,sizeof(BITMAP),&bm);
	m_nWidth   = bm.bmWidth;
	m_nHeight  = bm.bmHeight;

	if(lpdown)
	{
		m_hBitmapdown = (HBITMAP)::LoadImage(NULL,lpdown,
			IMAGE_BITMAP,
			width,height,
			LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
		if(m_hBitmapdown == NULL) goto err_exit;
	}
	if(lpfocus)
	{
		m_hbitmapfocus = (HBITMAP)::LoadImage(NULL,lpfocus,
			IMAGE_BITMAP,
			width,height,
			LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
		if(m_hbitmapfocus == NULL) goto err_exit;
	}
	if(lpdis)
	{
		m_hbitmapdis = (HBITMAP)::LoadImage(NULL,lpdis,
			IMAGE_BITMAP,
			width,height,
			LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
		if(m_hbitmapdis == NULL) goto err_exit;
	}
	GetWindowRect(&rect);
	rect.right  = rect.left + m_nWidth;
	rect.bottom = rect.top  + m_nHeight;
	MoveWindow(&rect);
	return TRUE;
err_exit:
	DEL_BMPOBJECT(m_hBitmap);
	DEL_BMPOBJECT(m_hBitmapdown);
	DEL_BMPOBJECT(m_hbitmapfocus);
	DEL_BMPOBJECT(m_hbitmapdis);
	return FALSE;
}

BOOL CMyFourButton::LoadBitmaps(UINT nnomalID, UINT ndownID, UINT nfocusID, UINT ndisID)
{
	RECT rect;
	BITMAP bm     = {0};
	m_hBitmap     = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(nnomalID));
	if(m_hBitmap == NULL) goto err_exit;
	GetObject(m_hBitmap,sizeof(BITMAP),&bm);
	m_nWidth   = bm.bmWidth;
	m_nHeight  = bm.bmHeight;
	if(ndownID)
	{
		m_hBitmapdown = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(ndownID));
		if(m_hBitmapdown == NULL) goto err_exit;
	}
	if(nfocusID)
	{
		m_hbitmapfocus = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(nfocusID));
		if(m_hbitmapfocus == NULL) goto err_exit;
	}
	if(ndisID)
	{
		m_hbitmapdis = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(ndisID));
		if(m_hbitmapdis == NULL) goto err_exit;
	}
	GetWindowRect(&rect);
	rect.right  = rect.left + m_nWidth;
	rect.bottom = rect.top  + m_nHeight;
	MoveWindow(&rect);
	return TRUE;
err_exit:
	DEL_BMPOBJECT(m_hBitmap);
	DEL_BMPOBJECT(m_hBitmapdown);
	DEL_BMPOBJECT(m_hbitmapfocus);
	DEL_BMPOBJECT(m_hbitmapdis);
	return FALSE;
}


BOOL CMyFourButton::LoadBitmaps(int width,int height,UINT nnomalID, UINT ndownID, UINT nfocusID, UINT ndisID)
{
	RECT rect;
	BITMAP bm     = {0};

	m_hBitmap     = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(nnomalID),
		IMAGE_BITMAP,
		width,height,
		LR_CREATEDIBSECTION|LR_DEFAULTSIZE);

	if(m_hBitmap == NULL) goto err_exit;
	GetObject(m_hBitmap,sizeof(BITMAP),&bm);
	m_nWidth   = bm.bmWidth;
	m_nHeight  = bm.bmHeight;
	if(ndownID)
	{
		m_hBitmapdown     = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
			MAKEINTRESOURCE(ndownID),
			IMAGE_BITMAP,
			width,height,
			LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
		if(m_hBitmapdown == NULL) goto err_exit;
	}
	if(nfocusID)
	{
		m_hbitmapfocus     = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
			MAKEINTRESOURCE(nfocusID),
			IMAGE_BITMAP,
			width,height,
			LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
		if(m_hbitmapfocus == NULL) goto err_exit;
	}
	if(ndisID)
	{
		m_hbitmapdis     = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
			MAKEINTRESOURCE(ndisID),
			IMAGE_BITMAP,
			width,height,
			LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
		if(m_hbitmapdis == NULL) goto err_exit;
	}
	GetWindowRect(&rect);
	rect.right  = rect.left + m_nWidth;
	rect.bottom = rect.top  + m_nHeight;
	MoveWindow(&rect);
	return TRUE;
err_exit:
	DEL_BMPOBJECT(m_hBitmap);
	DEL_BMPOBJECT(m_hBitmapdown);
	DEL_BMPOBJECT(m_hbitmapfocus);
	DEL_BMPOBJECT(m_hbitmapdis);
	return FALSE;
}

BOOL CMyFourButton::SetupRegionBitmap(LPCTSTR lpregion)
{
	HDC hDC;
	BITMAP bm     = {0};
	HBITMAP	hBitmap = NULL;
	hBitmap = (HBITMAP)::LoadImage(NULL,lpregion,
		IMAGE_BITMAP,
		0,0,
		LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
	if(hBitmap == NULL) return FALSE;

	GetObject(hBitmap,sizeof(BITMAP),&bm);
	m_nWidth   = bm.bmWidth;
	m_nHeight  = bm.bmHeight;
	
	hDC = ::GetWindowDC(m_hWnd);

	int x,y;
	HRGN hwndRgn,hrgnTemp;
	HDC hmemDC      = CreateCompatibleDC(hDC);
	HBITMAP hOldBmp = (HBITMAP)SelectObject(hmemDC,hBitmap);
	hwndRgn = CreateRectRgn(0, 0, m_nWidth,m_nHeight);
	for(y=0;y<m_nHeight;y++)
	{
		for(x=0;x<m_nWidth;x++)
		{
			if(GetPixel(hmemDC,x,y) == TRANPTZ_LEFT_COLOR)
			{
				hrgnTemp = CreateRectRgn(x, y, x+1, y+1);
				CombineRgn(hwndRgn,hwndRgn, hrgnTemp, RGN_XOR);
				DeleteObject(hrgnTemp);	
			}
		}
	}
	
	SelectObject(hmemDC,hOldBmp);
	DeleteDC(hmemDC);

	SetWindowRgn(hwndRgn, TRUE);	
	DeleteObject(hwndRgn);	

	::ReleaseDC(m_hWnd,hDC);
	DEL_BMPOBJECT(hBitmap);
	return TRUE;
}

void CMyFourButton::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	HDC hDC = dc.m_hDC;
	//´´½¨Í¼Æ¬DC
	HDC hdc;
	//»­Í¼
	HBITMAP hOldb;
	if(IsWindowEnabled())
	{
		if(m_state && m_hBitmapdown)// && m_pos != 2)
		{
			hdc = CreateCompatibleDC(hDC);
			hOldb = HBITMAP(SelectObject(hdc,m_hBitmapdown));
			BitBlt(	hDC,0,0,m_nWidth,m_nHeight,hdc,0,0,SRCCOPY);
			SelectObject(hdc,hOldb);
			DeleteDC(hdc);
			return;
		}
		if(m_pos == 0 && m_hBitmap)
		{
			hdc = CreateCompatibleDC(hDC);
			hOldb = HBITMAP(SelectObject(hdc,m_hBitmap));
			BitBlt(	hDC,0,0,m_nWidth,m_nHeight,hdc,0,0,SRCCOPY);
			SelectObject(hdc,hOldb);
			DeleteDC(hdc);
		}
		else if(m_pos == 1 && m_hBitmapdown)
		{
			hdc = CreateCompatibleDC(hDC);
			hOldb = HBITMAP(SelectObject(hdc,m_hBitmapdown));
			BitBlt(	hDC,0,0,m_nWidth,m_nHeight,hdc,0,0,SRCCOPY);
			SelectObject(hdc,hOldb);
			DeleteDC(hdc);
		}
		else if(m_pos == 2 && m_hbitmapfocus)
		{
			hdc = CreateCompatibleDC(hDC);
			hOldb = HBITMAP(SelectObject(hdc,m_hbitmapfocus));
			BitBlt(	hDC,0,0,m_nWidth,m_nHeight,hdc,0,0,SRCCOPY);
			SelectObject(hdc,hOldb);
			DeleteDC(hdc);
		}
		else if(m_hBitmap)
		{
			hdc = CreateCompatibleDC(hDC);
			hOldb = HBITMAP(SelectObject(hdc,m_hBitmap));
			BitBlt(	hDC,0,0,m_nWidth,m_nHeight,hdc,0,0,SRCCOPY);
			SelectObject(hdc,hOldb);
			DeleteDC(hdc);
		}
	}
	else
	{
		if(m_hbitmapdis)
		{
			hdc = CreateCompatibleDC(hDC);
			hOldb = HBITMAP(SelectObject(hdc,m_hbitmapdis));
			BitBlt(	hDC,0,0,m_nWidth,m_nHeight,hdc,0,0,SRCCOPY);
			SelectObject(hdc,hOldb);
			DeleteDC(hdc);
		}
		else
		{
			hdc = CreateCompatibleDC(hDC);
			hOldb = HBITMAP(SelectObject(hdc,m_hBitmap));
			BitBlt(	hDC,0,0,m_nWidth,m_nHeight,hdc,0,0,SRCCOPY);
			SelectObject(hdc,hOldb);
			DeleteDC(hdc);
		}
	}
}

void CMyFourButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if(m_hBitmapdown)
	{
		m_pos = 1;
		Invalidate();
	}
	m_bIn = TRUE;
	//CWnd::OnLButtonDown(nFlags, point);
	MOUSE_ENVENT(m_nID,MOUSE_LEFT_DOWN,nFlags,m_pWnd,point);
	RelayEvent(WM_LBUTTONUP, (WPARAM)nFlags,
		MAKELPARAM(LOWORD(point.x), LOWORD(point.y)));	
}

void CMyFourButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(!this->m_bIn)
	{
		RelayEvent(WM_LBUTTONDOWN, (WPARAM)nFlags,
			MAKELPARAM(LOWORD(point.x), LOWORD(point.y)));	
		return;
	}
	CRect rc;
	GetClientRect(&rc);
	this->m_bIn=FALSE;
	if(rc.PtInRect(point))
	{
	    m_pos=0;
	    Invalidate();
		ReleaseCapture();
		m_bCapture = FALSE;
		MOUSE_ENVENT(m_nID,MOUSE_LEFT_UP,nFlags,m_pWnd,point);
	}
	else
	{
		m_pos=0;
	    Invalidate();
		ReleaseCapture();
		m_bCapture = FALSE;
	}
	RelayEvent(WM_LBUTTONDOWN, (WPARAM)nFlags,
		MAKELPARAM(LOWORD(point.x), LOWORD(point.y)));	
}

void CMyFourButton::OnMouseMove(UINT nFlags, CPoint point) 
{
	if (!m_bCapture)
	{
		SetCapture();
		m_bCapture = TRUE;
		//m_oldpos   = m_pos;
		if(m_hbitmapfocus)
		{
			m_pos = 2;
			Invalidate();
		}
	}
	else
	{
		CRect rc;
		GetClientRect(&rc);
		if (!rc.PtInRect(point))
		{
			if(m_pos != 0)
			{
			  m_pos = 0;
			  Invalidate();
			}
			if(!this->m_bIn)
			{
				ReleaseCapture();
				m_bCapture = FALSE;
			}
		}
		else if(this->m_bIn)
		{
			if(m_pos != 1)
			{
			  //m_oldpos = m_pos;
			  if(m_hBitmapdown)
			  {
				  m_pos    = 1;
				  Invalidate();
			  }
			}
		}
	}
	RelayEvent(WM_MOUSEMOVE, (WPARAM)nFlags,
		MAKELPARAM(LOWORD(point.x), LOWORD(point.y)));
	//CWnd::OnMouseMove(nFlags, point);
}

void CMyFourButton::SetPosition(int x,int y)
{
	MoveWindow(x,y,m_nWidth,m_nHeight);
}

void CMyFourButton::OnEnable(BOOL bEnable) 
{
	Invalidate();
	CWnd::OnEnable(bEnable);	
}

void CMyFourButton::SetState(int state)
{
	if(m_state != state)
	{
		m_state = state;
		Invalidate();
	}
}
void CMyFourButton::UpdateTipTool(LPCTSTR string)
{
	m_tooltip.UpdateTipText(string,this);
}
void CMyFourButton::RelayEvent(UINT message, WPARAM wParam, LPARAM lParam)
{
	if (NULL != m_tooltip.m_hWnd) {
		MSG msg;
		
		msg.hwnd= m_hWnd;
		msg.message= message;
		msg.wParam= wParam;
		msg.lParam= lParam;
		msg.time= 0;
		msg.pt.x= LOWORD (lParam);
		msg.pt.y= HIWORD (lParam);
		
		m_tooltip.RelayEvent(&msg);
	}
}
