Windows程序代码重构
Posted HANCAO
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows程序代码重构相关的知识,希望对你有一定的参考价值。
代码重构:在程序功能实现之后,对代码进行一定规模的整理,使之符合“高内聚、低耦合”的软件设计原则,便于维护和使用。
①用函数封装消息处理代码——对Windows程序窗口函数中的每一个case程序段进行封装以形成一个消息处理函数,而在case中调用这个函数。
②利用数组或链表实现消息映射表进一步实现代码的隔离——因为窗口函数switch…case结构实质上实现的就是一个根据消息标识来查找消息处理代码的功能,故可以用消息映射表和一段查表程序来替代它,表中的每一项可以使用一个函数指针来指向消息处理函数。
固定的程序框架代码可以想办法用Windows程序开发工具自动生成,以减少程序员的工作量。
观察者模式(衍生于Windows消息发送者和消息处理者分开的做法)——java利用观察者模式实现了事件监听器模型、C#演化出了独特的事件委托模型。
1 #include<windows.h> 2 3 HINSTANCE hInst; 4 HWND hWnd; 5 MSG msg; 6 char lpszClassName[] = "窗口"; 7 char *ShowText; 8 9 ATOM MyRegisterClass(HINSTANCE hInstance); 10 BOOL Create(HINSTANCE,int); 11 int Run(); 12 13 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 14 void OnLButtonDown(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); 15 void OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); 16 void OnDestroy(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); 17 18 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lPCmdLine, int nCmdShow) 19 { 20 MyRegisterClass(hInstance); 21 Create(hInstance,nCmdShow); 22 ShowWindow(hWnd,nCmdShow); 23 UpdateWindow(hWnd); 24 return Run(); 25 } 26 27 ATOM MyRegisterClass(HINSTANCE hInstance) 28 { 29 WNDCLASS wc; 30 wc.style = 0; 31 wc.lpfnWndProc = WndProc; 32 wc.cbClsExtra = 0; 33 wc.cbWndExtra = 0; 34 wc.hInstance = hInstance; 35 wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); 36 wc.hCursor = LoadCursor(NULL,IDC_ARROW); 37 wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 38 wc.lpszMenuName = NULL; 39 wc.lpszClassName = lpszClassName; 40 return RegisterClass(&wc); 41 } 42 43 BOOL Create(HINSTANCE hInstance, int nCmdShow) 44 { 45 hWnd = CreateWindow(lpszClassName,"Windows",WS_OVERLAPPEDWINDOW,400,300,180,160,NULL,NULL,hInstance,NULL); 46 return TRUE; 47 } 48 49 int Run() 50 { 51 while (GetMessage(&msg, NULL, 0, 0)) 52 { 53 TranslateMessage(&msg); 54 DispatchMessage(&msg); 55 } 56 return msg.wParam; 57 } 58 59 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 60 { 61 switch (message) 62 { 63 case WM_LBUTTONDOWN: 64 OnLButtonDown(hWnd,message,wParam,lParam); 65 break; 66 case WM_PAINT: 67 OnPaint(hWnd,message,wParam,lParam); 68 break; 69 default: 70 return DefWindowProc(hWnd,message,wParam,lParam); 71 } 72 return 0; 73 } 74 75 void OnLButtonDown(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 76 { 77 ShowText = "Hello!";#include<windows.h> 78 79 HINSTANCE hInst; 80 HWND hWnd; 81 MSG msg; 82 char lpszClassName[] = "窗口"; 83 char *ShowText; 84 85 ATOM MyRegisterClass(HINSTANCE hInstance); 86 BOOL Create(HINSTANCE,int); 87 int Run(); 88 89 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 90 void OnLButtonDown(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); 91 void OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); 92 void OnDestroy(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); 93 94 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lPCmdLine, int nCmdShow) 95 { 96 MyRegisterClass(hInstance); 97 Create(hInstance,nCmdShow); 98 ShowWindow(hWnd,nCmdShow); 99 UpdateWindow(hWnd); 100 return Run(); 101 } 102 103 ATOM MyRegisterClass(HINSTANCE hInstance) 104 { 105 WNDCLASS wc; 106 wc.style = 0; 107 wc.lpfnWndProc = WndProc; 108 wc.cbClsExtra = 0; 109 wc.cbWndExtra = 0; 110 wc.hInstance = hInstance; 111 wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); 112 wc.hCursor = LoadCursor(NULL,IDC_ARROW); 113 wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 114 wc.lpszMenuName = NULL; 115 wc.lpszClassName = lpszClassName; 116 return RegisterClass(&wc); 117 } 118 119 BOOL Create(HINSTANCE hInstance, int nCmdShow) 120 { 121 hWnd = CreateWindow(lpszClassName,"Windows",WS_OVERLAPPEDWINDOW,400,300,180,160,NULL,NULL,hInstance,NULL); 122 return TRUE; 123 } 124 125 int Run() 126 { 127 while (GetMessage(&msg, NULL, 0, 0)) 128 { 129 TranslateMessage(&msg); 130 DispatchMessage(&msg); 131 } 132 return msg.wParam; 133 } 134 135 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 136 { 137 switch (message) 138 { 139 case WM_LBUTTONDOWN: 140 OnLButtonDown(hWnd,message,wParam,lParam); 141 break; 142 case WM_PAINT: 143 OnPaint(hWnd,message,wParam,lParam); 144 break; 145 default: 146 return DefWindowProc(hWnd,message,wParam,lParam); 147 } 148 return 0; 149 } 150 151 void OnLButtonDown(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 152 { 153 ShowText = "Hello!"; 154 InvalidateRect(hWnd,NULL,1); 155 } 156 157 void OnPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 158 { 159 PAINTSTRUCT ps; 160 HDC hdc; 161 hdc = BeginPaint(hWnd,&ps); 162 TextOut(hdc,50,50,ShowText,6); 163 EndPaint(hWnd,&ps); 164 } 165 166 void OnDestroy(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 167 { 168 PostQuitMessage(0); 169 } 170 InvalidateRect(hWnd,NULL,1); 171 } 172 173 void OnPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 174 { 175 PAINTSTRUCT ps; 176 HDC hdc; 177 hdc = BeginPaint(hWnd,&ps); 178 TextOut(hdc,50,50,ShowText,6); 179 EndPaint(hWnd,&ps); 180 } 181 182 void OnDestroy(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 183 { 184 PostQuitMessage(0); 185 }
以上是关于Windows程序代码重构的主要内容,如果未能解决你的问题,请参考以下文章