修改了窗口过程,这样看起来简单点
#include <Windows.h>
typedef WNDPROC WMPROC;
typedef struct{
UINT msg;
WMPROC wmProc;
}MsgWithProc;
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE L"致我们永不熄灭的游戏开发梦想——GDI程序核心框架"
HDC g_hdc = NULL;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMpaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMkeydown(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMdestory(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL Game_Init(HWND hwnd);
VOID Game_Paint(HWND hwnd);
BOOL Game_CleanUP(HWND hwnd);
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 = NULL;
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
);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
if(!Game_Init(hwnd)){
MessageBox(hwnd, L"资源初始化失败", L"消息窗口", 0);
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", hInstance);
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
MsgWithProc msgWithProc[] = {
{WM_PAINT,WMpaint},
{WM_KEYDOWN,WMkeydown},
{WM_DESTROY,WMdestory}
};
for(MsgWithProc mwp : msgWithProc){
if(mwp.msg == message){
mwp.wmProc(hwnd, message, wParam, lParam);
return 0;
}
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
LRESULT __stdcall WMpaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
PAINTSTRUCT paintStruct;
g_hdc = BeginPaint(hwnd, &paintStruct);
Game_Paint(hwnd);
EndPaint(hwnd, &paintStruct);
ValidateRect(hwnd, NULL);
return 0;
}
LRESULT __stdcall WMkeydown(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
if(wParam == VK_ESCAPE){
DestroyWindow(hwnd);
}
return 0;
}
LRESULT __stdcall WMdestory(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
Game_CleanUP(hwnd);
PostQuitMessage(0);
return 0;
}
BOOL Game_Init(HWND hwnd){
g_hdc = GetDC(hwnd);
Game_Paint(hwnd);
ReleaseDC(hwnd, g_hdc);
return TRUE;
}
VOID Game_Paint(HWND hwnd){
HFONT hFont = CreateFont(30, 0, 0, 0, 0, 0, 0, 0, GB2312_CHARSET, 0, 0, 0, 0, L"微软雅黑");
SelectObject(g_hdc, hFont);
SetBkMode(g_hdc, TRANSPARENT);
wchar_t text1[] = L"我们所有的梦想都可以成真,只要我们有勇气去追求它们。";
wchar_t text2[] = L"All our dreams can come true, if we have the courage to pursue them. ";
wchar_t text3[] = L"-------沃尔特 迪斯尼";
SetTextColor(g_hdc, RGB(50, 255, 50));
TextOut(g_hdc, 30, 150, text1, wcslen(text1));
SetTextColor(g_hdc, RGB(50, 50, 255));
TextOut(g_hdc, 30, 200, text2, wcslen(text2));
SetTextColor(g_hdc, RGB(255, 150, 50));
TextOut(g_hdc, 500, 250, text3, wcslen(text3));
DeleteObject(hFont);
}
BOOL Game_CleanUP(HWND hwnd){
return TRUE;
}