本文毫无看点,完全是把系统自动生成的win32代码拷贝过来,而已。
#include "stdafx.h"
#include "game01.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// 函数的声明
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);//宏,对为引用的对象进行处理,消除警告
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// 配置到全局配置中,在.rc中进行配置
//hInstance应用程序实例句柄,IDS_APP_TITLE 资源中的字符串编号,szTitle 资源文件里面的内容会先拷贝到这个里面,MAX_LOADSTRING是szTitle的大小
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_GAME01, szWindowClass, MAX_LOADSTRING);
//1.注册window类
MyRegisterClass(hInstance);
//2.初始化window窗体
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GAME01));
// 消息循环,这种方式如果没有处理完当前消息,需要进行等待直到做完
while (PeekMessage(&msg, NULL, 0, 0,PM_REMOVE)) //PM_REMOVE 处理完后,消息从队列里面移走
{
if(msg.message == WM_QUIT){
break;
}
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//TODO
}
消息循环,这种方式如果没有处理完当前消息,需要进行等待直到做完
//while (GetMessage(&msg, NULL, 0, 0))
//{
// if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
// {
// TranslateMessage(&msg);
// DispatchMessage(&msg);
// }
//}
return (int) msg.wParam;
}
/**
注册一个window对象
*/
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX); //当前类的大小
wcex.style = CS_HREDRAW | CS_VREDRAW; //窗口的类型,意思为横向纵向移动的时候,窗口刷新。
wcex.lpfnWndProc = WndProc; //一个函数指针,这个函数主要用于处理事件。
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance; //当前实例句柄,由winmain传入
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GAME01));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); //背景颜色,这个地方获取的是画刷对象
wcex.lpszMenuName = NULL;//MAKEINTRESOURCE(IDC_GAME01);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
/**
初始化,创建窗体并运行
*/
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 把实例句柄存储为全局变量
//创建window窗体,同一个实例句柄可以创建多个窗体并返回它们的句柄,
//所有窗体的事件都是在同一个回调函数里面处理的
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
//显示和刷新window窗体
ShowWindow(hWnd, nCmdShow); //nCmdShow 打开窗体的方式
UpdateWindow(hWnd);
return TRUE;
}
/**
回调函数,处理事件
*/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps); //hWnd是绘制的窗口的句柄,ps是区域。
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0); //系统停止消息
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam); //使用默认的事件处理函数进行处理
}
return 0;
}