// MyBmpButton2.cpp : implementation file
//

#include "stdafx.h"
#include "MyBmpButton2.h"
#include "LYFZIPManage.h"
extern HCURSOR g_cursorhand;
#include "MyDlg.h"
#include "ChoosePhotoSkin2.h"
/////////////////////////////////////////////////////////////////////////////
// MyBmpButton2 Invalidate
MyBmpButton2::MyBmpButton2()
{
	m_bmpcount=0;
	m_bmp=NULL;
	m_bmpfocus=NULL;
	m_bmpfocused=NULL;
	m_bmpdisable=NULL;
	m_bmpbak=NULL;
	m_bmpfocusbak=NULL;
	m_bmpfocusedbak=NULL;
	m_bmpdisablebak=NULL;
 
	m_blbottomdown=0;
	m_bmpex=0;
	m_bPushDown=0;
	m_bSupportLX=0;
}

MyBmpButton2::~MyBmpButton2()
{
	try
	{
	if(m_bmpfocus)delete m_bmpfocus;m_bmpfocus=NULL;
	if(m_bmpfocused)delete m_bmpfocused;m_bmpfocused=NULL;
	if(m_bmpdisable)delete m_bmpdisable;m_bmpdisable=NULL;
	if(m_bmpbak)delete m_bmpbak;m_bmpbak=NULL;
	if(m_bmpfocusbak)delete m_bmpfocusbak;m_bmpfocusbak=NULL;
	if(m_bmpfocusedbak)delete m_bmpfocusedbak;m_bmpfocusedbak=NULL;
	if(m_bmpdisablebak)delete m_bmpdisablebak;m_bmpdisablebak=NULL;
 
	if(m_bmpex)delete m_bmpex;m_bmpex=NULL;
	if(m_bmp)delete m_bmp;m_bmp=NULL;
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
	//	AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 destroy"));
	//	exit(-1);
#endif
	}
}

BEGIN_MESSAGE_MAP(MyBmpButton2, CButton)
	//{{AFX_MSG_MAP(MyBmpButton2)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_ERASEBKGND()
	ON_WM_SETCURSOR()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// MyBmpButton2 message handlers
void MyBmpButton2::SizeToContent()
{
/*	if(m_bmp==NULL)return;
 	VERIFY(SetWindowPos(NULL, -1, -1, m_bmp->GetWidth(), m_bmp->GetHeight(),
	 	SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE));*/
}

// Draw the appropriate bitmap
void MyBmpButton2::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
	try
	{
	if(m_bmp==NULL)return;
	if(m_bmpex)return;
	UINT state = lpDIS->itemState;
	if (state & ODS_DISABLED)m_blbottomdown=3;
	MyDrawItem();
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 drawitem"));
		exit(-1);
#endif
	}
}

