


#include "mgoldfish.h"
#include <stdlib.h>

void CMGoldFishWnd::OnDestroy()
{
	KillTimer(m_nIDEvent);

	SetMsgHandled(FALSE);

	CleanBitmap();

	delete m_pImageLeft;
	delete m_pImageRight;
	FreeLibrary(hFuncInst); 
	SetMsgHandled(FALSE);
	return ;
}


void  CMGoldFishWnd::OnTimer ( UINT_PTR uTimerID)
{
	if ( m_nIDEvent != uTimerID )
		SetMsgHandled(FALSE);
	else
	{

 		if (!mouseDown)
 		{
 			count++;
 			if (count > MaxCount)
 			{
 				MaxCount = rand()*(70-0)/RAND_MAX+70 + 30;
 				//if (speedMode) timerSpeed.Interval = 50;
 
 				count = 0;
				float r = (float)rand()/(float)RAND_MAX;
 				stepX = r * 3 + 0.5f;
 				stepY = ( r - 0.5f) * 0.5f;
 			}
 
 			left = (left + (toRight ? 1 : -1) * stepX);
 			top = (top + stepY);
 			FixLeftTop();
			::SetWindowPos(m_hWnd, HWND_TOPMOST, left,top, m_pImageLeft->GetWidth()/20 , m_pImageLeft->GetHeight(),SWP_SHOWWINDOW);   

 			//this.Left = (int)left;
 			//this.Top = (int)top;
 		}
 		frame++;
 		if (frame >= frameCount) frame = 0;


		UpdateFishDisplay();
	}
}

void CMGoldFishWnd::FixLeftTop()
{
	int scr_with= GetSystemMetrics(SM_CXFULLSCREEN);
	int scr_heigh= GetSystemMetrics(SM_CYFULLSCREEN);

	if (toRight && left > scr_with)
	{
		toRight = false;
		frame = 0;
		count = 0;
	}
	else if (!toRight && left < -frameWidth)
	{
		toRight = true;
		frame = 0;
		count = 0;
	}
	if (top < -frameHeight)
	{
		stepY = 1;
		count = 0;
	}
	else if (top > scr_heigh)
	{
		stepY = -1;
		count = 0;
	}
}


LRESULT CMGoldFishWnd::OnCreate ( LPCREATESTRUCT lpcs )
{
	SetWindowText(L"MGoldFish");

	m_nIDEvent = TIME_ID;
	SetTimer(m_nIDEvent,50);

	hFuncInst = LoadLibrary(L"User32.DLL"); 
	BOOL bRet=FALSE;
	if(hFuncInst) 
		UpdateLayeredWindow=(MYFUNC)GetProcAddress(hFuncInst, "UpdateLayeredWindow");
	else
	{
		//AfxMessageBox("User32.dll ERROR!");
		exit(0);
	}



	ImageFromIDResource(IDB_PNG_LEFT,L"PNG",m_pImageLeft);
	ImageFromIDResource(IDB_PNG_RIGHT,L"PNG",m_pImageRight);

	m_BakWidth  = m_pImageLeft->GetWidth()/20;
	m_BakHeight = m_pImageLeft->GetHeight();

	frame = 0;
	mouseDown = false;
	haveHandle = false;
	MaxCount = 50;
	stepX = 2;
	stepY = 0;
	count = 0;
	speedMode = false;
	left = 0;
	top = 0;

	toRight = true;
	frameCount = 20;
	frame = 0; 
	frameWidth = 100;
	frameHeight = 100; 
	oldPoint.x = 0;
	oldPoint.y = 0;


	m_Blend.BlendOp=0; 
	m_Blend.BlendFlags=0;
	m_Blend.AlphaFormat=1;
	m_Blend.SourceConstantAlpha=255;

	sizeWindow.cx = m_BakWidth,
	sizeWindow.cy = m_BakHeight;
	 ptSrc.x = 0;
	 ptSrc.y = 0;
	//ModifyStyle(WS_CAPTION,0,SWP_FRAMECHANGED);//去除标题栏
	ModifyStyleEx (WS_EX_APPWINDOW,WS_EX_TOOLWINDOW );//去除任务栏窗口对应按钮
	::SetWindowPos(m_hWnd, HWND_TOPMOST,300,300, m_pImageLeft->GetWidth()/20, m_pImageLeft->GetHeight(),SWP_NOSIZE|SWP_NOMOVE);   


  	DWORD dwExStyle=::GetWindowLong(m_hWnd,GWL_EXSTYLE);
  	if((dwExStyle&0x80000)!=0x80000)
  		::SetWindowLong(m_hWnd,GWL_EXSTYLE,dwExStyle^0x80000);


	SetMsgHandled(FALSE);
	return 0;
}


BOOL CMGoldFishWnd::ImageFromIDResource(UINT nID, LPCTSTR sTR,Image * &pImg)
{
	HINSTANCE hInst = _Module.GetResourceInstance();
	HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),sTR); // type
	if (!hRsrc)
		return FALSE;

	// load resource into memory
	DWORD len = SizeofResource(hInst, hRsrc);
	BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
	if (!lpRsrc)
		return FALSE;

	// Allocate global memory on which to create stream
	HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
	BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
	memcpy(pmem,lpRsrc,len);
	IStream* pstm;
	CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);

	// load from stream
	pImg=Gdiplus::Image::FromStream(pstm);

	// free/release stuff
	GlobalUnlock(m_hMem);
	pstm->Release();
	FreeResource(lpRsrc);

	return TRUE;
}


