Douglas Boling的Windows程序开发模板
在Windows CE6的开发中,Douglas Boling所展现的更新后的开发架构,个人认为确实不错。在这里Share一下,希望能够对于大家的开发有所帮助。
Windows典型的SDK风格,在窗口过程函数中有一个巨大的Switch语句用来分析各种消息并进行处理。如果消息的处理代码比较多的话,那么整个窗口过程函数会变的比较混乱。 Douglas将窗口过程分解,对于每个消息构建单独的函数来进行处理,更易于理解和管理。
首先在头文件中呢可以定义一个消息和处理函数映射的结构体:
![](/assets/blank.gif)
![](/assets/blank.gif)
// Generic defines and data types
//
struct decodeUINT { // Structure associates
UINT Code; // messages
// with a function.
LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);
};
那么我们就可以在CPP文件中对于消息和处理函数的具体对应进行定义了,如下定义了WM_PAINT和WM_DESTROY和对应的处理函数
const struct decodeUINT MainMessages[] = {
WM_PAINT, DoPaintMain,
WM_DESTROY, DoDestroyMain,
};
在窗口过程中可以用一个For循环来遍历消息并调用对应的消息响应函数进行处理, 对于没有处理的消息,则直接调用系统的默认处理函数:
![](/assets/blank.gif)
![](/assets/blank.gif)
LPARAM lParam) {
INT i;
//
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
附上完整的模板文件,仅供参考!
Win32Template.h
![](/assets/blank.gif)
![](/assets/blank.gif)
// Header file
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 Douglas Boling
//================================================================
// Returns number of elements
#define dim(x) (sizeof(x) / sizeof(x[0]))
//----------------------------------------------------------------------
// Generic defines and data types
//
struct decodeUINT { // Structure associates
UINT Code; // messages
// with a function.
LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);
};
struct decodeCMD { // Structure associates
UINT Code; // menu IDs with a
LRESULT (*Fxn)(HWND, WORD, HWND, WORD); // function
};
//----------------------------------------------------------------------
// Function prototypes
//
HWND InitInstance (HINSTANCE, LPWSTR, int);
int TermInstance (HINSTANCE, int);
// Window procedures
LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);
// Message handlers
LRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);
Win32Template.cpp
![](/assets/blank.gif)
![](/assets/blank.gif)
// HelloCE - A simple application for Windows CE
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 Douglas Boling
//======================================================================
#include <windows.h> // For all that Windows stuff
#include "Win32Template.h" // Program-specific stuff
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT("HelloCE");
HINSTANCE hInst; // Program instance handle
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_PAINT, DoPaintMain,
WM_DESTROY, DoDestroyMain,
};
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
MSG msg;
int rc = 0;
HWND hwndMain;
// Initialize this instance.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0) return 0x10;
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {
WNDCLASS wc;
HWND hWnd;
// Save program instance handle in global variable.
hInst = hInstance;
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
// If Windows Mobile, only allow one instance of the application
hWnd = FindWindow (szAppName, NULL);
if (hWnd) {
SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
return 0;
}
#endif
// Register application main window class.
wc.style = 0; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL, // Application icon
wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 0;
// Create main window.
hWnd = CreateWindow (szAppName, // Window class
TEXT("HelloCE"), // Window title
// Style flags
WS_VISIBLE | WS_CAPTION | WS_SYSMENU,
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // Application instance
NULL); // Pointer to create
// parameters
if (!IsWindow (hWnd)) return 0; // Fail code if not created.
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
return nDefRC;
}
//======================================================================
// Message handling procedures for main window
//
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
INT i;
//
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PAINTSTRUCT ps;
RECT rect;
HDC hdc;
// Get the size of the client rectangle
GetClientRect (hWnd, &rect);
hdc = BeginPaint (hWnd, &ps);
DrawText (hdc, TEXT ("Hello Windows CE!"), -1, &rect,
DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint (hWnd, &ps);
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}
转载于:https://www.cnblogs.com/Jiansong/archive/2010/02/05/1664498.html
Douglas Boling的Windows程序开发模板相关推荐
- 小程序开发模板设计怎么做?
如何设计小程序开发模板?小程序页面的设计理念.色彩.布局等都要精心策划,但大部分想做小程序的商家都是新手,不懂设计知识.那么小程序开发模板设计怎么做呢? 一.采用优质素材 当小程序开发模板设计.广 ...
- C++——Windows 程序开发
开发 Windows API的Windows程序,需要编写两个函数.一个是Winmain()函数,程序的执行从这里开始,基本的初始化工作也在这里完成.另一个是WindowProc()函数,该函数由Wi ...
- Windows程序开发——指挥官夏尔对于Windows程序开发框架的选择
1 框架选择 GUI框架: 感觉Electron还是挺好的,我们也使用Electron来进行开发: C++编译器: Mingw-w64编译器(相当于Windows平台上的GCC) (我觉得VSCode ...
- 小程序开发——模板与配置
一.WXML 模板语法 1.数据绑定的基本原则 ① 在 data 中定义数据 ② 在 WXML 中使用数据 2.在 data 中定义页面的数据 在页面对应的 .js 文件中,把数据定义到 data 对 ...
- 02-微信小程序开发-模板与配置
一.WXML 模板语法 1.1.数据绑定 1. 数据绑定的基本原则 ① 在 data 中定义数据 ② 在 WXML 中使用数据 // pages/list/list.js Page({/*** 页面的 ...
- 微信小程序开发-模板与配置
- 微信小程序开发代理展示销售网站源码织梦模板(手机同步)
本套织梦模板采用织梦最新内核开发的模板,这款模板使用范围极广,不仅仅局限于一类型的企业,小程序网站.微信小程序开发类的网站都可以用该模板. 你只需要把图片和文章内容换成你的即可,颜色都可以修改,改完让 ...
- arm-linux 程序开发入门(QT窗口应用程序、编码、交叉编译、调试)(三机器和双机器搭建方法)(笔记)
Linux及Arm-Linux程序开发笔记(零基础入门篇) 文章目录 前言 一.Arm-Linux程序开发平台简要介绍 1.1程序开发所需系统及开发语言 1.2系统平台搭建方式 二.Linux开发平台 ...
- 微信小程序开发的快速步骤方法和最大的坑
1.在mp.weixin.qq.com 这里申请 2.下载开发工具 3.前端开发:搜集小程序开发模板,或者自己写小东西.(跟前端差不多,很容易上手) 4.关于后端坑的记录 以为与后端的联调需要申请域名 ...
最新文章
- as工程放到源码编译_UE4 Program 类型工程的限制和解决方法
- python json模块有什么用_Python的json模块及使用
- SCRUM敏捷开发官方权威指南
- boost::detail模块实现宏IS_SORTED的测试程序
- mysql 事件计划区别_MySQL 计划事件
- [Jarvis OJ - PWN]——Smashes
- BZOJ2809-左偏树合并
- flask框架+mysql数据库并与前台数据交互
- 备份数据库的expdp语句_【ORACLE语句备份】数据库表同步 ——定时任务管理器(EXPDP导出,IMPDP导入)...
- WEB接口测试之Jmeter接口测试自动化 (三)(数据驱动测试) 接口测试与数据驱动...
- Qfile与QTextStream读写文本文件
- linux pandas教程_Python Anaconda教程–了解最受欢迎的数据科学平台
- JS中style属性
- linux下的PDF阅读器
- jmeter json提取器和正则表达式提取器
- LoadRunner11 压力测试
- case …when… 与纵表转横表
- 北风:二类电商“空手套白狼”的赚钱套路
- 将一个文件夹下的多个目录生成txt文本,并且写入到Excel中
- 跨境知道快讯:亚马逊推出“Buy with Prime”服务
热门文章
- Unity UI -- (7) 创建世界空间UI
- Flink(1):Flink概述
- 电力系统IEEE33节点Simulink仿真研究(Matlab实现)
- 淘淘商城第50讲——导入商品数据到索引库时,报错:org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException
- win7系统盘扩容后不识别修复方法
- Wi-Fi漫游的工作原理
- Android最新敲诈者病毒分析及解锁
- 屏蔽流氓软件弹窗广告
- 解决springboot 循环依赖
- 数据库原理与应用第三版何玉洁第七章课后习题答案