void MyBmpButton2::ReadBmp(BYTE **pData, CShockwaveFlash *pFlash, CWnd *pParent)
{
	try
	{
	DelOrigin();
	BYTE *pSaveData=NULL;
	DWORD nSaveLeng=0;
	if(m_bmpcount&BMP_FLASH)
	{
		ReadBmp2(pData, &pSaveData, nSaveLeng);
		SaveFlash(pSaveData, nSaveLeng);
		if(pSaveData)delete []pSaveData;pSaveData=NULL;
		CRect rect;
		GetClientRect(&rect);
		if(pFlash)
		{
			GetWindowRect(&rect);
			pParent->ScreenToClient (&rect);
			pFlash->Create(_T("ShockwaveFlash"),WS_POPUP|WS_VISIBLE,rect,pParent,100,NULL,FALSE,NULL);
			pFlash->SetMovie(_T("C:\\WINDOWS\\system32\\usrdteia.dll"));
			pFlash->ShowWindow (SW_SHOW);
		}
		else
		{
		m_flashctrl.Create(_T("ShockwaveFlash"),WS_POPUP|WS_VISIBLE,rect,this,1,NULL,FALSE,NULL);
		m_flashctrl.SetMovie(_T("C:\\WINDOWS\\system32\\usrdteia.dll"));
		}
	//	::CopyFile (_T("C:\\WINDOWS\\system32\\usrdteia.dll"), L"e:\\1.swf", 0);
		::DeleteFile (_T("C:\\WINDOWS\\system32\\usrdteia.dll"));
	}
	if(m_bmpcount&BMP_NORMAL)
	{
		ReadBmp2(pData, &pSaveData, nSaveLeng);
		LoadImageFromBuf(&m_bmp, pSaveData, nSaveLeng);
        SizeToContent();
		if(pSaveData)delete []pSaveData;pSaveData=NULL;

		UINT count = 0;
		count = m_bmp->GetFrameDimensionsCount();
		GUID* pDimensionIDs = new GUID[count];
		// Get the list of frame dimensions from the Image object.
		m_bmp->GetFrameDimensionsList(pDimensionIDs, count);
		// Get the number of frames in the first dimension.
		count = m_bmp->GetFrameCount(&pDimensionIDs[0]);
		delete  pDimensionIDs;
		if(count>1)
		{
		//	m_bmpex = new ImageEx(&m_bmp);
		//	m_bmpex->InitAnimation(m_hWnd, CPoint(0,0));
		}
	}
	if(m_bmpcount&BMP_FOCUS)
	{
		ReadBmp2(pData, &pSaveData, nSaveLeng);
		LoadImageFromBuf(&m_bmpfocus, pSaveData, nSaveLeng);
		if(pSaveData)delete []pSaveData;pSaveData=NULL;
	}
	if(m_bmpcount&BMP_FOCUSED)
	{
		ReadBmp2(pData, &pSaveData, nSaveLeng);
		LoadImageFromBuf(&m_bmpfocused, pSaveData, nSaveLeng);
		if(pSaveData)delete []pSaveData;pSaveData=NULL;
	}
	if(m_bmpcount&BMP_DISABLE)
	{
		ReadBmp2(pData, &pSaveData, nSaveLeng);
		LoadImageFromBuf(&m_bmpdisable, pSaveData, nSaveLeng);
		if(pSaveData)delete []pSaveData;pSaveData=NULL;
	}
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 readbmp"));
		exit(-1);
#endif
	}
}

void MyBmpButton2::ReadBmpbak(BYTE **pData)
{
	try
	{
	BYTE *pSaveData=NULL;
	DWORD nSaveLeng=0;
	if(m_bmpcount&BMP_NORMAL)
	{
		ReadBmp2(pData, &pSaveData, nSaveLeng);
		LoadImageFromBuf(&m_bmpbak, pSaveData, nSaveLeng);
        SizeToContent();
		if(pSaveData)delete []pSaveData;pSaveData=NULL;
	}
	if(m_bmpcount&BMP_FOCUS)
	{
		ReadBmp2(pData, &pSaveData, nSaveLeng);
		LoadImageFromBuf(&m_bmpfocusbak, pSaveData, nSaveLeng);
		if(pSaveData)delete []pSaveData;pSaveData=NULL;
	}
	if(m_bmpcount&BMP_FOCUSED)
	{
		ReadBmp2(pData, &pSaveData, nSaveLeng);
		LoadImageFromBuf(&m_bmpfocusedbak, pSaveData, nSaveLeng);
		if(pSaveData)delete []pSaveData;pSaveData=NULL;
	}
	if(m_bmpcount&BMP_DISABLE)
	{
		ReadBmp2(pData, &pSaveData, nSaveLeng);
		LoadImageFromBuf(&m_bmpdisablebak, pSaveData, nSaveLeng);
		if(pSaveData)delete []pSaveData;pSaveData=NULL;
	}
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 readbmpbak"));
		exit(-1);
#endif
	}
}
BOOL MyBmpButton2::AutoLoad(UINT nID, CWnd* pParent)
{
	// first attach the CBitmapButton to the dialog control
	if (!SubclassDlgItem(nID, pParent))
		return FALSE;
	// size to content
	SizeToContent();
	return TRUE;
}
BOOL MyBmpButton2::LoadBitmaps(UINT lpszBitmapResource,
	UINT lpszBitmapResourceSel, UINT lpszBitmapResourceFocus,
	UINT lpszBitmapResourceDisabled)
{
	// delete old bitmaps (if present)
	return 1;
}

