📄 play.cpp
字号:
#include "Gomoku.h"
/*=========================================================
程序小结:
本程序完全以C++和windows api来实现,比较底层,所以包括窗口,菜单等都要自已来设计。
窗口及事件:
windows中以WinMain()来作为程序的入口函数,并根据参数自动绘制窗口。以回调函数来实
现事件机制,本程序以GomokuProc作为回调函数,并处理各种事件。
菜单:
实现比较简单,也比较简陋,当WM_LBUTTONDOWN事件发生时,程序判断鼠标是否位于某个菜
单区域之内,来决定是否响应该消息。
=========================================================*/
Gomoku *m_gomoku;
LRESULT CALLBACK GomokuProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ICON);
wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=GomokuProc;
wndclass.lpszClassName="gomoku";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW | CS_VREDRAW;
m_gomoku = new Gomoku(15,15);
RegisterClass(&wndclass);
HWND hwnd;
hwnd=CreateWindow("gomoku","Gomoku game",WS_OVERLAPPEDWINDOW,
300,200,490,380,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 1;
}
LRESULT CALLBACK GomokuProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
HDC hdc;
PAINTSTRUCT ps;
switch(uMsg)
{
case WM_LBUTTONDOWN:
POINT point;
GetCursorPos(&point);
ScreenToClient(hwnd,&point);
m_gomoku->Input(point);
RedrawWindow(hwnd,NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME |RDW_ALLCHILDREN);
Square squ;
if((squ=m_gomoku->IsSuccess(point)) != Empty )
{
if (squ == Black)
{
MessageBox(hwnd,"Black win!","Gomoku",MB_OK);
}
else
{
MessageBox(hwnd,"White win!","Gomoku",MB_OK);
}
m_gomoku->NewGame();
}
break;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
m_gomoku->Display(hdc);
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
m_gomoku->SaveToFile();
delete m_gomoku;
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -