头文件:MainClass.h

内容:

#include <Windows.h>


//全局函数声明
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nShowCmd);
void MyPaint(HDC hdc);
BOOL Game_CleanUp(HWND hwnd);//在此函数中进行资源的清理

 

以下是框架内容:

#include "MainClass.h"

#define WINDOW_WIDTH 800 //为窗口宽度定义的宏,以方便在此处修改窗口宽度
#define WINDOW_HEIGHT 600 //为窗口高度定义的宏,以方便在此处修改窗口高度
#define WINDOW_TITLE "游戏开发的梦想" //为窗口标题定义的宏

//全局变量
HINSTANCE hInst;
HPEN hPen[7];
HBRUSH hBru[7];
int sPen[7] = { PS_SOLID, PS_DASH, PS_DOT, PS_DASHDOT, PS_DASHDOTDOT, PS_NULL, PS_INSIDEFRAME };
int sBru[6] = { HS_VERTICAL, HS_HORIZONTAL, HS_CROSS, HS_DIAGCROSS, HS_FDIAGONAL, HS_BDIAGONAL };

//--------------------------------------------WinMain() 入口函数-----------------------------------------------
//应用程序的入口函数
//-------------------------------------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg = { 0 };//定义并初始化msg
MyRegisterClass(hInstance);

if (!InitInstance(hInstance, nShowCmd))
{
return FALSE;
}

//游戏循环
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

//--------------------------------------------MyRegisterClass() 函数-------------------------------------------
//注册窗口
//-------------------------------------------------------------------------------------------------------------
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//创建窗口第一步:开始设计一个完整的窗口类
WNDCLASSEX wndClass;//定义一个窗口类
wndClass.cbSize = sizeof(WNDCLASSEX);//设置结构体的字节数大小
wndClass.style = CS_HREDRAW | CS_VREDRAW;//设置窗口的样式
wndClass.lpfnWndProc = (WNDPROC)WndProc;//设置指向窗口过程函数的指针
wndClass.cbClsExtra = 0;//窗口类的附加内存,取0就可以了
wndClass.cbWndExtra = 0;//窗口的附加内存,依然取0就行了
wndClass.hInstance = hInstance;//指定包含窗口过程的程序的实例句柄
wndClass.hIcon = (HICON) ::LoadImage(NULL, "icon.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);//加载自定义ico图标
wndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);//指定窗口类的光标句柄
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//指定一个灰色画刷句柄
wndClass.lpszMenuName = NULL;//用一个以空终止的字符串,指定菜单资源的名字.
wndClass.lpszClassName = "GameClass";//指定窗口类的名称
//wndClass.hIconsm = NULL;

//创建窗口第二步:注册窗口类
return RegisterClassEx(&wndClass);//设计完窗口后,需要对窗口类进行注册,这样才能创建该类型的窗口
}

//--------------------------------------------InitInstance() 函数----------------------------------------------
//加载资源
//-------------------------------------------------------------------------------------------------------------
BOOL InitInstance(HINSTANCE hInstance, int nShowCmd)
{
HWND hwnd;
int i;
HDC hdc;

hInst = hInstance;

//创建窗口第三步:正式创建窗口
//创建窗口函数
hwnd = CreateWindow("GameClass", WINDOW_TITLE, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hwnd)
{
return FALSE;
}
//创建窗口第四步:窗口的移动,显示和更新
MoveWindow(hwnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, true);//调整窗口显示时的位置及窗口的大小
ShowWindow(hwnd, nShowCmd);//设定显示窗口时的状态
UpdateWindow(hwnd);//将窗口绘制于显示设备上

for (i = 0; i <= 6; i++)
{
hPen[i] = CreatePen(sPen[i], 1, RGB(255, 0, 0));
if (i == 6)
hBru[i] = CreateSolidBrush(RGB(0, 255, 0));
else
hBru[i] = CreateHatchBrush(sBru[i], RGB(0, 255, 0));
}

hdc = GetDC(hwnd);
MyPaint(hdc);
ReleaseDC(hwnd, hdc);

return TRUE;
}

//--------------------------------------------MyPaint() 函数---------------------------------------------------
//绘制图形
//-------------------------------------------------------------------------------------------------------------
void MyPaint(HDC hdc)
{
int i, x1, x2, y;
//以7种不同画笔绘制线条
for (i = 0; i <= 6; i++)
{
y = (i + 1) * 30;
SelectObject(hdc, hPen[i]);//选用画笔
MoveToEx(hdc, 30, y, NULL);//移到线条起点
LineTo(hdc, 100, y);//画线
}
x1 = 120;
x2 = 180;
for (i = 0; i <= 6; i++)
{
SelectObject(hdc, hBru[i]);//选用画刷
Rectangle(hdc, x1, 30, x2, y);//画封闭矩形
x1 += 70;
x2 += 70;
}
}


//--------------------------------------------Game_CleanUp() 函数----------------------------------------------
//资源清理函数,在此函数中进行退出前的资源的清理工作
//-------------------------------------------------------------------------------------------------------------
BOOL Game_CleanUp(HWND hwnd)
{
for (int i = 0; i < 7; i++)
{
DeleteObject(hPen[i]);//删除画笔
DeleteObject(hBru[i]);//删除画刷
}
return TRUE;
}

//--------------------------------------------WndProc() 窗口消息回调函数---------------------------------------
//窗口过程函数WndProc,对窗口消息进行处理
//-------------------------------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT://窗口重绘消息
{
hdc = BeginPaint(hwnd, &ps);
MyPaint(hdc);
EndPaint(hwnd, &ps);
}
break;
case WM_KEYDOWN://若是键盘按下消息
{
if (wParam == VK_ESCAPE)//如果按下的键是ESC
{
if (MessageBox(hwnd, "退出?", "提示", MB_YESNO | MB_ICONQUESTION) == IDYES)
DestroyWindow(hwnd);//销毁窗口,并发送一条WM_DESTROY消息
}
}
break;
case WM_CLOSE://关闭按钮
{
if (MessageBox(hwnd, "是否要退出?", "提示", MB_YESNO | MB_ICONQUESTION) == IDYES)
DestroyWindow(hwnd);//销毁窗口,并发送一条WM_DESTROY消息
else
return(0);
}
break;
case WM_DESTROY://窗口结束消息
{
//删除资源
Game_CleanUp(hwnd);
PostQuitMessage(0);//向系统表明有个线程有终止的请求,用来响应WM_DESTROY消息
}
break;
default://若是上述case条件都不符合,则执行该default语句
{
return DefWindowProc(hwnd, message, wParam, lParam);//调用默认的窗口过程
}
}
return 0;//正常退出
}

 

游戏开发的梦想

梦开始的地方

posted on 2015-11-21 15:32  zishen  阅读(1307)  评论(0编辑  收藏  举报