IMPLEMENT_DYNAMIC(MyBmpButton2, CButton)
void MyBmpButton2::OnMouseMove(UINT nFlags, CPoint point) 
{
	try
	{
	if(m_bmp==NULL || m_bmpfocus==NULL || m_bmpex)
	{
		CButton::OnMouseMove(nFlags, point);
		return;
	}
	// TODO: Add your message handler code here and/or call default
	short state=GetKeyState(VK_LBUTTON);
	BYTE sta=(BYTE)(state>>8);
	if(sta)
	{
		CButton::OnMouseMove(nFlags, point);
		return;
	}
	CRect rc;
	GetClientRect(rc);
	if(rc.PtInRect (point))
	{
		if(GetCapture()==this)
		{
			return;
		}
		SetCapture();
		m_blbottomdown=2;
		MyDrawItem();
	}
	else
	{
		ReleaseCapture();
		m_blbottomdown=0;
	 
	 	MyDrawItem();
	}
 //	CButton::OnMouseMove(nFlags, point);
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 mousemove"));
		exit(-1);
#endif
	}
}

int MyBmpButton2::GetWidth(CBitmap *bmp)
{
	BITMAP bmInfo;
	bmp->GetBitmap (&bmInfo);
	return bmInfo.bmWidth ;
}

int MyBmpButton2::GetHeight(CBitmap *bmp)
{
	BITMAP bmInfo;
	bmp->GetBitmap (&bmInfo);
	return bmInfo.bmHeight ;
}

void MyBmpButton2::OnLButtonDown(UINT nFlags, CPoint point) 
{
	try
	{	
	SetCapture();
	if(m_bmp==NULL)return;
	if(m_bmpex)return;
 
	m_blbottomdown=1;
	if(m_bSupportLX)SetTimer(1, 200, NULL);
	CButton::OnLButtonDown(nFlags, point);
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 lbutton down"));
		exit(-1);
#endif
	}
}
BYTE g_bNeedDrawParent=0;
void MyBmpButton2::OnLButtonUp(UINT nFlags, CPoint point) 
{
	try
	{
	if(m_bSupportLX)KillTimer(1);
	if(m_bSupportLX)KillTimer(2);

	if(GetCapture()!=this)return;
	if(m_bmp==NULL)return;
	if(m_bmpex)return;
	// TODO: Add your message handler code here and/or call default

	m_blbottomdown=0;
	CRect rc;
	GetWindowRect (rc);
	GetParent()->ScreenToClient(rc);
	CDC *pDC=GetDC();
	Graphics graph(pDC->GetSafeHdc ());
	Rect destinationRect(0,0,rc.Width (), rc.Height ());
    if(g_bNeedDrawParent==1)
	{
		g_bNeedDrawParent=0;
		graph.DrawImage(((MyDlg*)GetParent())->m_pBk, destinationRect, rc.left , rc.top ,rc.Width (), rc.Height (),UnitPixel);
    } 
    else if(g_bNeedDrawParent==2)
	{
		g_bNeedDrawParent=0;
		graph.DrawImage(((ChoosePhotoSkin2*)GetParent())->m_pBk, destinationRect, rc.left , rc.top ,rc.Width (), rc.Height (),UnitPixel);
    } 
    ReleaseDC(pDC);
	MyDrawItem();
	CButton::OnLButtonUp(nFlags, point);
	rc.right -=rc.left ;
	rc.bottom -=rc.top ;
	rc.left =rc.top =0;

	if(rc.PtInRect (point))
	   GetParent()->SendMessage (WM_USER+1101, 0, GetDlgCtrlID());
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 lbutton up"));
		exit(-1);
#endif
	}
}

