WTL 拼图游戏

本文介绍了一个使用WTL实现的拼图游戏项目。该程序利用C++和Windows API创建了一个图形用户界面,允许用户通过键盘操作移动拼图块。文章详细展示了如何初始化窗口、加载图片并将其分割为拼图块,以及如何响应用户的按键事件来更新拼图状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

隐藏行号 复制代码 这是一段程序代码。
  1. /********************************************************************
    
  2.  FileName:      puzzle.h
    
  3.  Description:   WTL拼′图?游?戏·
    
  4.  Author:        Chengm, 2011/01/24,20:18
    
  5.  Version:       0.1
    
  6.  Compiled on:   Win2003 sp1 + VC.net 2005
    
  7.  Modification history:
    
  8.  Other:
    
  9. *********************************************************************/
    
  10. #pragma once
    
  11. #include "stdafx.h"
    
  12. 
    
  13. 
    
  14. typedef CWinTraits CAeroPuzzleWinTraits;
    
  15. class CAeroPuzzleWindow    :
    
  16.     public CWindowImpl,
    
  17.     public CDwmImpl,
    
  18.     public CBufferedAnimationImplint>
    
  19. {
    
  20. public:
    
  21.     DECLARE_WND_CLASS(_T("WTL Puzzle"))
    
  22. 
    
  23.     typedef CBufferedAnimationImplint> _baseAnitClass;
    
  24. 
    
  25.     BEGIN_MSG_MAP(CAeroPuzzleWindow)
    
  26.         MSG_WM_CREATE(OnCreate)
    
  27.         MSG_WM_KEYUP(OnKeyUp)
    
  28.         MSG_WM_DESTROY(OnDestroy)
    
  29.         MSG_WM_SIZE(OnSize)
    
  30.         MSG_WM_INITDIALOG(OnInitDialog)
    
  31.         CHAIN_MSG_MAP(_baseAnitClass)
    
  32.     END_MSG_MAP()
    
  33. 
    
  34. public:
    
  35.     CAeroPuzzleWindow()    : CBufferedAnimationImplint>(0)
    
  36.     {
    
  37.         ATLASSERT(IsDwmSupported());
    
  38.         CWndClassInfo& wci = GetWndClassInfo();
    
  39.         if(!wci.m_atom)
    
  40.         {
    
  41.             wci.m_wc.hbrBackground = AtlGetStockBrush(BLACK_BRUSH);
    
  42.         }
    
  43.     }
    
  44. 
    
  45.     void DoPaint(CDCHandle dc, RECT& rect, int picIndex)
    
  46.     {
    
  47.         CRect rc(rect);
    
  48.         dc.FillSolidRect(&rc, WHITE_COLOR);
    
  49.         CSize bmpSize;
    
  50.         m_Pic[0].GetSize(bmpSize);
    
  51.         CDC dcImage;
    
  52.         dcImage.CreateCompatibleDC(dc);
    
  53.         HBITMAP hOldBitmap = dcImage.SelectBitmap(m_Pic[0]);
    
  54.         for(int i=0;i<3; i++){
    
  55.             for(int j=0;j<3; j++){
    
  56.                 if(i == niWhite && j == njWhite)
    
  57.                     continue;
    
  58.                 dcImage.SelectBitmap(m_Pic[nCoordination[i][j]]);
    
  59.                 dc.SetStretchBltMode(COLORONCOLOR);
    
  60.                 dc.StretchBlt(bmpSize.cx * i, bmpSize.cy * j, bmpSize.cx , bmpSize.cy , dcImage, 0, 0,bmpSize.cx,bmpSize.cy, SRCCOPY);
    
  61.             }
    
  62.         }
    
  63.         dcImage.SelectBitmap(hOldBitmap);
    
  64.     }
    
  65. 
    
  66.     void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
    
  67.     {
    
  68.         if(nChar == VK_UP && njWhite < 2){
    
  69.             nCoordination[niWhite][njWhite] = nCoordination[niWhite][njWhite + 1];
    
  70.             njWhite += 1;
    
  71.         }else if(nChar == VK_DOWN && njWhite >0){
    
  72.             nCoordination[niWhite][njWhite] = nCoordination[niWhite][njWhite -1];
    
  73.             njWhite -= 1;
    
  74.         }else if(nChar == VK_LEFT && niWhite < 2){
    
  75.             nCoordination[niWhite][njWhite] = nCoordination[niWhite + 1][njWhite];
    
  76.             niWhite += 1;
    
  77.         }else if(nChar == VK_RIGHT && niWhite > 0){
    
  78.             nCoordination[niWhite][njWhite] = nCoordination[niWhite - 1][njWhite];
    
  79.             niWhite -= 1;
    
  80.         }
    
  81.         
    
  82.         this->RedrawWindow();
    
  83.     }
    
  84. 
    
  85. 
    
  86. protected:
    
  87.     int OnCreate(LPCREATESTRUCT /*lpCreateStruct*/)
    
  88.     {
    
  89. 
    
  90.         DwmExtendFrameIntoEntireClientArea();
    
  91. 
    
  92.         m_Picture = AtlLoadGdiplusImage(IDR_JPG2, _T("JPG"));
    
  93.         ATLASSERT(!m_Picture.IsNull());
    
  94. 
    
  95.         this->ResizeClient(800,600);
    
  96.         niWhite = 2;
    
  97.         njWhite = 2;
    
  98.         
    
  99.         CRect rc;
    
  100.         GetClientRect(&rc);
    
  101. 
    
  102.         int nWidth  = rc.Width() / 3;
    
  103.         int nHeight = rc.Height() / 3;
    
  104. 
    
  105.         SIZE bmpSize;
    
  106.         m_Picture.GetSize(bmpSize);
    
  107. 
    
  108.         int bmpWidth = bmpSize.cx / 3;
    
  109.         int bmpHeight = bmpSize.cy / 3;
    
  110. 
    
  111.         CDC srcBmp;
    
  112.         CDC destBmp;
    
  113. 
    
  114.         srcBmp.CreateCompatibleDC(GetWindowDC());
    
  115.         destBmp.CreateCompatibleDC(GetWindowDC());
    
  116. 
    
  117.         int nSrcBmp = srcBmp.SaveDC();
    
  118.         int nDestBmp = destBmp.SaveDC();
    
  119.         srcBmp.SelectBitmap(m_Picture);
    
  120. 
    
  121. 
    
  122.         for(int i=0;i<3; i++){
    
  123.             for(int j=0;j<3; j++){
    
  124.                 nCoordination[i][j] = i * 3 + j;
    
  125.                 m_Pic[nCoordination[i][j]].CreateCompatibleBitmap(GetWindowDC(), nWidth, nHeight);
    
  126.                 destBmp.SelectBitmap(m_Pic[nCoordination[i][j]]);
    
  127.                 destBmp.SetStretchBltMode(COLORONCOLOR);
    
  128.                 destBmp.StretchBlt(0,0,nWidth, nHeight, srcBmp, bmpWidth * i, bmpHeight * j, bmpWidth, bmpHeight, SRCCOPY);
    
  129.             }
    
  130.         }
    
  131. 
    
  132.         srcBmp.RestoreDC(nSrcBmp);
    
  133.         destBmp.RestoreDC(nDestBmp);
    
  134. 
    
  135.         SetDuration(400);
    
  136. 
    
  137.         int tmp = 0;
    
  138.         for(int i=0;i<9;i++)
    
  139.         {
    
  140.             int a = rand() % 9;
    
  141.             tmp = nCoordination[niWhite][njWhite];
    
  142.             nCoordination[niWhite][njWhite] = nCoordination[a / 3][a % 3];
    
  143.             nCoordination[a / 3][a % 3] = tmp;
    
  144.             niWhite = a / 3;
    
  145.             njWhite = a % 3;
    
  146.         }
    
  147.         return 0;
    
  148.     }
    
  149. 
    
  150.     void OnDestroy()
    
  151.     {
    
  152. 
    
  153.         PostQuitMessage(0);
    
  154.     }
    
  155. 
    
  156.     void OnSize(UINT /*nType*/, CSize size)
    
  157.     {
    
  158. 
    
  159.     }
    
  160. 
    
  161.     BOOL OnInitDialog(CWindow wndFocus, LPARAM lInitParam)
    
  162.     {
    
  163.         SetWindowPos(m_hWnd, 0, 0,800, 600, SWP_SHOWWINDOW);
    
  164.         
    
  165.         return 0;
    
  166.     }
    
  167. 
    
  168. private:
    
  169.     CBitmap m_Picture;
    
  170.     int nCoordination[3][3];
    
  171.     CBitmap m_Pic[9];
    
  172.     int niWhite,njWhite;
    
  173. };
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值