win32应用程序框架的两种方式
来源:互联网 发布:欧莱雅买什么 知乎 编辑:程序博客网 时间:2024/05/21 06:33
win32框架最为见的是把主窗口类和初始化窗口以及主窗口回调函数放在同一个文件中,适用于较为小型的软件开发。
先列出最基本的框架,紧接着展示另一种框架,便于比较。个人感觉是从MFC开发中移植过来的,但是程序具有很强的扩展性,适用于二次开发,方便修改。
原框架:
#include <windows.h>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")//窗口风格优化int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){ static TCHAR szAppName[] = TEXT ("HelloWin") ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName, // window class name TEXT ("The Hello Program"), // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL) ; // creation parameters ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ;}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ HDC hdc ; PAINTSTRUCT ps ; RECT rect ; switch (message) { case WM_CREATE: return 0 ; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ;}
进行拆分框架:
//initwindow.cpp 用于创建窗口类和实例
#include "stdafx.h"TCHAR szTitle[]=TEXT("tcpClient");TCHAR szWindowClass[]=TEXT("tcpClient");LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);ATOM MyRegisterClass(PWNDCLASSEX wcex,HINSTANCE hInstance){wcex->cbSize = sizeof(WNDCLASSEX); wcex->style= CS_HREDRAW | CS_VREDRAW;wcex->lpfnWndProc= (WNDPROC)WndProc;wcex->cbClsExtra= 0;wcex->cbWndExtra= 0;wcex->hInstance= hInstance;wcex->hIcon= 0;wcex->hCursor= LoadCursor(NULL, IDC_ARROW);wcex->hbrBackground=(HBRUSH) GetStockObject (WHITE_BRUSH);wcex->lpszMenuName= 0;wcex->lpszClassName= szWindowClass;wcex->hIconSm= 0;return RegisterClassEx(wcex);}HWND InitInstance(HINSTANCE hInstance, int nCmdShow){HWND hWnd;HINSTANCE hInst;hInst = hInstance; // Store instance handle in our global variablehWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, windowx, windowy, NULL, NULL, hInstance, NULL);if (!hWnd){return hWnd;}ShowWindow(hWnd, nCmdShow);UpdateWindow(hWnd);return hWnd;}//windowTool.cpp用于创建窗口中各种控件#include "stdafx.h"HWND hInputEdit,hSendButton,hChatEdit,hLaunchButton,hCloseButton,hIPaddrEdit,hIPaddrStatic;//定义控件句柄为局部变量,通过getDlgItem(hwnd,资源标识符进行调用)void initTool(HWND hWnd){//动态创建各种子控件}//stdafx.h 用于包含全局变量、导入库及申明各cpp文件中的函数#include <windows.h>#include "resource.h"#pragma comment(lib, "ws2_32.lib") // 引入lib库文件#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")//win7窗口风格优化ATOM MyRegisterClass(PWNDCLASSEX,HINSTANCE);extern HWND InitInstance(HINSTANCE, int);//对窗口进行初始化并注册热键extern void initTool(HWND hWnd);#include "stdafx.h"HINSTANCE g_hInstance;int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){g_hInstance = hInstance;HWND hWnd;MSG msg;WNDCLASSEX WndClass;MyRegisterClass(&WndClass, hInstance);if (!(hWnd = InitInstance(hInstance, nCmdShow))) return FALSE;while (GetMessage(&msg, NULL, 0, 0)) //创建消息循环获取机制{TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){HDC hdc ; PAINTSTRUCT ps ; RECT rect ; switch (message) { case WM_CREATE:initTool(); return 0 ; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ;
}//initwindow.cpp 用于创建窗口类和实例
#include "stdafx.h"TCHAR szTitle[]=TEXT("tcpClient");TCHAR szWindowClass[]=TEXT("tcpClient");LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);ATOM MyRegisterClass(PWNDCLASSEX wcex,HINSTANCE hInstance){wcex->cbSize = sizeof(WNDCLASSEX); wcex->style= CS_HREDRAW | CS_VREDRAW;wcex->lpfnWndProc= (WNDPROC)WndProc;wcex->cbClsExtra= 0;wcex->cbWndExtra= 0;wcex->hInstance= hInstance;wcex->hIcon= 0;wcex->hCursor= LoadCursor(NULL, IDC_ARROW);wcex->hbrBackground=(HBRUSH) GetStockObject (WHITE_BRUSH);wcex->lpszMenuName= 0;wcex->lpszClassName= szWindowClass;wcex->hIconSm= 0;return RegisterClassEx(wcex);}HWND InitInstance(HINSTANCE hInstance, int nCmdShow){HWND hWnd;HINSTANCE hInst;hInst = hInstance; // Store instance handle in our global variablehWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, windowx, windowy, NULL, NULL, hInstance, NULL);if (!hWnd){return hWnd;}ShowWindow(hWnd, nCmdShow);UpdateWindow(hWnd);return hWnd;}//windowTool.cpp用于创建窗口中各种控件#include "stdafx.h"HWND hInputEdit,hSendButton,hChatEdit,hLaunchButton,hCloseButton,hIPaddrEdit,hIPaddrStatic;//定义控件句柄为局部变量,通过getDlgItem(hwnd,资源标识符进行调用)void initTool(HWND hWnd){//动态创建各种子控件}//stdafx.h 用于包含全局变量、导入库及申明各cpp文件中的函数#include <windows.h>#include "resource.h"#pragma comment(lib, "ws2_32.lib") // 引入lib库文件#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")//win7窗口风格优化ATOM MyRegisterClass(PWNDCLASSEX,HINSTANCE);extern HWND InitInstance(HINSTANCE, int);//对窗口进行初始化并注册热键extern void initTool(HWND hWnd);
这样就将最基本的win32进行了拆分,有效的提高了程序的扩展性查找起来也很方便,另外需要自定义资源标识符的时候,建议在resource.h头文件中进行定义,便于统一管理和维护。
这是本人的第一篇博客,希望大家多多指教。
- win32应用程序框架的两种方式
- [Win32]Win32窗口应用程序的框架
- ios应用程序的两种启动方式
- 退出应用程序的两种监听方式
- Android更改桌面应用程序launcher的两种方式
- Android更改桌面应用程序launcher的两种方式
- Android更改桌面应用程序launcher的两种方式
- Android 应用程序发送短信的两种方式
- Andoird的两种应用程序界面构建方式
- Android更改桌面应用程序launcher的两种方式
- Android更改桌面应用程序launcher的两种方式
- Android更改桌面应用程序launcher的两种方式
- Android 重启应用程序 的两种方式
- Win32应用程序基本框架详解
- win32应用程序基础框架及位图的显示
- Hibernate框架利用sessionFactory保存数据的两种方式
- XZ_CoreMotion框架获取数据的两种方式
- Spring集成Hibernate框架的两种方式
- java中关于try、catch、finally中的细节分析
- ASP.NET中时间的绑定和格式化
- 智能手机应用去除返回键
- getsockopt/setsockopt 函数说明 .
- 为什么应用商店里搜索不到你的App?
- win32应用程序框架的两种方式
- DM8168集成图像算法
- C#常用控件的属性
- 软件架构师需要知道的97件事
- 无限级别菜单下拉
- native2ascii
- git reset用法 一 重置引用(版本切换)
- WIFI Demo
- 几个检查当前运行的LINUX是在VM还是在实体机中的方法