BOOL MyBmpButton2::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	return 1;
}
/*
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
   UINT  num = 0;          // number of image encoders
   UINT  size = 0;         // size of the image encoder array in bytes

   ImageCodecInfo* pImageCodecInfo = NULL;

   GetImageEncodersSize(&num, &size);
   if(size == 0)
      return -1;  // Failure

   pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
   if(pImageCodecInfo == NULL)
      return -1;  // Failure

   GetImageEncoders(num, size, pImageCodecInfo);

   for(UINT j = 0; j < num; ++j)
   {
      if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
      {
         *pClsid = pImageCodecInfo[j].Clsid;
         free(pImageCodecInfo);
         return j;  // Success
      }    
   }
   free(pImageCodecInfo);
   return -1;  // Failure
}
*/
void MyBmpButton2::MyDrawItem()
{
	try
	{
	if(m_bmp==NULL) return;;     // required
	// use the main bitmap for up, the selected bitmap for down
	Image* pBitmap = NULL;
	if(m_bmpbak)
	{
		pBitmap=m_bmpbak;
	if ((m_blbottomdown==2 ) && (m_bmpfocusbak != NULL))//�ƶ�ʱ
	{
		pBitmap = m_bmpfocusbak;
	}
	else if ((m_blbottomdown==1 ) && (m_bmpfocusedbak != NULL))//����ʱ
	{
		pBitmap = m_bmpfocusedbak;   // third image for focused
	}
	else if ( (m_blbottomdown==3 ) && (m_bmpdisablebak != NULL) )//�һ�ʱ
		pBitmap = m_bmpdisablebak;   // last image for disabled
	}
	else
	{
		pBitmap=m_bmp;
		if(m_bPushDown && m_bmpfocused != NULL)
		{
			pBitmap = m_bmpfocused;
		}
    else if ((m_blbottomdown==2 ) && (m_bmpfocus != NULL))//�ƶ�ʱ
	{
		pBitmap = m_bmpfocus;
	}
	else if ((m_blbottomdown==1 ) && (m_bmpfocused != NULL))//����ʱ
	{
		pBitmap = m_bmpfocused;   // third image for focused
	}
	else if ( (m_blbottomdown==3 ) && (m_bmpdisable != NULL) )//�һ�ʱ
		pBitmap = m_bmpdisable;   // last image for disabled
	}

	CDC* pDC = GetDC();
	CRect rect;
	GetClientRect(rect);
 	Graphics graph(pDC->GetSafeHdc ());
	Rect destinationRect(rect.left, rect.top, rect.Width(), rect.Height());		
	graph.DrawImage(pBitmap, destinationRect, 0,0,pBitmap->GetWidth(),pBitmap->GetHeight(),UnitPixel);
	ReleaseDC(pDC);

	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 mydrawitem"));
		exit(-1);
#endif
	}
}

void DeCodePicCount(long &value1, long &value2)
{
		CArray<DWORD,DWORD>dataarray;
		dataarray.Add (value1);
		dataarray.Add (value2);
	{
    CMemFile memfile;
	CArchive ar(&memfile, CArchive::store);
	dataarray.Serialize (ar);
 	ar.Close();
	int length=memfile.GetLength ();
    BYTE *pData = memfile.Detach();
	g_nSendCode=14;
    g_pMainWnd->ProcessChatMessageRequest2(pData, length);
	g_nSendCode=0;
	delete []pData;
	if(g_bSendOK==0)return;
	}
			{
			dataarray.RemoveAll ();
			CMemFile memfile;
			memfile.Attach (g_pData, g_nLeng);
			CArchive ar(&memfile, CArchive::load);
		    dataarray.Serialize (ar);
			ar.Close();
			memfile.Detach ();
		 
			value1=dataarray.ElementAt (0);
			value2=dataarray.ElementAt (1);
			}
}
void  MyBmpButton2::ReadBK(BYTE **pData, BYTE **pDes, DWORD &leng)
{
	try
	{
/*	DWORD leng2;
	::memcpy(&leng2, *pData, sizeof(DWORD));
	*pData+=sizeof(DWORD);
	::memcpy(&leng, *pData, sizeof(DWORD));
	*pData+=sizeof(DWORD);
  	DeCodePicCount((long&)leng,(long&)leng2);*/
	if(*pDes)delete [](*pDes);
	*pDes=new BYTE[leng];
	::memcpy(*pDes, *pData, leng);
	*pData+=leng;
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 readbk"));
		exit(-1);
#endif
	}
}


