// Zhych22.cpp : Defines the entry point for the application.
//
#include <tchar.h>
#include "resource.h"
#include "RussiaBox.h"
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)
{
//进程句柄赋值。
g_hInstance = hInstance;
//定义一个窗口类结构体变量,对相关参数进行设置。
WNDCLASS wndclass = {0};
//窗口显示样式。
wndclass.style = CS_HREDRAW | CS_VREDRAW;
//窗口消息响应过程函数。
wndclass.lpfnWndProc = WindowProc;
//进程句柄。
wndclass.hInstance = hInstance;
//程序图标,修改可在源文件目录下替换图标文件。
wndclass.hIcon = LoadIcon(hInstance, MAKEINTATOM(IDI_ICON));
//主窗口背景刷。
wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
//窗口类名。
wndclass.lpszClassName = _T("SDKWND");
//以下是窗口菜单设置,没有的话可以不用设置,菜单请从资源里新建或插入。
wndclass.lpszMenuName = MAKEINTATOM(IDR_MENU);
//注册窗口类。
if(RegisterClass(&wndclass) == 0)
{
MessageBox(NULL, _T("注册窗口类失败!"), _T("提示:"), MB_OK | MB_ICONEXCLAMATION);
return 0;
}
//取屏幕宽度。
int nScrWidth = GetSystemMetrics(SM_CXSCREEN);
//取屏幕高度。
int nScrHeight = GetSystemMetrics(SM_CYSCREEN);
//设置窗口左上角位于屏幕X坐标的位置。
int nWndX = nScrWidth / 2 - nWndWidth / 2;
//设置窗口左上角位于屏幕Y坐标的位置。
int nWndY = nScrHeight / 2 - nWndHeight / 2;
//创建窗口。
HWND hwnd = CreateWindow(_T("SDKWND"), _T("俄罗斯方块"), YCWND, nWndX, nWndY,
nWndWidth, nWndHeight, NULL, NULL, hInstance, NULL);
//判断窗口句柄是否为空,为空则创建窗口失败。
if(hwnd == NULL)
{
MessageBox(NULL, _T("创建窗口失败!"), _T("提示:"), MB_OK | MB_ICONEXCLAMATION);
return 0;
}
//载入窗口。
if(ShowWindow(hwnd, nCmdShow) == true)
{
MessageBox(hwnd, _T("载入窗口失败!"), _T("提示:"), MB_OK | MB_ICONEXCLAMATION);
return 0;
}
//刷新窗口。
if(UpdateWindow(hwnd) == false)
{
MessageBox(hwnd, _T("刷新窗口失败!"), _T("提示:"), MB_OK | MB_ICONEXCLAMATION);
return 0;
}
//如果窗口设置的有快捷键,请按以下方式加载。
//HACCEL haccel = LoadAccelerators(hInstance, MAKEINTATOM(快捷键ID));
//if(haccel == NULL)
//{
// MessageBox(hwnd, _T("加载加速键失败!"), _T("提示:"), MB_OK | MB_ICONASTERISK);
// return 0;
//}
//定义一个消息结构体。
MSG msg;
int nMsg = 0;
while(nMsg = GetMessage(&msg, NULL, 0, 0))
{
if(nMsg == -1)
{
break;
}
//如果程序中使用了快捷键,请参照以下方式
//if(TranslateAccelerator(msg.hwnd, haccel, &msg) == false)
//{
// TranslateMessage(&msg);
// DispatchMessage(&msg);
//}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_KEYDOWN:
{
int nMsg = LOWORD(wParam);
//处理游戏按键消息。
KeyClick(hwnd, nMsg);
break;
}
case WM_PAINT:
{
//绘制游戏界面。
DrewGmUi(hwnd, g_UiBox, g_nLeft, g_nTop);
break;
}
case WM_CLOSE:
{
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
}
case WM_COMMAND:
{
//此处为所有键盘按键消息的处理。
int nCmd = LOWORD(wParam);
switch(nCmd)
{
case IDM_STGM:
{
//开始游戏。
StrartGame(hwnd);
break;
}
}
break;
}
case WM_CREATE:
{
//初始化游戏界面图标句柄。
InitGm(hwnd);
//设置按钮的宽度和高度。
int nBtnWidth = 100;
int nBtnHeight = 25;
break;
}
default : return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
/************************************************************************/
/* 函数功能:初始化界面图标元素句柄 */
/*参数一:窗口句柄。 */
/************************************************************************/
void InitGm(HWND hwnd)
{
g_bgicon = LoadIcon(g_hInstance, MAKEINTATOM(IDI_BGICON));
g_fgicon = LoadIcon(g_hInstance, MAKEINTATOM(IDI_FGICON));
}
/************************************************************************/
/* 函数功能:绘制游戏界面 */
/*参数一:窗口句柄 */
/*参数二:绘制的图形样式 */
/*参数三:绘制的开始坐标X */
/*参数四:绘制的开始坐标Y */
/************************************************************************/
void DrewGmUi(HWND hwnd, const BOX *box, int x, int y)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT rect;
GetClientRect(hwnd, &rect);
//游戏界面的X坐标的开始位置。
int nLeft = ((rect.right - rect.left) - ngmWidth * 32) / 2;
//游戏界面的Y坐标的开始位置。
int nTop = ((rect.bottom - rect.top) - ngmHeight * 32) / 2;
for(int i = 0; i < ngmHeight; i++)
{
for(int j = 0; j < ngmWidth; j++)
{
DrawIcon(hdc, nLeft + j * 32, nTop + i * 32, g_bgicon);
}
}
if(box != NULL)
{
//绘制方块。
for(i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(box->nData[i][j] == 1)
{
DrawIcon(hdc, x + j * 32, y + i * 32, g_fgicon);
}
}
}
}
EndPaint(hwnd, &ps);
}
//开始游戏。
void StrartGame(HWND hwnd)
{
g_UiBox = &myRussiaBox[5].ClassData[g_nDef];
InvalidateRect(hwnd, NULL, false);
}
/************************************************************************/
/* 函数功能:处理游戏按键消息 */
/*参数一:消息值 */
/************************************************************************/
void KeyClick(HWND hwnd, int nMsg)
{
switch(nMsg)
{
case VK_UP:
{
if(g_UiBox == NULL)
{
break;
}
if(g_nDef == 3)
{
g_nDef = 0;
}
else
{
g_nDef++;
}
g_UiBox = &myRussiaBox[5].ClassData[g_nDef];
InvalidateRect(hwnd, NULL, false);
break;
}
case VK_DOWN:
{
//Y坐标加1
break;
}
case VK_LEFT:
{
//X坐标减1
break;
}
case VK_RIGHT:
{
//X坐标加1
break;
}
}
}