Douglas Boling的Windows程序开发模板

在Windows CE6的开发中,Douglas Boling所展现的更新后的开发架构,个人认为确实不错。在这里Share一下,希望能够对于大家的开发有所帮助。

Windows典型的SDK风格,在窗口过程函数中有一个巨大的Switch语句用来分析各种消息并进行处理。如果消息的处理代码比较多的话,那么整个窗口过程函数会变的比较混乱。 Douglas将窗口过程分解,对于每个消息构建单独的函数来进行处理,更易于理解和管理。

首先在头文件中呢可以定义一个消息和处理函数映射的结构体:

ExpandedBlockStart.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和对应的处理函数

 

// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
    WM_PAINT, DoPaintMain,
    WM_DESTROY, DoDestroyMain,
};

 

在窗口过程中可以用一个For循环来遍历消息并调用对应的消息响应函数进行处理, 对于没有处理的消息,则直接调用系统的默认处理函数:

ExpandedBlockStart.gif代码
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);
}

 

附上完整的模板文件,仅供参考!

Win32Template.h

ExpandedBlockStart.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

 

ExpandedBlockStart.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 == 0return 0x10;

    
// Application message loop
    while (GetMessage (&msg, NULL, 00)) {
        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) == 0return 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

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值