beautifulGame.cpp
资源名称:连连看源程序.rar [点击查看]
上传用户:z3308516
上传日期:2013-01-23
资源大小:703k
文件大小:13k
源码类别:
2D图形编程
开发平台:
Visual C++
- // beautifulGame.cpp : 定义应用程序的入口点。
- //
- #include "stdafx.h"
- #include "beautifulGame.h"
- #include "GridPicture.h"
- #include "Registry.h"
- #include "TimeElapse.h"
- #define MAX_LOADSTRING 100
- // 全局变量:
- HINSTANCE hInst; // 当前实例
- TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
- TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
- // 此代码模块中包含的函数的前向声明:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- void LoadAndShowPicture(HWND,HDC hDC,LPCTSTR pRes,BOOL bLoadOnly=FALSE,BOOL bDiscard=FALSE);
- BOOL ExtractFile(const char *pszFile,LPCTSTR res);
- CGridPicture gridPic;
- CTimeElapse timeElapse;
- CRegistry reg;
- IPicture *pPicture;
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- // TODO: 在此放置代码。
- MSG msg;
- HACCEL hAccelTable;
- ///com库初始化
- CoInitialize(NULL);
- // 初始化全局字符串
- LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_BEAUTIFULGAME, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- // 执行应用程序初始化:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BEAUTIFULGAME);
- // 主消息循环:
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- if(pPicture){
- pPicture->Release();
- }
- CoUninitialize(); //com unload
- return (int) msg.wParam;
- }
- //
- // 函数:MyRegisterClass()
- //
- // 目的:注册窗口类。
- //
- // 注释:
- //
- // 仅当希望在已添加到 Windows 95 的
- // “RegisterClassEx”函数之前此代码与 Win32 系统兼容时,
- // 才需要此函数及其用法。调用此函数
- // 十分重要,这样应用程序就可以获得关联的
- // “格式正确的”小图标。
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = (WNDPROC)WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_BEAUTIFULGAME);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = (LPCTSTR)IDC_BEAUTIFULGAME;
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
- return RegisterClassEx(&wcex);
- }
- //
- // 函数:InitInstance(HANDLE, int)
- //
- // 目的:保存实例句柄并创建主窗口
- //
- // 注释:
- //
- // 在此函数中,我们在全局变量中保存实例句柄并
- // 创建和显示主程序窗口。
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- hInst = hInstance; // 将实例句柄存储在全局变量中
- hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- 0, 0, 800,600, NULL, NULL, hInstance, NULL);
- // hWnd = CreateWindowEx(WS_EX_TOOLWINDOW,szWindowClass, szTitle, WS_POPUP,
- // 1024*3, 0, 2048,1536, NULL, NULL, hInstance, NULL);
- // hWnd = CreateWindowEx(WS_EX_TOOLWINDOW,szWindowClass, szTitle, WS_POPUP,
- // 0, 0,800,600, NULL, NULL, hInstance, NULL);
- // SetMenu(hWnd,NULL);
- if (!hWnd)
- {
- return FALSE;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- //
- // 函数:WndProc(HWND, unsigned, WORD, LONG)
- //
- // 目的:处理主窗口的消息。
- //
- // WM_COMMAND - 处理应用程序菜单
- // WM_PAINT - 绘制主窗口
- // WM_DESTROY - 发送退出消息并返回
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- POINT ptPos;
- int wmId, wmEvent;
- PAINTSTRUCT ps;
- HDC hdc;
- int id1,id2;
- BOOL bFound;
- static BOOL bGameStart=FALSE;
- HMENU hMenu;
- int lv,ifen;
- switch (message)
- {
- case WM_CREATE:
- hMenu=GetMenu(hWnd);
- hMenu=GetSubMenu(hMenu,0);
- hMenu=GetSubMenu(hMenu,2);
- CheckMenuItem(hMenu,ID_SOUND,MF_BYCOMMAND|MF_CHECKED);
- SetTimer(hWnd,1,200,NULL);
- // SetTimer(hWnd,2,300,NULL);
- ExtractFile((const char*)"back",(LPCTSTR)IDR_BACKMIDI);
- break;
- case WM_TIMER:
- if(wParam==1){
- timeElapse.QueryStatus(); //sound play
- }else{
- bFound=FALSE;
- gridPic.FoundLink(&gridPic,&id1,&id2,bFound);
- if(bFound){
- gridPic.DrawLinkLine(id1,id2);
- gridPic.Explore(id1,id2);
- }else{
- gridPic.SetFenLevel(0,20,3);
- gridPic.CGridPictureInit(hWnd,22,11,IDB_BITMAP,TRUE);
- bGameStart=TRUE;
- InvalidateRect(hWnd,NULL,TRUE);
- UpdateWindow(hWnd);
- }
- }
- break;
- case OUTOF_TIME:
- timeElapse.Stop();
- gridPic.GetLevelAndFen(ifen,lv);
- MessageBox(hWnd,"由于时间的关系,你的生命走到尽头了,请休息一下再玩吧","信息",MB_OK|MB_ICONWARNING);
- switch(lv){
- case 0:
- reg.CompareAndSetValue(hWnd,enumLow,ifen);
- break;
- case 1:
- reg.CompareAndSetValue(hWnd,enumNormal,ifen);
- break;
- case 2:
- reg.CompareAndSetValue(hWnd,enumHigh,ifen);
- break;
- case 3:
- reg.CompareAndSetValue(hWnd,enumSpecial,ifen);
- break;
- }
- SendMessage(hWnd,WM_COMMAND,ID_DISCARD,0);
- break;
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- // 分析菜单选择:
- switch (wmId)
- {
- case ID_BACKMIDI:
- hMenu=GetMenu(hWnd);
- hMenu=GetSubMenu(hMenu,0);
- hMenu=GetSubMenu(hMenu,2);
- if(timeElapse.bBackMidi){
- KillTimer(hWnd,1);
- timeElapse.CloseMidiSound();
- CheckMenuItem(hMenu,ID_BACKMIDI,MF_BYCOMMAND|MF_UNCHECKED);
- }else{
- timeElapse.bBackMidi=TRUE;
- SetTimer(hWnd,1,200,NULL);
- timeElapse.PlayMidiSound();
- CheckMenuItem(hMenu,ID_BACKMIDI,MF_BYCOMMAND|MF_CHECKED);
- }
- break;
- case ID_SOUND:
- hMenu=GetMenu(hWnd);
- hMenu=GetSubMenu(hMenu,0);
- hMenu=GetSubMenu(hMenu,2);
- if(gridPic.GetSound()){
- CheckMenuItem(hMenu,ID_SOUND,MF_BYCOMMAND|MF_UNCHECKED);
- gridPic.SetSound(FALSE);
- }else{
- CheckMenuItem(hMenu,ID_SOUND,MF_BYCOMMAND|MF_CHECKED);
- gridPic.SetSound(TRUE);
- }
- break;
- case IDM_ABOUT:
- DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
- break;
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
- case ID_NORMAL:
- case ID_COMPLEX:
- case ID_EASY:
- case ID_SPECIAL:
- timeElapse.Stop();
- timeElapse.bPause =FALSE;
- Sleep(500);
- if(wmId==ID_EASY){
- gridPic.SetFenLevel(0,0,0);
- timeElapse.CTimeElapseInit(hWnd,600); //120 sec
- gridPic.CGridPictureInit(hWnd,12,7,IDB_BITMAP);
- }else if(wmId==ID_NORMAL){ //180 sec
- gridPic.SetFenLevel(0,0,1);
- timeElapse.CTimeElapseInit(hWnd,600,300);
- gridPic.CGridPictureInit(hWnd,14,8,IDB_BITMAP);
- }else if(wmId==ID_COMPLEX){ //240 sec
- gridPic.SetFenLevel(0,0,2);
- timeElapse.CTimeElapseInit(hWnd,600,400);
- gridPic.CGridPictureInit(hWnd,16,9,IDB_BITMAP);
- }else{
- gridPic.SetFenLevel(0,0,3);
- timeElapse.CTimeElapseInit(hWnd,600,600); //180 sec
- gridPic.CGridPictureInit(hWnd,22,11,IDB_BITMAP,TRUE);
- }
- bGameStart=TRUE;
- InvalidateRect(hWnd,NULL,TRUE);
- UpdateWindow(hWnd);
- timeElapse.Begin();
- timeElapse.InvalidIt(hWnd);
- break;
- case ID_DISCARD:
- if(bGameStart){
- gridPic.Discard();
- timeElapse.Stop();
- bGameStart=FALSE;
- InvalidateRect(hWnd,NULL,TRUE);
- }
- break;
- case ID_FIRSTEASY:
- reg.ShowAll(hWnd,enumLow);
- break;
- case ID_FIRSTNORMAL:
- reg.ShowAll(hWnd,enumNormal);
- break;
- case ID_FIRSTCOMPLEX:
- reg.ShowAll(hWnd,enumHigh);
- break;
- case ID_FIRSTSPECIALVER:
- reg.ShowAll(hWnd,enumSpecial);
- break;
- case ID_EXPLORE:
- SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_F4,0);
- break;
- case ID_REORDER:
- SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_F5,0);
- break;
- case ID_PAUSE:
- SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_F6,0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
- case WM_KEYDOWN:
- if(bGameStart){
- switch(wParam){
- case VK_F5:
- gridPic.Shuffle();
- InvalidateRect(hWnd,NULL,TRUE);
- if(timeElapse.totalSecond-100<0)
- timeElapse.totalSecond =0;
- else
- timeElapse.totalSecond -=100;
- break;
- case VK_F4:
- bFound=FALSE;
- gridPic.FoundLink(&gridPic,&id1,&id2,bFound);
- if(bFound){
- gridPic.DrawLinkLine(id1,id2);
- gridPic.Explore(id1,id2);
- if(timeElapse.totalSecond-50<0)
- timeElapse.totalSecond =0;
- else
- timeElapse.totalSecond -=50;
- timeElapse.InvalidIt(hWnd);
- }
- break;
- case VK_F6:
- timeElapse.Pause();
- if(timeElapse.bPause){
- hdc=GetDC(hWnd);
- LoadAndShowPicture(hWnd,hdc,(LPCTSTR)IDR_PAUSEJPG,FALSE,TRUE);
- ReleaseDC(hWnd,hdc);
- }else{
- LoadAndShowPicture(hWnd,NULL,(LPCTSTR)IDR_MAINJPG,TRUE,TRUE);
- InvalidateRect(hWnd,NULL,TRUE);
- }
- break;
- }
- }
- break;
- case STOP_TIME:
- if(bGameStart){
- timeElapse.Stop();
- bGameStart=FALSE;
- InvalidateRect(hWnd,NULL,TRUE);
- }
- break;
- case WM_LBUTTONDOWN:
- if(bGameStart && !timeElapse.bPause){
- ptPos.x=LOWORD(lParam);
- ptPos.y=HIWORD(lParam);
- gridPic.Picture_Click(ptPos);
- }
- break;
- case WM_RBUTTONDOWN:
- if(bGameStart && !timeElapse.bPause){
- gridPic.CancelFocus();
- }
- break;
- case WM_PAINT:
- // HBRUSH hbr;
- // RECT rcBack;
- hdc = BeginPaint(hWnd, &ps);
- if(bGameStart && !timeElapse.bPause){
- /* hbr=CreateSolidBrush(RGB(0,0,0));
- GetClientRect(hWnd,&rcBack);
- FillRect(hdc,(CONST RECT*)&rcBack,hbr);
- DeleteObject(hbr);
- */
- gridPic.Draw(hdc);
- timeElapse.FillRegion();
- }else{
- LoadAndShowPicture(hWnd,hdc,(LPCTSTR)IDR_MAINJPG);
- }
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- // “关于”框的消息处理程序。
- LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_INITDIALOG:
- return TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return TRUE;
- }
- break;
- }
- return FALSE;
- }
- void LoadAndShowPicture(HWND hWnd,HDC hDC,LPCTSTR pRes,BOOL bLoadOnly,BOOL bDiscard){
- HRSRC hsrc;
- HGLOBAL hGlobal;
- HGLOBAL hGlobal1; //memory must moveable
- HRESULT hr;
- long sz;
- LPVOID pData;
- LPVOID pData1;
- IStream *pStream;
- POINT imageSize;
- RECT rcBounds;
- RECT rcWindow;
- OLE_XSIZE_HIMETRIC cx;
- OLE_YSIZE_HIMETRIC cy;
- if(bDiscard){
- if(pPicture){
- pPicture->Release();
- pPicture=NULL;
- }
- }
- if(!pPicture){
- hsrc=FindResource((HMODULE)GetModuleHandle(NULL),(LPCTSTR)pRes,(LPCTSTR)"RT_JPGDATA");
- if(hsrc){
- sz=SizeofResource((HMODULE)GetModuleHandle(NULL),hsrc);
- hGlobal=LoadResource((HMODULE)GetModuleHandle(NULL),hsrc);
- if(hGlobal){
- hGlobal1=GlobalAlloc(GMEM_MOVEABLE,sz);
- pData=GlobalLock(hGlobal);
- pData1=GlobalLock(hGlobal1);
- memcpy((void*)pData1,(const void*)pData,sz);
- GlobalUnlock(hGlobal1);
- GlobalUnlock(hGlobal);
- hr=CreateStreamOnHGlobal(hGlobal1,TRUE,&pStream);
- if(SUCCEEDED(hr=OleLoadPicture(pStream,0,FALSE,IID_IPicture,(LPVOID*)&pPicture)) && !bLoadOnly){
- pPicture->get_Width(&cx);
- pPicture->get_Height(&cy);
- imageSize.x=cx;
- imageSize.y=cy;
- // SetMapMode(hDC,MM_HIMETRIC);
- // LPtoDP(hDC,(LPPOINT)&imageSize,1);
- // SetMapMode(hDC,MM_TEXT);
- GetClientRect(hWnd,&rcWindow);
- imageSize.x=rcWindow.right;
- imageSize.y=rcWindow.bottom;
- rcBounds.left =0;
- rcBounds.top =0;
- rcBounds.right =imageSize.x;
- rcBounds.bottom =imageSize.y;
- pPicture->Render(hDC,0,0,imageSize.x ,imageSize.y,0,cy,cx,-cy,&rcBounds);
- pStream->Release();
- }
- GlobalFree(hGlobal1);
- }
- FreeResource(hsrc);
- }
- }else{
- pPicture->get_Width(&cx);
- pPicture->get_Height(&cy);
- imageSize.x=cx;
- imageSize.y=cy;
- GetClientRect(hWnd,&rcWindow);
- imageSize.x=rcWindow.right;
- imageSize.y=rcWindow.bottom;
- rcBounds.left =0;
- rcBounds.top =0;
- rcBounds.right =imageSize.x;
- rcBounds.bottom =imageSize.y;
- pPicture->Render(hDC,0,0,imageSize.x ,imageSize.y,0,cy,cx,-cy,&rcBounds);
- }
- }
- BOOL ExtractFile(const char *pszFile,LPCTSTR res){
- HANDLE hFile;
- HRSRC hsrc;
- HGLOBAL hglobal;
- LPVOID lpv;
- DWORD ret;
- BOOL bret;
- bret=FALSE;
- if(_access(pszFile,0)==-1){
- hFile=CreateFile(pszFile,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
- NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
- if(hFile){
- hsrc=FindResource(GetModuleHandle(NULL),(LPCTSTR)res,(LPCTSTR)"RT_JPGDATA");
- if(hsrc){
- hglobal=LoadResource((HMODULE)GetModuleHandle(NULL),hsrc);
- if(hglobal){
- lpv=LockResource(hglobal);
- if(lpv){
- WriteFile(hFile,lpv,SizeofResource(GetModuleHandle(NULL),hsrc),&ret,NULL);
- bret=TRUE;
- }
- }
- }
- CloseHandle(hFile);
- }
- }else
- bret=TRUE;
- return bret;
- }