五子棋游戏程序(二.图形接口部分)

来源:互联网 发布:实时滤镜软件 编辑:程序博客网 时间:2024/04/30 00:13

很简单的GDI函数应用和Win32App框架,而且只用了椭圆和直线函数,没什么难点,高手勿笑,初心者就多看看吧.

==Define.h=====================================================================

#ifndef _DEFINE_H

#define _DEFINE_H
#define TOP 1
#define BOTTOM 20
#define LEFT 1
#define RIGHT 20
#define NOCHESS  0
#define WHITE_CHESSMAN 1
#define BLACK_CHESSMAN 2
#define MAX_SIZE 20

#endif //#ifndef _DEFINE_H

===Shell.cpp===================================================================

//DEVCAPS1.C
/*------------------------------------------------------------------------
DEVCAPS1.C -- Device Capabilities Display Program No. 1
(c) Charles Petzold, 1998
----------------------------------------------------------------------*/
#define _WIN32_WINNT 0x0502
#include <windows.h>
#include "resource.h"
#include "./Header/Define.h"
#include "./header/ChesmanDefine.h"

static __int8   Chess[20][20]; //棋盘
static INT   fChess=BLACK_CHESSMAN;  //棋子标志,用来表示当前走棋的一方
static BOOL   fIsWin=FALSE; //是否产生赢的结果
static INT   cScreenX,cScreenY; //窗口长宽,用来绘制棋盘
static RECT   ReDrawRt; //填充矩形,用来在开始新局时刷新屏幕

DWORD WINAPI  DrawAll(LPVOID lpParameter); //用来处理WM_PAINT消息的屏幕重画函数,
VOID APIENTRY fnDrawChessboard(HDC hdc); //画棋盘函数
LRESULT APIENTRY fnDrawChessman(HDC hdc,INT cxChessman,INT cyChessman,INT fChessMan);//画单个棋子函数
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
VOID APIENTRY DetectDrawPos(DWORD cxPos,DWORD cyPos); //处理鼠标点击WM_LBUTTONUP输入函数

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
      LPSTR szCmdLine, int iCmdShow)
{
 HWND   hwnd ;
 MSG   msg ;
 WNDCLASS  wndclass ;
 
 ReDrawRt.bottom=445;
 ReDrawRt.left=0;
 ReDrawRt.right=407;
 ReDrawRt.top=0;
 static TCHAR szAppName[] = TEXT ("ChessWindow") ;

 
 wndclass.style   = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
 wndclass.lpfnWndProc = WndProc ;
 wndclass.cbClsExtra = 0 ;
 wndclass.cbWndExtra = 0 ;
 wndclass.hInstance  = hInstance ;
 wndclass.hIcon   = LoadIcon (NULL,IDI_INFORMATION) ;
 wndclass.hCursor = LoadCursor (NULL,IDC_CROSS) ;

 wndclass.hbrBackground = CreateSolidBrush(RGB(0x40,0x00,0xa0));
 wndclass.lpszMenuName = (LPCSTR)IDC_MENU ;
 wndclass.lpszClassName = szAppName ;
 
 if (!RegisterClass (&wndclass))
  {
   MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
    szAppName, MB_ICONERROR) ;
   return 0 ;
  }
 
 hwnd = CreateWindowEx(NULL,szAppName,TEXT ("五子棋(Ver 0.2)"),
    WS_SYSMENU | WS_MINIMIZEBOX,
    CW_USEDEFAULT, CW_USEDEFAULT,
    400+7, 440+5,
    NULL,NULL,
    hInstance, NULL) ;
 

 ShowWindow(hwnd,SW_SHOW);
 UpdateWindow(hwnd) ;


 
 while (GetMessage (&msg, NULL, 0, 0))
  {
   TranslateMessage (&msg) ;
   DispatchMessage (&msg) ;
  }
   DeleteObject(wndclass.hbrBackground);
   return msg.wParam ;
 
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 HDC hdc;
 INT wmId,wmEvent;
 DWORD posX,posY;
 HBRUSH hRefershBrush;
 switch (message)
  {
   case WM_CREATE:
    hdc = GetDC (hwnd) ;
    SelectObject(hdc,GetStockObject(DC_PEN));
    SelectObject(hdc,GetStockObject(DC_BRUSH));
    
    SetBkColor(hdc,RGB(0x40,0x00,0xa0));
    SetTextColor(hdc,RGB(0xff,0xff,0xff)); 
    SetDCPenColor(hdc,RGB(0xff,0xff,0xff));
    SetDCBrushColor(hdc,RGB(0xff,0xff,0xff));
    
    cScreenX = GetSystemMetrics(SM_CXSCREEN);
    cScreenY = GetSystemMetrics(SM_CYSCREEN);
    
    ReleaseDC (hwnd, hdc);
    return  TRUE;
   case WM_COMMAND:
    {
     wmId    = LOWORD(wParam);
     wmEvent = HIWORD(wParam);
     switch(wmId)
      {   
       case IDM_RESTART:
           ZeroMemory(Chess,400);
           Replace();
           fIsWin=FALSE;
           fChess=BLACK_CHESSMAN;
           hdc=GetDC(hwnd);
           hRefershBrush = CreateSolidBrush(RGB(0x40,0x00,0xa0));
           FillRect(hdc,&ReDrawRt,hRefershBrush);
           DeleteObject(hRefershBrush);
           InvalidateRect(hwnd,NULL,TRUE);
           ReleaseDC(hwnd,hdc);
           break;
       case IDM_EXIT:
           DestroyWindow(hwnd);
           break;
       default:
        
        return DefWindowProc(hwnd, message, wParam, lParam);
      }
    }
   
   case WM_PAINT:
    hdc = GetDC(hwnd);
    ValidateRect(hwnd,NULL);
    
    fnDrawChessboard(hdc);
    DrawAll((LPVOID)(&hdc));
    ReleaseDC(hwnd,hdc);
    break;
    
   case WM_DESTROY:
    PostQuitMessage (0);
    return 0 ;
  
   case WM_LBUTTONUP:
    if (fIsWin == TRUE)
     {
      MessageBox(hwnd,"请重新开始","提示!",MB_OK);
      break;
     }
    
    posX = LOWORD(lParam);
    posY = HIWORD(lParam);
    
    DetectDrawPos(posY,posX);
    InvalidateRect(hwnd,NULL,TRUE);
    UpdateWindow(hwnd);    
    
    if ((CheckIsWin((__int8*)Chess))==BLACK_WIN)
     {
      fIsWin=TRUE;
      MessageBox(hwnd,"黑棋胜利","恭喜!",MB_OK);
      break;
     }
    if ((CheckIsWin((__int8*)Chess))==WHITE_WIN)
     {
      fIsWin=TRUE;
      MessageBox(hwnd,"白棋胜利","恭喜!",MB_OK);
      break;
     } 
    break;
 }
 return DefWindowProc (hwnd, message, wParam, lParam) ;
}

