#include <windows.h>

HINSTANCE g_hInst = NULL;

//2 窗口处理函数
LRESULT CALLBACK WndProc( HWND hWnd,
UINT nMsg,
WPARAM wParam,
LPARAM lParam )
{
//判断消息ID
switch( nMsg )
{
case WM_DESTROY: //窗口销毁的消息
PostQuitMessage( 0 ); //发送退出消息
return 0;
}
//调用缺省的消息处理程序
return DefWindowProc( hWnd, nMsg,
wParam, lParam );
} //3 注册窗口类型
BOOL MyRegister( LPSTR pszClassName )
{
WNDCLASS wc = {0};
ATOM nAtom = 0;
//构造注册窗口的参数
wc.style = CS_VREDRAW|CS_HREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = g_hInst;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE);
wc.lpszMenuName = NULL;
wc.lpszClassName = pszClassName;
//注册窗口
nAtom = RegisterClass( &wc );
if( 0 == nAtom )
{
MessageBox( NULL, "Register Failed",
"Error", MB_OK|MB_ICONWARNING );
return FALSE;
}
return TRUE;
} //4 窗口创建
HWND MyCreate( LPSTR pszClassName )
{
HWND hWnd = NULL;
//创建窗口
hWnd = CreateWindow( pszClassName,
"HelloWnd", /*WS_OVERLAPPEDWINDOW*/WS_POPUP,
0, 0, 800, 600, NULL, NULL,
g_hInst, NULL );
if( NULL == hWnd )
{
MessageBox( NULL, "CreateWnd Failed",
"Error", MB_OK );
return NULL;
} return hWnd;
} //5 显示窗口
void DisplayWnd( HWND hWnd )
{
//显示
ShowWindow( hWnd, SW_SHOW );
//刷新
UpdateWindow( hWnd );
}
//6 消息处理
void Message( )
{
MSG msg = { 0 };
//消息循环处理,获取消息
while( GetMessage( &msg, NULL, 0, 0 ) )
{
//派发消息
DispatchMessage( &msg );
}
}
//1 入口函数
int WINAPI WinMain( HINSTANCE hInst,
HINSTANCE hPrevInst,
LPSTR pszCmdLine,
int nShowCmd )
{
HWND hWnd = NULL;
g_hInst = hInst;
//注册窗口类型
MyRegister( "MyWnd" );
//创建注册类型的窗口
hWnd = MyCreate( "MyWnd" );
//显示窗口
DisplayWnd( hWnd );
//消息处理
Message( );
return 0;
}
05-11 22:49