源代码:
#include "stdafx.h"
#include "GDIDemoWindowsDraft.h"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE L"【Windows窗口测试——GDI练习】"
HDC g_hdc = NULL; // 新增:全局设备环境句柄
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL Game_Init(HWND hwnd); // 新增:函数,进行资源的初始化
VOID Game_Paint(HWND hwnd); // 新增:函数,进行绘图代码的书写
BOOL Game_CleanUp(HWND hwnd); // 新增:函数,进行资源的清理
//-----------------------------------【WinMain( )函数】--------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX wndClass = { 0 };
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = (HICON)::LoadImage(NULL, L"icon.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = L"ForTheDreamOfGameDevelop";
if (!RegisterClassEx(&wndClass))
return -1;
HWND hwnd = CreateWindow(L"ForTheDreamOfGameDevelop",
WINDOW_TITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
WINDOW_WIDTH,
WINDOW_HEIGHT,
NULL,
NULL,
hInstance,
NULL);
MoveWindow(hwnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, true);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
// 新增:游戏资源的初始化,若初始化失败,弹出一个消息框,并返回FALSE
if (!Game_Init(hwnd))
{
MessageBox(hwnd, L"资源初始化失败", L"消息窗口", 0); //使用MessageBox函数,创建一个消息窗口
return FALSE;
}
MSG msg = { 0 };
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
UnregisterClass(L"ForTheDreamOfGameDevelop", wndClass.hInstance);
return 0;
}
//-----------------------------------【WndProc( )函数】--------------------------------------
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT paintStruct; //定义一个PAINTSTRUCT结构体来记录一些绘制信息
switch (message) //switch语句开始
{
case WM_PAINT: // 若是客户区重绘消息
g_hdc = BeginPaint(hwnd, &paintStruct); //指定窗口进行绘图工作的准备,并用将和绘图有关的信息填充到paintStruct结构体中。
Game_Paint(hwnd);
EndPaint(hwnd, &paintStruct); //EndPaint函数标记指定窗口的绘画过程结束
ValidateRect(hwnd, NULL); // 更新客户区的显示
break; //跳出该switch语句
case WM_KEYDOWN:
if (wParam == VK_ESCAPE)
DestroyWindow(hwnd);
break;
case WM_DESTROY: //若是窗口销毁消息
Game_CleanUp(hwnd); //调用自定义的资源清理函数Game_CleanUp()进行退出前的资源清理
PostQuitMessage(0); //向系统表明有个线程有终止请求。用来响应WM_DESTROY消息
break; //跳出该switch语句
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
//-----------------------------------【Game_Init( )函数】--------------------------------------
// 描述:初始化函数,进行一些简单的初始化
//------------------------------------------------------------------------------------------------
BOOL Game_Init( HWND hwnd )
{
g_hdc = GetDC(hwnd);
Game_Paint( hwnd);
return TRUE;
}
//-----------------------------------【Game_Paint( )函数】--------------------------------------
// 描述:绘制函数,在此函数中进行绘制操作
//--------------------------------------------------------------------------------------------------
VOID Game_Paint( HWND hwnd )
{
}
//-----------------------------------【Game_CleanUp( )函数】--------------------------------
// 描述:资源清理函数,在此函数中进行程序退出前资源的清理工作
//---------------------------------------------------------------------------------------------------
BOOL Game_CleanUp( HWND hwnd )
{
ReleaseDC(hwnd,g_hdc);
return TRUE;
}