写个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 )
{
}
这个博客展示了如何使用C语言封装消息处理和窗口程序。通过定义EventArg和Event结构体,将消息ID与处理函数对应起来,简化了消息的响应流程。在主程序中,创建窗口并进入消息循环,根据消息类型调用相应的处理函数,如onDestroy、onCommand等,实现了窗口的关闭、命令响应等功能。
5447

被折叠的 条评论
为什么被折叠?



