/**************************************************
WinMain.cpp
应用程序框架的模型
**************************************************/
// Include files
#include <windows.h>
#include <stdio.h>
// Window handles, class and caption text 声明WINDOWS句柄 和窗体类的标题类名等
HWND g_hWnd;
HINSTANCE g_hInst;
static char g_szClass[] = "ShellClass";
static char g_szCaption[] = "Shell Application";
// Function prototypes
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, /
LPSTR szCmdLine, int nCmdShow);
long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, /
WPARAM wParam, LPARAM lParam);
BOOL DoInit(); //声明初始化函数
BOOL DoShutdown();//声明退出函数
BOOL DoFrame();//声明主框架函数
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, /
LPSTR szCmdLine, int nCmdShow)
{
WNDCLASSEX wcex;
MSG Msg;
g_hInst = hInst;
// Create the window class here and register it
//设置窗体与注册窗体
wcex.cbSize = sizeof(wcex);
wcex.style = CS_CLASSDC;
wcex.lpfnWndProc = WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInst;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = g_szClass;
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wcex))
return FALSE;
//建立窗体
// Create the Main Window
g_hWnd = CreateWindow(g_szClass, g_szCaption,
WS_CAPTION | WS_SYSMENU,
0, 0, 400, 400,
NULL, NULL,
hInst, NULL );
if(!g_hWnd)
return FALSE;
//显示与更新窗体
ShowWindow(g_hWnd, SW_NORMAL);
UpdateWindow(g_hWnd);
// Run init function and return on error
if(DoInit() == FALSE)
return FALSE;
// Start message pump, waiting for signal to quit
ZeroMemory(&Msg, sizeof(MSG)); //ZeroMemory 宏为填充一内存块为零
//&Msg :starting address of the block of memory to fill with zeros sizeof(MSG):长度
//消息循环
while(Msg.message != WM_QUIT) {
if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
//调用框架函数
if(DoFrame() == FALSE)
break;
}
// Run shutdown function
DoShutdown();
//The UnregisterClass function unregisters a window class, freeing the memory required for the class.
//窗体类的类名, 窗口句柄
UnregisterClass(g_szClass, hInst);
return Msg.wParam;
}
//回调函数
long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, /
WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
case WM_DESTROY:
PostQuitMessage(0); //退出窗口消息
return 0;
}
//返回默认消息处理
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
BOOL DoInit()
{
return TRUE;
}
BOOL DoShutdown()
{
return TRUE;
}
BOOL DoFrame()
{
return TRUE;
}