/********************************************************************
FileName: puzzle.h
Description: WTL拼′图?游?戏·
Author: Chengm, 2011/01/24,20:18
Version: 0.1
Compiled on: Win2003 sp1 + VC.net 2005
Modification history:
Other:
*********************************************************************/
#pragma once
#include "stdafx.h"
typedef CWinTraits CAeroPuzzleWinTraits;
class CAeroPuzzleWindow :
public CWindowImpl ,
public CDwmImpl ,
public CBufferedAnimationImpl int>
{
public:
DECLARE_WND_CLASS(_T("WTL Puzzle"))
typedef CBufferedAnimationImpl int> _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() : CBufferedAnimationImpl int>(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 拼图游戏
最新推荐文章于 2024-07-27 10:49:41 发布