/***********************************************************************
*Copyright (c)2012 , 天际边工作室
*All rights reserved.
**
文件名称：	Main.cpp
*摘要：		程序入口，主程序
**
*修改历史：
*	版本    日期     作者     改动内容和原因
*   ----------------------------------------------------
*	1.0    2012-10-5  wstjb     基本的功能完成
***********************************************************************/

#define WINDOW_CLASS_NAME "SkySide3gby"
#define SCREEN_WIDTH 400
#define SCREEN_HEIGHT 300
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "GameEng.h"
#include <fstream>
#include <time.h>
#include<Shlobj.h>  
#include<shlwapi.h>
#include "resource.h" 
#include "Dialog.h" 

#pragma comment(lib,"Shell32.lib")  
#pragma comment(lib,"shlwapi.lib")  

HWND hDlgModeless;
RECT rect;
HWND hwnd;
HDC hdc;
HDC hdcMem;
HBITMAP hbmp;
HBITMAP hbmp2;
HDC hdcTmp;
unsigned int active;//本窗口是否为活动
PAINTSTRUCT ps;
BITMAP bmp;
HINSTANCE hInst;

Game game;//=NULL;
char texta[TEXTAMAX][20];
char title[TEXTAMAX];
int flag=0;
//SIZE size;
int version_now=0;
int color=0;
int menushow1=1;
int menumove1=1;
int menuspeed1=1;
extern int highAtk;
extern int alwaysSave;
extern int network;
//按键定义
int KeyReturn=13;
int KeyEsc=27;
int KeyHome=36;
int KeySpace=32;
int KeyLeft0=37;
int KeyUp0=38;
int KeyRight0=39;
int KeyDown0=40;

int widthx;//20170330改全局
int heighty;//20170330改全局
int MouseStartX = 0;//20170205
int MouseStartY = 0;//20170205
int MouseEndX = 0;//20170205
int MouseEndY = 0;//20170205
int MouseDown = 0;//左键已经按下
int MouseWheelLen = 0;//滚轮距离
double scaleTimes = 0.0;//20170330

char workpath[MAX_PATH];   
char absolutepath[MAX_PATH];

int KeyEnable;
int MouseEnable;

LRESULT CALLBACK WinSkySideProc(	//声明窗口函数
								HWND hwnd,      // handle to window
								UINT uMsg,      // message identifier
								WPARAM wParam,  // first message parameter
								LPARAM lParam   // second message parameter
								);

