转:WIN32.02.改良版的Win32应用程序-封装

 1 #include <windows.h>
 2 
 3 class Window{
 4 private:
 5     TCHAR *ClassName;
 6     TCHAR *AppName;
 7 public:
 8     Window(TCHAR* classname, TCHAR* appname);
 9     
10     BOOL InitApplication(HINSTANCE hInstance, WNDPROC WndProc );
11     
12     HWND InitInstance(HINSTANCE hInstance, int nCmdShow);
13 };
14 
15 //构造器
16 Window::Window(TCHAR* classname, TCHAR* appname){
17     this->ClassName = classname;
18     this->AppName = appname;
19 }
20 
21 //完成窗口类的注册,输入实例句柄和窗口过程地址;失败返回FALSE
22 BOOL Window::InitApplication(HINSTANCE hInstance, WNDPROC WndProc ){
23 24     WNDCLASS wndclass;
25     wndclass.cbClsExtra = 0;
26     wndclass.cbWndExtra = 0;
27     wndclass.hbrBackground =(HBRUSH) GetStockObject(WHITE_BRUSH);
28     wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
29     wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
30     wndclass.hInstance = hInstance;
31     wndclass.lpfnWndProc = WndProc;       //Windows Message Handle
32     wndclass.lpszClassName = this->ClassName;
33     wndclass.lpszMenuName =NULL;// this->AppName;
34     wndclass.style = CS_HREDRAW | CS_VREDRAW;
35     BOOL bOK = RegisterClass(&wndclass);
36     return bOK;
37 }
38 
39 //CreateWindow、ShowWindow、UpdateWindow;失败返回FALSE,成功返回窗口句柄
40 HWND Window::InitInstance(HINSTANCE hInstance, int nCmdShow){
41     //与WNDCLASS关联项:hInstance、lpszClassName、lpszMenuName;CreateWindow调用失败返回NULL
42     HWND hwnd = CreateWindow(this->ClassName,TEXT("Win32 application test"),WS_OVERLAPPEDWINDOW,
43               CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
44     ShowWindow(hwnd,nCmdShow);
45     UpdateWindow(hwnd);
46     return hwnd;
47 }
48 
49 //回调函数
50 LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg,WPARAM wParam, LPARAM lParam){
51     HDC hdc;
52     RECT rect;
53     PAINTSTRUCT ps;
54     char info[100]= "...for test win32...";
55     switch(uMsg){
56         case WM_CREATE:
57                 MessageBeep(0);
58                 return 0;
59         case WM_PAINT:
60                 hdc = BeginPaint(hwnd,&ps);
61                 GetClientRect(hwnd,&rect);
62                 strcat(info, __TIMESTAMP__);
63                 DrawTextA(hdc,info,-1,&rect,DT_VCENTER |DT_CENTER | DT_SINGLELINE );
64                 EndPaint(hwnd,&ps);
65                 return 0;
66         case WM_DESTROY:
67                 PostQuitMessage(0);
68                 return 0;
69     }
70     return DefWindowProc(hwnd,uMsg,wParam,lParam);
71 }
72 
73 //主程
74 int CALLBACK WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd ){
75     MSG msg;
76     TCHAR classname[] = TEXT("WIN32APP");
77     TCHAR appname[100] = TEXT("Windows 32 Bit Application test.");
78     Window *window = new Window(classname, appname); //此处注意,必须用指针方式Window *window
79     if(!window->InitApplication(hInstance,WndProc))
80             return 0;
81     HWND hwnd = window->InitInstance(hInstance,nShowCmd);
82     if(hwnd == NULL)
83             return 0;
84     while (GetMessage(&msg,0,NULL,NULL)){
85             TranslateMessage(&msg);
86             DispatchMessage(&msg);
87     }
88     return (msg.wParam);
89 }

转FROM:http://www.189works.com/article-6803-1.html

posted @ 2012-06-01 21:41  完美科技  阅读(179)  评论(0编辑  收藏  举报