模仿WIN32程序处理消息
来源:互联网 发布:saas软件美国公司 编辑:程序博客网 时间:2024/06/03 22:08
#include "stdafx.h"#include "MyMessage.h"#include <conio.h>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_KEY0X1224//有键盘输入#define MSG_CHAR0X1225//有字符输入#define MSG_QUIT0X0001//退出#define MSG_CLOSE0X0002//关闭//定义消息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<<endl;}//对MSG_CLOSE的响应void OnClose(int charInput){_PostMessage(MSG_QUIT,0);}//相似于WIN32编程里的消息处理回调函数void MyMessageProc(int msg,int param){switch (msg){case MSG_CHAR:{OnChar(param);break;}case MSG_CLOSE:{OnClose(param);break;}}}//消息发生器UINT GatherMessage(LPVOID pParam){while (*(bool*)pParam){//键盘输入if (_kbhit()){int ch = _getch();_PostMessage(MSG_KEY,ch);}}return 0;}int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){bool flag = true;//打开消息发生器AfxBeginThread(GatherMessage,&flag);//开始消息处理循环MessageLoop(/*MyMessageProc*/);flag = false;AfxEndThread(0);return 0;}
流程图如下
0 0
- 模仿WIN32程序处理消息
- win32控件消息处理
- win32控件消息处理
- Win32消息处理API---GetMessage
- Win32消息处理API---PeekMessage
- Win32消息处理API---WaitMessage
- Win32消息处理API---SendMessage
- Win32消息处理API---PostMessage
- Win32消息处理API---PeekMessage
- Win32消息处理API---PeekMessage
- Win32消息处理API---PostMessage
- [Win32]Windows消息处理机制
- Win32消息处理API---GetMessage
- win32(3)--消息处理机制
- Win32事件消息处理函数
- Win32程序的空闲处理
- WIN32汇编: 6.处理键盘消息
- WIN32汇编: 7.处理鼠标输入消息
- 字符编码认识
- Leetcode-2D Dynamic Programming
- C++新特性:函数模板
- MySQL启程学习回忆录3
- oracle最强大函数之一decode函数的使用
- 模仿WIN32程序处理消息
- 预处理:条件编译
- 重磅!支付宝小程序悄然上线,分享能力更强大!
- bootstrap的按钮
- C语言学习入门--基础数据类型(二)
- 接口
- Struts2标签学习:hidden标签
- linux中实现文件的复制(c代码实现)
- Linux--RH134---unit 11 系统恢复技术