/*
VOID GetFontInfo(HWND hWnd,struct_FontInfo* FontInfo)
{
 HDC localhDC;
 localhDC = GetDC(hWnd);
 GetTextMetrics(localhDC,&(FontInfo->SystemTextMetric));
 (FontInfo->cxFont) = (FontInfo->SystemTextMetric).tmAveCharWidth;
 (FontInfo->cyFont) = ((FontInfo->SystemTextMetric).tmExternalLeading)+((FontInfo->SystemTextMetric).tmHeight);
 (FontInfo->cxCaps) = (((FontInfo->SystemTextMetric).tmPitchAndFamily)& 1 ? 3 : 2)*(FontInfo->cxFont)/2;
 ReleaseDC(hWnd,localhDC);
}
*/

VOID APIENTRY fnDrawChessboard(HDC hdc)
{
 INT cpX=0,cpY=0,Tmp_a=0;
 for(Tmp_a=0;Tmp_a<=20;Tmp_a++)
  { 
   MoveToEx(hdc,cpX,cpY,NULL);
   LineTo(hdc,cpX+cScreenX,cpY);
   cpY+=20;
  }
 cpX = cpY = 0;
 for(Tmp_a=0;Tmp_a<=20;Tmp_a++)
  {
   MoveToEx(hdc,cpX,cpY,NULL);
   LineTo(hdc,cpX,cpY+cScreenY);
   cpX+=20;
  }

LRESULT APIENTRY fnDrawChessman(HDC hdc,INT cxChessman,INT cyChessman,INT fChessMan)
{
 SaveDC(hdc);
 if (fChessMan == WHITE_CHESSMAN)
  {
   SetDCBrushColor(hdc,RGB(0xff,0xff,0xff));
   SetDCPenColor(hdc,RGB(0xff,0xff,0xff));
  }
 else 
  {
   SetDCPenColor(hdc,RGB(0,0,0));
   SetDCBrushColor(hdc,RGB(0x0,0x0,0x0));
  }
 
 if (cxChessman<0){cxChessman=1;}
 if (cxChessman>MAX_SIZE){cxChessman=MAX_SIZE-1;}
 
 if (cyChessman<0){cyChessman=1;}
 if (cyChessman>MAX_SIZE){cyChessman=MAX_SIZE-1;}
 
 Ellipse(hdc,LEFT+(cxChessman)*20,TOP+(cyChessman)*20,RIGHT+(cxChessman)*20,BOTTOM+(cyChessman)*20);
 
 RestoreDC(hdc,-1);
 return TRUE;
}


VOID APIENTRY DetectDrawPos(DWORD cxPos,DWORD cyPos)
{
 if (Chess[cxPos/20][cyPos/20]==NO_CHESSMAN)
  {
   switch(fChess)
   {
    case BLACK_CHESSMAN:
     Chess[cxPos/20][cyPos/20]=BLACK_CHESSMAN;
     fChess=WHITE_CHESSMAN;
    break;
    case WHITE_CHESSMAN:
     Chess[cxPos/20][cyPos/20]=WHITE_CHESSMAN;
     fChess=BLACK_CHESSMAN;
    break;
   }
  }

}

DWORD WINAPI DrawAll(LPVOID lpParameter)
{
 INT Tmp_a,Tmp_b;
 HDC hdc;
 _asm
  {
   mov EAX,lpParameter
   mov EAX,[EAX]
   mov hdc,EAX
  }

 for(Tmp_a=0;Tmp_a<MAX_SIZE;Tmp_a++)
  {
   for(Tmp_b=0;Tmp_b<MAX_SIZE;Tmp_b++)
    {
     if(Chess[Tmp_a][Tmp_b]==WHITE_CHESSMAN)
      {
       fnDrawChessman(hdc,Tmp_b,Tmp_a,WHITE_CHESSMAN);
      }
     if(Chess[Tmp_a][Tmp_b]==BLACK_CHESSMAN)
      {
       fnDrawChessman(hdc,Tmp_b,Tmp_a,BLACK_CHESSMAN);
      }
    }
  }

 return TRUE;   
}

原创粉丝点击