/***********************************************************************
* 说明:     windows主函数
* 输入参数: 略
* 返回值  : 略
* 修改历史:
*               姓名            日期             说明
*             ------          ----------      -------------
*             wstjb          2012-10-5       完成基本功能
***********************************************************************/
int WINAPI WinMain(
	HINSTANCE hInstance,      // handle to current instance
	HINSTANCE hPrevInstance,  // handle to previous instance
	LPSTR lpCmdLine,          // command line
	int nCmdShow              // show state
	)
{
	wlogfile(5,__FUNCTION__,"开始",0);
	WNDCLASSEX wndcs;
	MSG msg;
	char version[50];
	//HDC hdc;
	hInst = hInstance;

	
    GetModuleFileName(NULL,(LPSTR)workpath,sizeof(workpath));   
	char *p = strrchr(workpath, '\\');  
	*p=0x00;  
	
	sprintf_s(absolutepath,"%s\\%s",workpath,"Res\\startup.ini");
	char versionpath[MAX_PATH];
	sprintf_s(versionpath,"%s\\%s",workpath,"Res\\version.ini");

	widthx = GetPrivateProfileInt("Startup", "Width", 800, absolutepath );
	heighty = GetPrivateProfileInt("Startup", "Height", 600, absolutepath );
	version_now=GetPrivateProfileInt("Startup", "Version", 130818100, versionpath );//130325021;
	color=GetPrivateProfileInt("Startup", "Color", 0, absolutepath );
	menushow1=GetPrivateProfileInt("Startup", "Menushow", 0, absolutepath );
	menumove1=GetPrivateProfileInt("Startup", "Menumove", 0, absolutepath );
	menuspeed1=GetPrivateProfileInt("Startup", "Menuspeed", 0, absolutepath );

	KeyReturn=GetPrivateProfileInt("Key", "Return", 13, absolutepath );
	KeyEsc=GetPrivateProfileInt("Key", "Esc", 27, absolutepath );
	KeySpace=GetPrivateProfileInt("Key", "Space" ,32, absolutepath );
	KeyHome=GetPrivateProfileInt("Key", "Home", 36, absolutepath );
	KeyUp0=GetPrivateProfileInt("Key", "Up", 38, absolutepath );
	KeyDown0=GetPrivateProfileInt("Key", "Down", 40, absolutepath );
	KeyLeft0=GetPrivateProfileInt("Key", "Left", 37, absolutepath );
	KeyRight0=GetPrivateProfileInt("Key", "Right", 39, absolutepath );

	KeyEnable = GetPrivateProfileInt("Startup", "UseKey", 1, absolutepath );
	MouseEnable = GetPrivateProfileInt("Startup", "UseMouse", 1, absolutepath );

	int DebugEnable = GetPrivateProfileInt("Debug", "Enable", 0, absolutepath );
	memset(title,0,sizeof(title));
	GetPrivateProfileString("Startup", "Title", "天际边三国霸业",title, TEXTAMAX,versionpath );
	
	wndcs.cbSize =sizeof(WNDCLASSEX); 
	wndcs.cbClsExtra =0;
	wndcs.cbWndExtra =0;
	wndcs.hbrBackground =(HBRUSH)GetStockObject(BLACK_BRUSH);
	wndcs.hCursor =LoadCursor(NULL,IDC_ARROW);
	wndcs.hIcon =LoadIcon(hInstance, "about");
	wndcs.hInstance =hInstance;
	wndcs.lpfnWndProc =WinSkySideProc;
	wndcs.lpszClassName =WINDOW_CLASS_NAME;
	wndcs.lpszMenuName =MAKEINTRESOURCE(IDR_MENU1);
	wndcs.style = CS_DBLCLKS|CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
	wndcs.hIconSm = LoadIcon(hInstance, "about");
	//hinstance_app=hinstance;

	RegisterClassEx(&wndcs);
	
	sprintf_s(version,"%s ver%d.%02d",title,version_now%1000/100,version_now - version_now/100*100);
	hwnd=CreateWindowEx(NULL,
		WINDOW_CLASS_NAME,
		version,
		WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU| WS_VISIBLE,
		CW_USEDEFAULT,CW_USEDEFAULT,
		widthx+10,heighty+20,
		NULL,
		NULL,
		hInstance,
		NULL);

	ShowWindow (hwnd, nCmdShow) ;//显示Windows窗口
	UpdateWindow (hwnd) ;//修改Windows窗口

	/*	hdcPaint=GetDC(hwnd);
	hdc =CreateCompatibleDC(hdcPaint);
	hbmp=CreateCompatibleBitmap(hdcPaint,size.cx,size.cy);
	SelectObject(hdc,hbmp);*/
	hdc = GetDC(hwnd );

	SetMapMode(hdc, MM_ISOTROPIC);    
	SetWindowExtEx(hdc, 800, 600, NULL); 
	SetViewportExtEx(hdc, widthx, heighty, NULL);

	hdcMem  = CreateCompatibleDC(hdc);
	hdcTmp  = CreateCompatibleDC(hdc);

	if(color==1)
	{
		SetBkMode(hdc, TRANSPARENT);
		SetBkMode(hdcMem, TRANSPARENT);
		SetBkMode(hdcTmp, TRANSPARENT);
	}
	//hbmp=CreateCompatibleBitmap(hdc,800,600);
	hbmp=(HBITMAP)LoadImage(NULL, "Res\\01a.sbp", IMAGE_BITMAP,0,0, LR_LOADFROMFILE);
	//hbmp2=CreateCompatibleBitmap(hdc,800,600);
	hbmp2=(HBITMAP)LoadImage(NULL, "Res\\01a.sbp", IMAGE_BITMAP,0,0, LR_LOADFROMFILE);
	SelectObject(hdcTmp,hbmp);
	SelectObject(hdcMem,hbmp2);
	DeleteObject(hbmp);
	DeleteObject(hbmp2);
	srand((unsigned)time(0));

	if(DebugEnable == 1)
	{
		SetEnableDebug();
	}

	if(1==GetPrivateProfileInt("Startup", "Screen", 0, absolutepath ))
	{
		HWND    hDesk;     
		RECT    rc;     
		hDesk   =    GetDesktopWindow();     
		GetWindowRect(    hDesk,   &rc    );     
		SetWindowLong(    hwnd,    GWL_STYLE,    WS_BORDER    );     
		SetWindowPos( hwnd,    HWND_TOP,0,0, rc.right, rc.bottom,  SWP_SHOWWINDOW); 
		widthx=GetSystemMetrics(SM_CXSCREEN);
		heighty=GetSystemMetrics(SM_CYSCREEN);
		SetViewportExtEx(hdc, widthx, heighty, NULL);
		SetMenu(hwnd,NULL);
	}
	

	MyCreateFont(1.0);//字体初始化

	SetUnhandledExceptionFilter(TopLevelExceptionFilter); 
	SetTimer (hwnd, 1, 1500, NULL) ;
	//game=new Game();
	game.Game_Init();
	game.Game_Main();

	//int *p = NULL;
	//*p=1; 
	/*while(1)

	{
	if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
	{
	if(!GetMessage(&msg,NULL,0,0))
	return msg.wParam;
	TranslateMessage(&msg);
	DispatchMessage(&msg);
	}
	else if(active)
	{
	game.Game_Main();
	}
	else
	WaitMessage();
	}*/
	while (GetMessage (&msg, NULL, 0, 0))
	{
		if(hDlgModeless==0||!IsDialogMessage(hDlgModeless,&msg))
		{
			TranslateMessage (&msg) ;
			DispatchMessage (&msg) ;
			Sleep(5);
		}
		else if(IsDialogMessage(hDlgModeless,&msg))
		{
			if (((&msg)->message == WM_KEYDOWN) && ((&msg)->wParam == VK_RETURN))
			{
				TranslateMessage (&msg) ;
				DispatchMessage (&msg) ;
			}
			if (((&msg)->message == WM_KEYDOWN) && ((&msg)->wParam == VK_TAB))
			{
				TranslateMessage (&msg) ;
				DispatchMessage (&msg) ;
			}
			if (((&msg)->message == WM_KEYDOWN) && ((&msg)->wParam == VK_ESCAPE))
			{
				TranslateMessage (&msg) ;
				DispatchMessage (&msg) ;
			}
		}
	}

	//game.Game_Shutdown();

	return (msg.wParam);
}


