/********************************************************************FileName: puzzle.hDescription: WTL拼′图?游?戏·Author: Chengm, 2011/01/24,20:18Version: 0.1Compiled on: Win2003 sp1 + VC.net 2005Modification history:Other:*********************************************************************/#pragma once#include "stdafx.h"
typedef CWinTraits CAeroPuzzleWinTraits;class CAeroPuzzleWindow :public CWindowImpl,public CDwmImpl,public CBufferedAnimationImplint>{public:DECLARE_WND_CLASS(_T("WTL Puzzle"))typedef CBufferedAnimationImplint> _baseAnitClass;BEGIN_MSG_MAP(CAeroPuzzleWindow)
MSG_WM_CREATE(OnCreate)
MSG_WM_KEYUP(OnKeyUp)
MSG_WM_DESTROY(OnDestroy)
MSG_WM_SIZE(OnSize)
MSG_WM_INITDIALOG(OnInitDialog)
CHAIN_MSG_MAP(_baseAnitClass)
END_MSG_MAP()
public:CAeroPuzzleWindow() : CBufferedAnimationImplint>(0)
{ATLASSERT(IsDwmSupported());
CWndClassInfo& wci = GetWndClassInfo();
if(!wci.m_atom){wci.m_wc.hbrBackground = AtlGetStockBrush(BLACK_BRUSH);
}
}
void DoPaint(CDCHandle dc, RECT& rect, int picIndex)
{CRect rc(rect);
dc.FillSolidRect(&rc, WHITE_COLOR);
CSize bmpSize;
m_Pic[0].GetSize(bmpSize);
CDC dcImage;
dcImage.CreateCompatibleDC(dc);
HBITMAP hOldBitmap = dcImage.SelectBitmap(m_Pic[0]);
for(int i=0;i<3; i++){
for(int j=0;j<3; j++){
if(i == niWhite && j == njWhite)continue;dcImage.SelectBitmap(m_Pic[nCoordination[i][j]]);
dc.SetStretchBltMode(COLORONCOLOR);
dc.StretchBlt(bmpSize.cx * i, bmpSize.cy * j, bmpSize.cx , bmpSize.cy , dcImage, 0, 0,bmpSize.cx,bmpSize.cy, SRCCOPY);
}
}
dcImage.SelectBitmap(hOldBitmap);
}
void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags){if(nChar == VK_UP && njWhite < 2){nCoordination[niWhite][njWhite] = nCoordination[niWhite][njWhite + 1];
njWhite += 1;
}else if(nChar == VK_DOWN && njWhite >0){nCoordination[niWhite][njWhite] = nCoordination[niWhite][njWhite -1];
njWhite -= 1;
}else if(nChar == VK_LEFT && niWhite < 2){nCoordination[niWhite][njWhite] = nCoordination[niWhite + 1][njWhite];
niWhite += 1;
}else if(nChar == VK_RIGHT && niWhite > 0){nCoordination[niWhite][njWhite] = nCoordination[niWhite - 1][njWhite];
niWhite -= 1;
}
this->RedrawWindow();}
protected:int OnCreate(LPCREATESTRUCT /*lpCreateStruct*/)
{DwmExtendFrameIntoEntireClientArea();
m_Picture = AtlLoadGdiplusImage(IDR_JPG2, _T("JPG"));ATLASSERT(!m_Picture.IsNull());
this->ResizeClient(800,600);niWhite = 2;
njWhite = 2;
CRect rc;
GetClientRect(&rc);
int nWidth = rc.Width() / 3;int nHeight = rc.Height() / 3;SIZE bmpSize;
m_Picture.GetSize(bmpSize);
int bmpWidth = bmpSize.cx / 3;int bmpHeight = bmpSize.cy / 3;CDC srcBmp;
CDC destBmp;
srcBmp.CreateCompatibleDC(GetWindowDC());
destBmp.CreateCompatibleDC(GetWindowDC());
int nSrcBmp = srcBmp.SaveDC();int nDestBmp = destBmp.SaveDC();srcBmp.SelectBitmap(m_Picture);
for(int i=0;i<3; i++){
for(int j=0;j<3; j++){
nCoordination[i][j] = i * 3 + j;
m_Pic[nCoordination[i][j]].CreateCompatibleBitmap(GetWindowDC(), nWidth, nHeight);
destBmp.SelectBitmap(m_Pic[nCoordination[i][j]]);
destBmp.SetStretchBltMode(COLORONCOLOR);
destBmp.StretchBlt(0,0,nWidth, nHeight, srcBmp, bmpWidth * i, bmpHeight * j, bmpWidth, bmpHeight, SRCCOPY);
}
}
srcBmp.RestoreDC(nSrcBmp);
destBmp.RestoreDC(nDestBmp);
SetDuration(400);
int tmp = 0;for(int i=0;i<9;i++)
{int a = rand() % 9;tmp = nCoordination[niWhite][njWhite];
nCoordination[niWhite][njWhite] = nCoordination[a / 3][a % 3];
nCoordination[a / 3][a % 3] = tmp;
niWhite = a / 3;
njWhite = a % 3;
}
return 0;}
void OnDestroy(){PostQuitMessage(0);
}
void OnSize(UINT /*nType*/, CSize size)
{}
BOOL OnInitDialog(CWindow wndFocus, LPARAM lInitParam)
{SetWindowPos(m_hWnd, 0, 0,800, 600, SWP_SHOWWINDOW);
return 0;}
private:CBitmap m_Picture;
int nCoordination[3][3];CBitmap m_Pic[9];
int niWhite,njWhite;};
WTL 拼图游戏
最新推荐文章于 2018-12-31 13:58:42 发布
本文介绍了一个使用WTL实现的拼图游戏项目。该程序利用C++和Windows API创建了一个图形用户界面,允许用户通过键盘操作移动拼图块。文章详细展示了如何初始化窗口、加载图片并将其分割为拼图块,以及如何响应用户的按键事件来更新拼图状态。
3367

被折叠的 条评论
为什么被折叠?



