当前位置:首页 > 嵌入式 > 嵌入式软件
[导读]这样使添加处理消息函数变得简单步骤1: 头文件中添加相关项步骤2: cpp添加具体实现方法步骤3: 结构体数组中添加相关项.h 文件:struct DecodeUint{UINT meesage;LONG (*

这样使添加处理消息函数变得简单

步骤1: 头文件中添加相关项

步骤2: cpp添加具体实现方法

步骤3: 结构体数组中添加相关项

.h 文件:

struct DecodeUint{

UINT meesage;

LONG (*fun)(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

};

#define dim(x) (sizeof(x)/sizeof(x[0]))

LONG OnDestory(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

LONG OnPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

.c文件:

#include

#include "Border.h"

struct DecodeUint MainMessageProc[] = {

WM_DESTROY,OnDestory,

WM_PAINT,OnPaint,

};

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPTSTR lpCmdLine,

int nCmdShow)

{

WNDCLASS wc;

wc.cbClsExtra = 0;

wc.cbWndExtra = 0;

wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);

wc.hCursor = NULL;

wc.hIcon = NULL;

wc.hInstance = hInstance;

wc.lpfnWndProc = WndProc;

wc.lpszClassName = TEXT("hello");

wc.lpszMenuName = NULL;

wc.style = CS_HREDRAW | CS_VREDRAW;

if (!RegisterClass(&wc)) return -1;

HWND hWnd = CreateWindow(TEXT("hello"),TEXT("helloworld"), WS_VISIBLE | WS_BORDER | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_CAPTION,

10,10,320,240,NULL,NULL,hInstance,NULL);

UpdateWindow(hWnd);

ShowWindow(hWnd,nCmdShow);

MSG msg;

while (GetMessage(&msg, NULL, 0, 0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

return 1;

}

LONG OnDestory(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

PostQuitMessage(1);

return TRUE;

}

LONG OnPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

PAINTSTRUCT ps;

HDC hdc;

RECT rect;

GetClientRect(hWnd,&rect);

hdc = BeginPaint(hWnd,&ps);

DrawText(hdc,TEXT("hello world"),-1,&rect,DT_VCENTER | DT_CENTER);

EndPaint(hWnd,&ps);

return TRUE;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

for (int i = 0;i < dim(MainMessageProc); i++)

{

if (MainMessageProc[i].meesage == message)

{

(*MainMessageProc[i].fun)(hWnd,message,wParam,lParam);

}

}

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

}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