/***********************************************************************
* 说明:     窗口函数
* 输入参数: 略
* 返回值  : 略
* 修改历史:
*               姓名            日期             说明
*             ------          ----------      -------------
*             wstjb          2012-10-5       完成基本功能
***********************************************************************/
LRESULT CALLBACK WinSkySideProc(
	HWND hwnd,      // handle to window
	UINT uMsg,      // message identifier
	WPARAM wParam,  // first message parameter
	LPARAM lParam   // second message parameter
	)
{
	//wlogfile(1,__FUNCTION__,"开始",0);
	SetUnhandledExceptionFilter(TopLevelExceptionFilter); 
	switch (uMsg)
	{
	case WM_CREATE:
		{
		RECT rc, rc1, rctomove;
		int width = GetSystemMetrics(SM_CXSCREEN);
		int height = GetSystemMetrics(SM_CYSCREEN);
		rc.left = 0;
		rc.top = 0;
		rc.right = width+10;
		rc.bottom = height+50;
		GetClientRect(hwnd, &rc1);
		rc1.right +=10;
		rc1.bottom +=50;
		rctomove.left = (rc.right - rc.left) / 2 - (rc1.right - rc1.left) / 2;
		rctomove.right =  widthx + 10;
		rctomove.top = (rc.bottom - rc.top) / 2 - (rc1.bottom - rc1.top) / 2;
		rctomove.bottom = heighty + 50;
		//设置窗体位置
		SetWindowPos(hwnd,HWND_TOP,rctomove.left, rctomove.top, rctomove.right, rctomove.bottom,SWP_SHOWWINDOW);
		break;
		}
	case WM_ACTIVATEAPP:
		active=wParam;
		SetFocus(hwnd);

		break;
		//case WM_LBUTTONDOWN:
		//	ShowCG(hdc, hdcMem,200,hbmp,bmp,"Res\\2037.sbp");
		//	ShowCG(hdc, hdcMem,200,hbmp,bmp,"Res\\a.sbp");
		//	TextOut(hdc,700 ,100,"dsdsd",strlen("dsdsd"));
		//	Delay(5000);
		//return 0;
	case	WM_DESTROY:
		//ReleaseObjects();
		DeleteLog();
		PostQuitMessage (0) ;
		break;
	case 	WM_PAINT:
		//HDC hdcPaint;
		//hdcPaint=GetDC(hwnd);
		//hdcPaint = BeginPaint(hwnd, &ps); 
		/*hdc =CreateCompatibleDC(hdcPaint);
		hbmp=CreateCompatibleBitmap(hdcPaint,size.cx,size.cy);
		SelectObject(hdc,hbmp);
		//hdc = GetDC(hwnd );
		hdcMem  = CreateCompatibleDC(hdcPaint);
		SelectObject(hdcMem,hbmp);*/
		if(flag==0)
		{
			HDC hdcPaint;
			hdcPaint = BeginPaint(hwnd, &ps); 
			BitBlt(hdcPaint,0,0,800,600,hdcTmp,0,0,SRCCOPY);
			EndPaint(hwnd, &ps); 
		}
		else
		{
			hdc = BeginPaint(hwnd, &ps); 
			BitBlt(hdc,0,0,800,600,hdcTmp,0,0,SRCCOPY);
			EndPaint(hwnd, &ps); 
		}
		//EndPaint(hwnd, &ps); 
		break;   

	case WM_SIZE:
		//size.cx  = LOWORD(lParam);
		//size.cy  = HIWORD(lParam);

		return 0;

		/*case WM_CLOSE:
		if(IDYES==MessageBox(hwnd,"是否退出?","提示",MB_YESNO))
		{
		PostQuitMessage(0);
		//DestroyWindow(hwnd);
		}
		break;*/
	case WM_TIMER:
		{
			game.newgame.who.gamemap.DelTimeEvent();
			break;
		}

	case WM_KEYDOWN:

		{
			game.KeyDown(wParam);
			break;
		}

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{	
		case IDM_EXIT:
			PostQuitMessage(0);
			break;
		case IDM_ABOUT:
			//char word[200];
			//sprintf_s(word,"天际边三国霸业 ver%d.%02d\n天际边工作室 荣誉出品",version_now%1000/100,version_now - version_now/100*100);

			//MessageBox(hwnd,word,"关于",MB_OK);  
			DialogBox(hInst,LPCTSTR(IDD_ABOUT),hwnd,(DLGPROC)AboutProc);
			break;
		case IDM_KEY:
			MessageBox(hwnd,"实际按键控制以使用按键配置修改后的信息为准，以下为初始按键设置\n按键操作：\n方向键——移动 \nEnter（回车）——确认 \nEsc——退出、返回或菜单 \nSpace（空格）——战争界面可查看武将信息 \nHome——地图界面显示城市连接信息，战争界面显示当前战场情况 \nTab ——地图界面设置高攻击概率 \nEnd ——调用窗口配置程序\n\n鼠标操作：\n左键点击选择或在菜单外关闭活动菜单\n滚轮滚动列表及菜单","按键说明",MB_OK);  
			break;
		case IDM_NEW:
			ShellExecute(hwnd,"open","notepad.exe","天际边三国霸业版本更新记录.txt","",SW_SHOW );
			break;
		case IDM_WINDOWINI:
			DialogBox(hInst,LPCTSTR(IDD_WINDOW),hwnd,(DLGPROC)WindowCfgProc);
			//ShellExecute(hwnd,"open","天际边三国霸业窗口配置程序.exe","","",SW_SHOW );
			break;
		case IDM_KEYINI:
			hDlgModeless = CreateDialog(hInst,LPCTSTR(IDD_KEYINI),hwnd,(DLGPROC)KeyCfgProc);
			ShowWindow (hDlgModeless, SW_SHOW) ;
			//DialogBox(hInst,LPCTSTR(IDD_KEYINI),hwnd,(DLGPROC)KeyCfgProc);
			//ShellExecute(hwnd,"open","天际边三国霸业按键配置程序.exe","","",SW_SHOW );
			break;
		case IDM_ONLINEHELP:
			ShellExecute(hwnd,"open","http://3gby.skysidestudio.com/help/",NULL,NULL,SW_SHOW );
			break;
		case IDM_HIGHATT:
			{
			HMENU hMenu = GetMenu(hwnd);
			HMENU hSubMenu = GetSubMenu(hMenu,2);

			if(highAtk == 0)
			{
				highAtk = 1;
				network = 0;
				CheckMenuItem ( hSubMenu,IDM_HIGHATT,MF_CHECKED  );
				CheckMenuItem ( hSubMenu,IDM_NORMAL,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenu,IDM_NETWORK,MF_UNCHECKED  );
				TimerMessage("开启","高攻击概率模式成功！",100,450,10,1,1000);
			}
			
			DeleteObject(&hSubMenu);
			DeleteObject(&hMenu);
			break;
			}
		case IDM_NORMAL:
			{
			HMENU hMenu = GetMenu(hwnd);
			HMENU hSubMenu = GetSubMenu(hMenu,2);

			if(highAtk == 1|| network == 1)
			{
				highAtk = 0;
				network = 0;
				CheckMenuItem ( hSubMenu,IDM_NORMAL,MF_CHECKED  );
				CheckMenuItem ( hSubMenu,IDM_HIGHATT,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenu,IDM_NETWORK,MF_UNCHECKED  );
				TimerMessage("开启","普通模式成功！",100,450,10,1,1000);
			}
			DeleteObject(&hSubMenu);
			DeleteObject(&hMenu);
			break;
			}
		case IDM_NETWORK:
			{
			HMENU hMenu = GetMenu(hwnd);
			HMENU hSubMenu = GetSubMenu(hMenu,2);

			if(network == 0)
			{
				network = 1;
				highAtk = 0;
				CheckMenuItem ( hSubMenu,IDM_NETWORK,MF_CHECKED  );
				CheckMenuItem ( hSubMenu,IDM_NORMAL,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenu,IDM_HIGHATT,MF_UNCHECKED  );
				TimerMessage("开启","网络模式成功！",100,450,10,1,1000);
			}
			DeleteObject(&hSubMenu);
			DeleteObject(&hMenu);
			break;
			}
		case IDM_ALLWAYSSAVE:
			{
			HMENU hMenu = GetMenu(hwnd);
			HMENU hSubMenu = GetSubMenu(hMenu,1);
			HMENU hSubMenu1 = GetSubMenu(hSubMenu,2);
			if(alwaysSave == 0)
			{
				alwaysSave = 1;
				CheckMenuItem ( hSubMenu1,IDM_ALLWAYSSAVE,MF_CHECKED  );
				TimerMessage("开启","实时存档成功！",100,450,10,1,1000);
				MessageBox(hwnd, "实时存档默认覆盖第一存档位置","提示", 0); 
			}
			else
			{
				alwaysSave = 0;
				CheckMenuItem ( hSubMenu1,IDM_ALLWAYSSAVE,MF_UNCHECKED  );
				TimerMessage("关闭","实时存档成功！",100,450,10,1,1000);
			}
			DeleteObject(&hSubMenu1);
			DeleteObject(&hSubMenu);
			DeleteObject(&hMenu);
			break;
			}
		case IDM_SAVEINPUT:
			{
			OPENFILENAME ofn;
			static char szFile[256];
			static char szFileTitle[256];
			memset(&ofn,0,sizeof(ofn));
			ofn.lStructSize=sizeof(ofn);
			ofn.hwndOwner=hwnd;
			ofn.lpstrFilter="存档文件(*.ssv)\0*.ssv;\0\0";
			ofn.nFilterIndex=1;
			ofn.lpstrFile=szFile;
			ofn.nMaxFile=sizeof(szFile);
			ofn.lpstrFileTitle=szFileTitle;
			ofn.nMaxFileTitle=sizeof(szFileTitle);
			ofn.Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_EXPLORER;
			//当按下确定按钮
			if(GetOpenFileName(&ofn))
			{
				char dstFile[MAX_PATH];
				char szBuffer[MAX_PATH];
				sprintf(szBuffer,"%s",szFile);
				PathStripPath(szFile);

				char workpath[MAX_PATH];   
				GetModuleFileName(NULL,(LPSTR)workpath,sizeof(workpath));   
				char *p = strrchr(workpath, '\\');  
				*p=0x00;  


				sprintf(dstFile,"%s\\Res\\Save\\%s",workpath,szFile);
				CopyFile(szBuffer,dstFile,false);

				MessageBox(hwnd, "导入完成","提示", 0); 
			}
			break;
			}
		case IDM_SAVEOUTPUT:
			{
				TCHAR szBuffer[MAX_PATH] = { 0 };  
				BROWSEINFO bi = { 0 };  
				bi.hwndOwner = hwnd;//拥有着窗口句柄，为NULL表示对话框是非模态的，实际应用中一般都要有这个句柄  
				bi.pszDisplayName = szBuffer;//接收文件夹的缓冲区  
				bi.lpszTitle = "选择一个文件夹用于导出所有存档文件";//标题  
				bi.ulFlags = BIF_NEWDIALOGSTYLE;  
				LPITEMIDLIST idl = SHBrowseForFolder(&bi);  
				if (SHGetPathFromIDList(idl, szBuffer))
				{  
					char dstFile[MAX_PATH];
					char srcFile[MAX_PATH];
					char workpath[MAX_PATH];   
					GetModuleFileName(NULL,(LPSTR)workpath,sizeof(workpath));   
					char *p = strrchr(workpath, '\\');  
					*p=0x00;  

					sprintf(dstFile,"%s\\save0.ssv",szBuffer);
					sprintf(srcFile,"%s\\Res\\Save\\save0.ssv",workpath);
					CopyFile("Res\\Save\\save0.ssv",dstFile,false);
					sprintf(srcFile,"%s\\Res\\Save\\save1.ssv",workpath);
					sprintf(dstFile,"%s\\save1.ssv",szBuffer);
					CopyFile("Res\\Save\\save1.ssv",dstFile,false);
					sprintf(srcFile,"%s\\Res\\Save\\save2.ssv",workpath);
					sprintf(dstFile,"%s\\save2.ssv",szBuffer);
					CopyFile("Res\\Save\\save2.ssv",dstFile,false);
					MessageBox(hwnd, "导出完成","提示", 0);  
				}  
				else{  
					MessageBox(hwnd, "请选择一个文件夹", NULL, MB_ICONERROR);  
				}  
				break;
			}
		}
	case WM_LBUTTONDOWN:
		if(((double)widthx/(double)heighty)<=(800.0/600.0))
		{
			scaleTimes = (double)widthx / 800;
		}
		else
		{
			scaleTimes = (double)heighty / 600;
		}
		MouseStartX = (int)(LOWORD(lParam)/scaleTimes);
		MouseStartY = (int)(HIWORD(lParam)/scaleTimes);
		MouseDown = 1;
		break;
	case WM_LBUTTONUP:
		if(((double)widthx/(double)heighty)<=(800.0/600.0))
		{
			scaleTimes = (double)widthx / 800;
		}
		else
		{
			scaleTimes = (double)heighty / 600;
		}
		MouseEndX = (int)(LOWORD(lParam)/scaleTimes);
		MouseEndY = (int)(HIWORD(lParam)/scaleTimes);
		MouseDown = 0;
		game.DelMouseMsg(0);//20170205
		break;
	case WM_MOUSEWHEEL:
		MouseWheelLen = HIWORD(wParam);
		game.DelMouseMsg(1);//20170205
		break;
	case WM_RBUTTONDOWN:
		if(((double)widthx/(double)heighty)<=(800.0/600.0))
		{
			scaleTimes = (double)widthx / 800;
		}
		else
		{
			scaleTimes = (double)heighty / 600;
		}
		MouseStartX = (int)(LOWORD(lParam)/scaleTimes);
		MouseStartY = (int)(HIWORD(lParam)/scaleTimes);
		MouseDown = 2;
		break;
	case WM_RBUTTONUP:
		if(((double)widthx/(double)heighty)<=(800.0/600.0))
		{
			scaleTimes = (double)widthx / 800;
		}
		else
		{
			scaleTimes = (double)heighty / 600;
		}
		MouseEndX = (int)(LOWORD(lParam)/scaleTimes);
		MouseEndY = (int)(HIWORD(lParam)/scaleTimes);
		MouseDown = 0;
		game.DelMouseMsg(2);//20170205
		break;
	default:
		return DefWindowProc (hwnd, uMsg, 
			wParam, lParam) ;
	}
	return 0;
}