void CMGoldFishWnd::UpdateFishDisplay()
{
	CreateBitmap();
	
  	RECT rct;
  	GetWindowRect(&rct);
  	POINT ptWinPos={rct.left,rct.top};
	
	Graphics graph(m_hMemDC);

 if (toRight)
	graph.DrawImage(m_pImageRight,0,0, m_BakWidth*frame,0,m_BakWidth,m_BakHeight,UnitPixel); 
 else 
	graph.DrawImage(m_pImageLeft,0,0, m_BakWidth*frame,0,m_BakWidth,m_BakHeight,UnitPixel); 
		
 	BOOL bRet=FALSE;
 	bRet= UpdateLayeredWindow( m_hWnd,m_hWinDC,&ptWinPos,&sizeWindow, m_hMemDC,&ptSrc,0,&m_Blend,ULW_ALPHA);
 		
 	graph.ReleaseHDC(m_hMemDC);
	CleanBitmap();
// 
}


LRESULT CMGoldFishWnd::OnLButtonDown(UINT Flags, CPoint Pt)
{

	//禁止显示移动矩形窗体框
	::SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,TRUE,NULL,0);
	//非标题栏移动整个窗口//
	//SendMessage(WM_SYSCOMMAND,0xF012,0);   
	//	PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));   
//	CDialog::OnLButtonDown(nFlags, point);
	oldPoint = Pt;
	mouseDown = true;
	SetMsgHandled(false);
	return 0;
}


LRESULT CMGoldFishWnd::OnMouseMove(UINT Flags, CPoint Pt)
{
	if (mouseDown)
	{
		left += (Pt.x - oldPoint.x);//就这里
		top += (Pt.y - oldPoint.y);
		::SetWindowPos(m_hWnd, HWND_TOPMOST, left,top, m_pImageLeft->GetWidth()/20 , m_pImageLeft->GetHeight(),SWP_SHOWWINDOW);   

		FixLeftTop();
	}
	SetMsgHandled(false);
	return 0;
}



LRESULT CMGoldFishWnd::OnLButtonUp(UINT Flags, CPoint Pt)
{
	count = 0;
	MaxCount = rand()*(70-0)/RAND_MAX+70 + 40;
	//timerSpeed.Interval = new Random().Next(20) + 2;
	speedMode = true;
	mouseDown = false;
	SetMsgHandled(false);
	return 0;
}

LRESULT CMGoldFishWnd::OnRButtonDblClk(UINT Flags, CPoint Pt)
{
	PostQuitMessage(0);	
	SendMessage(WM_CLOSE,1,0);	
	SetMsgHandled(false);

	return 0;
}


void CMGoldFishWnd::CreateBitmap()
{
	if(rcSize.cx <= 0 || rcSize.cy <= 0)
	{
		// no place to do a bitmap, then get out
		return;
	}

	ATLTRACE(_T("CFireWnd::CreateBitmap()\n"));
	if (m_hMemDC != NULL)
	{
		::SelectObject(m_hMemDC, m_hBitmap);
		DeleteDC(m_hMemDC);
		m_hMemDC = NULL;
		m_pOldBitmap = NULL;
	}

	if (m_hWinDC != NULL)
	{
		//::SelectObject(m_hWinDC,m_hPalette);
		DeleteDC(m_hWinDC);
		m_hWinDC = NULL;
		//m_pOldPalette = NULL;
	}

	if (m_hBitmap != NULL)
		DeleteObject(m_hBitmap);

	// rcSize is the cached size of the window that was
	// set during the call to IOleInPlaceObject_SetObjectRects
	_ASSERTE(rcSize.cx != 0);
	_ASSERTE(rcSize.cy != 0);

	m_hWinDC = GetDC();
	_ASSERTE(m_hWinDC);

	HBITMAP hBitmap = CreateCompatibleBitmap(m_hWinDC,m_BakWidth,m_BakHeight);
	_ASSERTE(hBitmap);

	m_hBitmap = hBitmap;

	m_hMemDC = CreateCompatibleDC(m_hWinDC);
	_ASSERTE(m_hMemDC);

	m_pOldBitmap = (HBITMAP)::SelectObject(m_hMemDC, m_hBitmap );

}


LRESULT CMGoldFishWnd::OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
	ATLTRACE(_T("CFireWnd::OnSize()\n"));
	rcSize.cx = LOWORD(lParam);
	rcSize.cy = HIWORD(lParam);

	CreateBitmap();

	return 0;
}


void CMGoldFishWnd::CleanBitmap()
{
	if (m_hMemDC != NULL)
	{
		::SelectObject(m_hMemDC, m_hBitmap);
		DeleteDC(m_hMemDC);
		m_hMemDC = NULL;
		m_pOldBitmap = NULL;
	}

	if (m_hWinDC != NULL)
	{
		//::SelectObject(m_hWinDC,m_hPalette);
		DeleteDC(m_hWinDC);
		m_hWinDC = NULL;
	}

	if (m_hBitmap != NULL)
		DeleteObject(m_hBitmap);

}