WIN32.02.改良版的Win32应用程序-封装
WIN32.02.改良版的Win32应用程序-封装
既然每个程序都需要注册窗口类,都需要CreateWindow、UpdateWidow、ShowWindow,那么我们应该把注册窗口类和窗口处理部分分别写入不同的函数,以方便调用。最好,我们有一个Window类实现这个功能。
/************************************************************************/
/* Win32Imp.h :Window类的头文件 */
/************************************************************************/
#include <windows.h>
class Window
{
private:
TCHAR *ClassName;
TCHAR *AppName;
public:
Window(TCHAR* classname, TCHAR* appname);
/* InitApplication:完成窗口类的注册,输入实例句柄和窗口过程地址;失败返回FALSE */
BOOL InitApplication(HINSTANCE hInstance, WNDPROC WndProc );
/* InitInstance:CreateWindow、ShowWindow、UpdateWindow;失败返回FALSE,成功返回窗口句柄*/
HWND InitInstance(HINSTANCE hInstance, int nCmdShow);
};
/************************************************************************/
/* Win32Imp.cpp :Window类的实现代码 */
/************************************************************************/
#include "Win32Imp.h"
Window::Window(TCHAR* classname, TCHAR* appname)
{
this->ClassName = classname;
this->AppName = appname;
}
BOOL Window::InitApplication(HINSTANCE hInstance, WNDPROC WndProc )
{
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground =(HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WndProc; //Windows Message Handle
wndclass.lpszClassName = this->ClassName;
wndclass.lpszMenuName =NULL;// this->AppName;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
BOOL bOK = RegisterClass(&wndclass);
return bOK;
}
HWND Window::InitInstance(HINSTANCE hInstance, int nCmdShow)
{
//与WNDCLASS关联项:hInstance、lpszClassName、lpszMenuName;CreateWindow调用失败返回NULL
HWND hwnd = CreateWindow(this->ClassName,TEXT("Win32 application test"),WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
return hwnd;
}
/************************************************************************
* WinMain.cpp :主程序
* 编译方法:
cl -c Win32Imp.cpp
cl -c WinMain.cpp
link Win32Imp.obj Winmain.obj kernel32.lib user32.lib gdi32.ib
************************************************************************/
#include "Win32Imp.h"
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg,WPARAM wParam, LPARAM lParam)
{
HDC hdc;
RECT rect;
PAINTSTRUCT ps;
char info[100]= "Coded by Minghui Xu. TIMESTAMP : ";
switch(uMsg)
{
case WM_CREATE:
MessageBeep(0);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
strcat(info, __TIMESTAMP__);
DrawTextA(hdc,info,-1,&rect,DT_VCENTER |DT_CENTER | DT_SINGLELINE );
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
int CALLBACK WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
MSG msg;
TCHAR classname[] = TEXT("WIN32APP");
TCHAR appname[100] = TEXT("Windows 32 Bit Application test.");
Window *window = new Window(classname, appname); //此处注意,必须用指针方式Window *window
if(!window->InitApplication(hInstance,WndProc))
return 0;
HWND hwnd = window->InitInstance(hInstance,nShowCmd);
if(hwnd == NULL)
return 0;
while (GetMessage(&msg,0,NULL,NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}