windows应用程序编程是一种基于消息的编程。
一般创建一个窗口应用程序, 按以下步骤:
1. winmain函数是windows程序的入口函数。
int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state );
2. 创建窗口对象, 并初始化其成员函数。
typedef struct _WNDCLASS { UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HINSTANCE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName; } WNDCLASS, *PWNDCLASS;
3. 注册窗口对象。
ATOM RegisterClass( CONST WNDCLASS *lpWndClass // class data );4. 创建窗口。HWND CreateWindow( LPCTSTR lpClassName, // registered class name LPCTSTR lpWindowName, // window name DWORD dwStyle, // window style int x, // horizontal position of window int y, // vertical position of window int nWidth, // window width int nHeight, // window height HWND hWndParent, // handle to parent or owner window HMENU hMenu, // menu handle or child identifier HINSTANCE hInstance, // handle to application instance LPVOID lpParam // window-creation data );5. 显示更新窗口。BOOL ShowWindow( HWND hWnd, // handle to window int nCmdShow // show state );BOOL UpdateWindow( HWND hWnd // handle to window );6.消息循环。BOOL GetMessage( LPMSG lpMsg, // message information HWND hWnd, // handle to window UINT wMsgFilterMin, // first message UINT wMsgFilterMax // last message );BOOL TranslateMessage( CONST MSG *lpMsg // message information );LRESULT DispatchMessage( CONST MSG *lpmsg // message information );如下一个win32的简单窗口应用程序:MFC-----Microsoft Foundation ClassesMFC编写win32应用程序基本脉络和上面是一样的。但是我们用Wizard新建程序时, Wizard都替我们把这些事情都做好了。我们只要基于它的框架进行修改和消息循环操作即可。MFC更能体现出面向对象的编程思维。如我们进行窗口应用程序编程时, 主要就是对窗口类的设计和操作。