一次简单的面向对象抽象
2016-06-23 19:52
274 查看
[align=justify]最近拿起《Windows程序设计(第5版珍藏版)》来看,其中第3章的那个演示程序可谓是Windows程序的经典写法。条理清晰,结构明了,几乎没有什么好挑剔的了。代码如下:[/align]
[align=justify]这段代码看久了会觉得比较乏味。于是想着能不能用OO的方式来抽象封装一下。[/align]
[align=justify]于是就有了下面的代码(main.cpp):[/align]
[align=justify]这下干净多了,不过那个application.h里面是什么东西呢?就是对应用程序的封装,代码如下:[/align]
[align=justify]Application的实现如下(application.cpp):[/align]
[align=justify]然后刚才的application.h中引用了window.h,这个文件中包含了窗口的封装,代码如下:[/align]
#include<windows.h> LRESULT CALLBACK WndProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE instance, HINSTANCE otherInstance, PSTR cmdline, int showType) { static TCHAR applicationName[] = TEXT("HelloWin"); WNDCLASS wndClass; wndClass.style = CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = WndProc; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = instance; wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndClass.hCursor = LoadCursor(NULL, IDI_APPLICATION); wndClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); wndClass.lpszMenuName = NULL; wndClass.lpszClassName = applicationName; if(!RegisterClass(&wndClass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), applicationName, MB_ICONERROR); } HWND window; window = CreateWindow(applicationName, TEXT("The Hello Program"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, instance, NULL); ShowWindow(window, showType); UpdateWindow(window); MSG message; while(GetMessage(&message, NULL, 0, 0)) { TranslateMessage(&message); DispatchMessage(&message); } return message.wParam; } LRESULT CALLBACK WndProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_CREATE: PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC); return 0; case WM_PAINT: { RECT rectangle; GetClientRect(window, &rectangle); PAINTSTRUCT ps; HDC deviceContent = BeginPaint(window, &ps); DrawText(deviceContent, TEXT("Hello, Windows 98!"), -1, &rectangle, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint(window, &ps); } return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(window, message, wParam, lParam); }
[align=justify]这段代码看久了会觉得比较乏味。于是想着能不能用OO的方式来抽象封装一下。[/align]
[align=justify]于是就有了下面的代码(main.cpp):[/align]
#include<windows.h> #include"application.h" int WINAPI WinMain(HINSTANCE instance, HINSTANCE otherInstance, PSTR cmdline, int showType) { Application application(instance); return application.run(showType); }
[align=justify]这下干净多了,不过那个application.h里面是什么东西呢?就是对应用程序的封装,代码如下:[/align]
#ifndef APPLICATION_H #define APPLICATION_H #include<windows.h> #include"window.h" class Application { public: Application(HINSTANCE instance); public: int run(int showType); private: Window window; }; #endif //APPLICATION_H
[align=justify]Application的实现如下(application.cpp):[/align]
#include"application.h" Application::Application(HINSTANCE instance) : window(instance) {} int Application::run(int showType) { window.show(showType); MSG message; while(GetMessage(&message, NULL, 0, 0)) { TranslateMessage(&message); DispatchMessage(&message); } return static_cast<int>(message.wParam); }
[align=justify]然后刚才的application.h中引用了window.h,这个文件中包含了窗口的封装,代码如下:[/align]
#ifndef WINDOW_H #define WINDOW_H #include<windows.h> class Window { public: Window(HINSTANCE instance); public: void show(int showType); public: static LRESULT CALLBACK WndProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam); public: WNDCLASS wndClass; HWND window; }; #endif //WINDOW_H窗口的实现代码如下所示(window.cpp):
#include"window.h" Window::Window(HINSTANCE instance) { static TCHAR applicationName[] = TEXT("HelloWindows"); static TCHAR applicationTitle[] = TEXT("Hello Windows 98!"); wndClass.style = CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = WndProc; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = instance; wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndClass.hCursor = LoadCursor(NULL, IDI_APPLICATION); wndClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); wndClass.lpszMenuName = NULL; wndClass.lpszClassName = applicationName; if(!RegisterClass(&wndClass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), applicationTitle, MB_ICONERROR); } window = CreateWindow(applicationName, applicationTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, instance, NULL); } void Window::show(int showType) { ShowWindow(window, showType); UpdateWindow(window); } LRESULT CALLBACK Window::WndProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_CREATE: PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC); return 0; case WM_PAINT: { RECT rectangle; GetClientRect(window, &rectangle); PAINTSTRUCT ps; HDC deviceContent = BeginPaint(window, &ps); DrawText(deviceContent, TEXT("Hello, Windows 98!"), -1, &rectangle, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint(window, &ps); } return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(window, message, wParam, lParam); }至此,整个抽象封装结束了,总算不用一下子看一堆代码了。
相关文章推荐
- 如何重装TCP/IP协议
- Windows 8 官方高清壁纸欣赏与下载
- 谁是桌面王者?Win PK Linux三大镇山之宝
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- Windows Clang开发环境备忘
- 从Windows系统下访问Linux分区相关软件
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- Windows下搭建本地SVN服务器
- 使用Windows原生命令一键清空剪贴板
- windows用windeployqt发布qt quick application程序
- 利用开源软件打造自己的全功能远程工具
- Windows 8虚拟机不能全屏的解决方法
- Visual Studio 2012 示例代码浏览器 - 数以千计的开发示例近在手边,唾手可得
- Visual Studio 2012 示例代码浏览器 - 数以千计的开发示例近在手边,唾手可得
- 微软镜像下载
- 一步一步跟我学易语言之第二个易程序菜单设计
- windows server域用户提升到本地更高权限组中的方法