WindowPro消息处理函数的封装,测试代码

写个DEMO,总是没有顺手的代码模板,在这里写个自己能用到的代码。

简单封装消息的处理,用于写窗口程序时用。

C语言格式,把消息及相对应的函数指针存放在数组中。

// XFClass.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "XFClass.h"

// Global Variables:
HINSTANCE hInstance;								// current instance
TCHAR szWindowClass[] =_T("XFWindow");			// the main window class name

// Forward declarations of functions included in this code module:

BOOL				InitInstance(HINSTANCE   );
LRESULT CALLBACK	WindowProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
HWND NewWindow(LPCTSTR lpszTitle);
int MessageLoop() ; 
#define SIZEOF_ARRAY(x) ((sizeof(x))/(sizeof(x[0])))

typedef struct EventArg
{
    HWND hWnd;
    UINT uMsg; 
    WPARAM wParam; 
    LPARAM lParam ; 
    LRESULT lResult;
    BOOL    bHanded;
}EventArg;

typedef void (*Function)(HWND sender,EventArg* e) ; 

 

typedef struct Event
{
    UINT id;
    Function fun;
}Event;


void onDestroy(HWND sender,EventArg*e ); 
void onCommand(HWND sender,EventArg*e ); 
void onSize(HWND sender,EventArg*e ); 
void onButtonOk(HWND sender,EventArg*e ); 
void onButtonExit(HWND sender,EventArg*e ); 
void onButtonAbout(HWND sender,EventArg*e ); 

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{ 
    HWND    hMain =NULL;  
    int     iResult = 0; 


	if (!InitInstance (hInstance )){return FALSE;}

    hMain = NewWindow(_T("XFWindow"));

    iResult = MessageLoop();

    return iResult  ; 

}
int MessageLoop() 
{
    MSG msg; 
    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_XFCLASS));

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

 
HWND NewWindow(LPCTSTR lpszTitle )
{
    HWND hWnd;
     
    hWnd = CreateWindow(szWindowClass, lpszTitle, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, 500 ,330 , NULL, NULL, hInstance, NULL);

    if (!hWnd)
    {
        return NULL;
    }

    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

    return hWnd;
}


BOOL InitInstance(HINSTANCE hInstance   )
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style			= CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc	= WindowProc;
    wcex.cbClsExtra		= 0;
    wcex.cbWndExtra		= 0;
    wcex.hInstance		= hInstance;
    wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_XFCLASS));
    wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground	= (HBRUSH)GetSysColorBrush(COLOR_BTNFACE);
    wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_XFCLASS);
    wcex.lpszClassName	= szWindowClass;
    wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    RegisterClassEx(&wcex);
     

   return TRUE;
}
 
//消息函数

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    Event eMap[] ={{WM_COMMAND,onCommand}, {WM_DESTROY,onDestroy},{WM_SIZE,onSize} };
	Event cmdMap[] = {{IDOK,onButtonOk},{IDM_ABOUT,onButtonAbout},{IDM_EXIT,onButtonExit}};

    int iSize       = SIZEOF_ARRAY(eMap) ; 
    int iSizeCmd    = SIZEOF_ARRAY(cmdMap) ; 
    int i = 0;
     
    
    for(i = 0;i<iSize;i++)
    {        
        if(eMap[i].id == message)
        {
            EventArg e = {hWnd,message,wParam,lParam,0,0};
            (*eMap[i].fun)(hWnd,&e); //调用函数

            if(e.bHanded) {return e.lResult;} //判断返回值
        }
    } 

    if(message == WM_COMMAND)
    {
        for(i = 0;i<iSizeCmd;i++)
        {
            if(cmdMap[i].id == LOWORD(wParam))
            {
                EventArg e = {hWnd,message,wParam,lParam,0,0};
                (*cmdMap[i].fun)(hWnd,&e); //调用函数
                if(e.bHanded) {return e.lResult;} //判断返回值
            }
        }

    }
	return DefWindowProc(hWnd,message,wParam,lParam);
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
 


void onDestroy(HWND sender,EventArg*e )
{
     PostQuitMessage(0);
}
 

void onSize(HWND sender,EventArg*e )
{
    TCHAR szText[100];
    RECT rc; 
    GetWindowRect(sender,&rc);
    wsprintf(szText,_T("width:%d height:%d"),rc.right - rc.left,rc.bottom - rc.top );

    SetWindowText(sender,szText);

}


void onButtonOk(HWND sender,EventArg*e )
{
    DestroyWindow(e->hWnd);
}
void onButtonExit(HWND sender,EventArg*e )
{
    DestroyWindow(e->hWnd);
}
void onButtonAbout(HWND sender,EventArg*e )
{
    DialogBox(hInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), e->hWnd, About);
}

void onCommand(HWND sender,EventArg*e )
{

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白(litebai.com)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值