win32应用程序的设计主线:
首先进入WinMain函数,然后设计窗口类、注册窗口类、产生窗口、显示窗口、更新窗口,最后进入消息循环,将消息路由到窗口过程函数中去处理。
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="sunxin2006";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;//CS_开头的类样式(Class Style),采用的是位标示,所以这里用|更加灵活,意思相当于了并集。
RegisterClass(&wndcls); //注册窗口类。HWND hwnd;
hwnd=CreateWindow("sunxin2006","http://www.sunxin.org",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);//终于可以创建窗口了。ShowWindow(hwnd,SW_SHOWNORMAL); //显示窗口哦
UpdateWindow(hwnd);MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg); //用于将虚拟键消息转换为字符消息
DispatchMessage(&msg); //分派一个消息到窗口过程,由窗口过程函数对消息进行处理。
}
return msg.wParam;
}
1、WinMain:程序的入口函数
WinMain函数的原型声明如下:
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
);
2、