模仿WIN32程序处理消息
发布日期:2021-06-30 12:12:18 浏览次数:3 分类:技术文章

本文共 1519 字,大约阅读时间需要 5 分钟。

#include "stdafx.h"#include "MyMessage.h"#include 
using namespace std;#ifdef _DEBUG#define new DEBUG_NEW#define DBUG_LOG(st) printf("%s\r\n",st);#else#define DBUG_LOG(st) #endif#define MSG_KEY 0X1224 //有键盘输入#define MSG_CHAR 0X1225 //有字符输入#define MSG_QUIT 0X0001 //退出#define MSG_CLOSE 0X0002 //关闭//定义消息struct _MSG{ int msg; //消息 int param; //消息参数};//消息队列CList<_MSG> _MsgQueue;//消息处理函数typedef void (*MessageProc)(int msg,int param);MessageProc _MessageHandler;void MyMessageProc(int msg,int param);//发送消息---投递消息void _PostMessage(int msg,int param){ _MSG NewMsg; NewMsg.msg = msg; NewMsg.param = param; _MsgQueue.AddTail(NewMsg);}//读取消息队列中的新消息bool _GetMessage(_MSG* pMsg){ while (_MsgQueue.IsEmpty()); _MSG LastMsg = _MsgQueue.RemoveHead(); *pMsg = LastMsg; if (pMsg->msg == MSG_QUIT) { return false; } return true;}//转换消息void _TranslateMessage(_MSG *pMsg){ //x键为系统键 if (pMsg->msg == MSG_KEY && pMsg->param == _T('x')) { pMsg->msg = MSG_CLOSE; return; } //将键盘消息转换成字符消息 if (pMsg->msg == MSG_KEY) { pMsg->msg = MSG_CHAR; }}//分发消息void _DispatchMessage(_MSG *pMsg){ _MessageHandler = MyMessageProc; //直接调用消息处理函数---MyMessageProc (*_MessageHandler)(pMsg->msg,pMsg->param);}//消息大循环,负责读取消息-》分发消息void MessageLoop(/*MessageProc handler*/){ //_MessageHandler = handler; _MSG msg; while (_GetMessage(&msg)) { _TranslateMessage(&msg); _DispatchMessage(&msg); }}//对MSG_CHAR的响应void OnChar(int charInput){ DBUG_LOG("进入OnChar函数") if (charInput == _T('q')) { _PostMessage(MSG_QUIT,0); } cout<<"输入:"<<(char)charInput<

流程图如下

转载地址:https://jadeshu.blog.csdn.net/article/details/71157045 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:重载消息进行界面自绘
下一篇:字符通用函数和单字节多字节版本

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月29日 04时52分07秒