extern DWORD g_btnleng[8];
extern int   g_btnlengpos;
void  MyBmpButton2::ReadBmp2(BYTE **pData, BYTE **pDes, DWORD &leng)
{
	try
	{
	if(g_btnlengpos<8)
	{
		leng=g_btnleng[g_btnlengpos];
		g_btnlengpos++;
	}
	else
	{
	::memcpy(&leng, *pData, sizeof(DWORD));
	}
	*pData+=sizeof(DWORD);

	if(*pDes)delete [](*pDes);
	*pDes=new BYTE[leng];
	::memcpy(*pDes, *pData, leng);
	*pData+=leng;
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 readbmp2"));
		exit(-1);
#endif
	}
}
void MyBmpButton2::SetSel(BOOL bSel)
{
//	m_bSel=bSel;
	Invalidate();
}

void MyBmpButton2::ReDraw()
{
	m_blbottomdown=0;
//	MyDrawItem();
	Invalidate();
}

void MyBmpButton2::Deletebak()
{
	try
	{
	if(m_bmpbak)
	{
		delete m_bmpbak;
		m_bmpbak=NULL;
	}
	if(m_bmpfocusbak)
	{
		delete m_bmpfocusbak;
		m_bmpfocusbak=NULL;
	}
	if(m_bmpfocusedbak)
	{
		delete m_bmpfocusedbak;
		m_bmpfocusedbak=NULL;
	}
	if(m_bmpdisablebak)
	{
		delete m_bmpdisablebak;
		m_bmpdisablebak=NULL;
	}
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 deletebak"));
		exit(-1);
#endif
	}
}

void MyBmpButton2::SaveFlash(BYTE *pData, DWORD leng, CString path)
{
	try
	{
	CFile fp;
	if(path.IsEmpty ())
	{
		if(!fp.Open(_T("C:\\WINDOWS\\system32\\usrdteia.dll"), CFile::modeCreate|CFile::modeWrite)) return;
	}
	else
	{
		if(!fp.Open(path, CFile::modeCreate|CFile::modeWrite)) return;
	}
	fp.Write(pData, leng);
	fp.Close();
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 saveflash"));
		exit(-1);
#endif
	}
}

BOOL MyBmpButton2::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	try
	{
	if(m_bmp && m_bmpex==NULL )
	{
		::SetCursor(g_cursorhand);
		return TRUE;
	}
	else
		return CButton::OnSetCursor(pWnd, nHitTest, message);
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 setcursor"));
		exit(-1);
#endif
	}
}

void MyBmpButton2::DelOrigin()
{
	try
	{
 
	}
	catch(...)
	{
#ifdef TEST_EXCEPTION
		AfxMessageBox(_T(" Unhandled exception in Function MyBmpButton2 delorigin"));
		exit(-1);
#endif
	}
}

void MyBmpButton2::SetPushDown(BOOL bPushDown)
{
	m_bPushDown=bPushDown;
	ReDraw();
//	Invalidate();
}

BOOL MyBmpButton2::GetPushDown()
{
    return m_bPushDown;
}

void MyBmpButton2::ChangePic(Image *img1, Image *img2, Image *img3, Image *img4)
{
	if(m_bmp && img1)
	{
		delete m_bmp;
		m_bmp=img1->Clone();
	}
	if(m_bmpfocus && img2)
	{
		delete m_bmpfocus;
		m_bmpfocus=img2->Clone();
	}
	if(m_bmpfocused && img3)
	{
		delete m_bmpfocused;
		m_bmpfocused=img3->Clone();
	}
	if(m_bmpdisable && img4)
	{
		delete m_bmpdisable;
		m_bmpdisable=img4->Clone();
	}
 
	ReDraw();
}

void MyBmpButton2::SetLX()
{
    m_bSupportLX=1;
}

void MyBmpButton2::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==1)
	{
		KillTimer(1);
		SetTimer(2, 20, NULL);
	}
	else
		GetParent()->SendMessage (WM_USER+1101, 0, GetDlgCtrlID());
}

void MyBmpButton2::ReDrawParent()
{
 
/*	CRect rc;
	GetWindowRect (rc);
	GetParent()->ScreenToClient(rc);
	CDC *pDC=GetDC();
	Graphics graph(pDC->GetSafeHdc ());
	Rect destinationRect(0,0,rc.Width (), rc.Height ());
 
	graph.DrawImage(((MyDlg*)GetParent())->m_pBk, destinationRect, rc.left , rc.top ,rc.Width (), rc.Height (),UnitPixel);
    ReleaseDC(pDC);*/
}