win32.h
#ifndef _X_WIN32_H_
#define _X_WIN32_H_
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE,int);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
#endif
win32.cpp
#include <windows.h>
#include "win32.h"
HINSTANCE g_hInst;
HWND g_hWnd;
char g_szAppName[]="WIN32Sample";
char g_szTitle[]="Win32 Sample Application";
int CALLBACK WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
MSG msg;
UNREFERENCED_PARAMETER(lpCmdLine);//to avoid compile warnning
if(!hPrevInstance)
{
if (!InitApplication(hInstance))
{
return FALSE;
}
}
if(!InitInstance(hInstance,nShowCmd))
{
return FALSE;
}
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;//PostQuitMessage's wParam
}
BOOL InitApplication( HINSTANCE hInstance )
{
WNDCLASS wc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance=hInstance;
wc.lpfnWndProc=(WNDPROC)WndProc;
wc.lpszClassName=g_szAppName;
wc.lpszMenuName=NULL;
wc.style=0;
return RegisterClass(&wc);
}
BOOL InitInstance( HINSTANCE hInstance,int nShowCmd)
{
g_hInst=hInstance;
g_hWnd= CreateWindow(g_szAppName, g_szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if(!g_hWnd)
{
return FALSE;
}
ShowWindow(g_hWnd,nShowCmd);//show window
UpdateWindow(g_hWnd);//send WM_PAINT to msgQue
return TRUE;
}
LRESULT CALLBACK WndProc( HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);//wnd destoryed,application will quit soon
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);//pass to default wndproc
}
return 0;
}
更多请参考:
http://www.ibm.com/developerworks/cn/linux/l-cn-guimigrt/index4.html?ca=drs-cn#N1019A