Win32控制台下消息机制的模拟

#include "stdafx.h" #include "SimulateMsg.h" #include "conio.h" #include <iostream> using namespace std; #define MSG_KEY 0x1224 //有键盘输入 #define MSG_CHAR 0x1225 //有字符输入 #define MSG_QUIT 0x0001 //退出 #define MSG_CLOSE 0x0002 //关闭 #ifdef _DEBUG #define new DEBUG_NEW #endif // 唯一的应用程序对象 CWinApp theApp; //定义消息 struct _MSG { int Message; int Parameter; }; //消息队列 CList<_MSG> _MsgQueue; //消息处理函数 typedef void (*MessageProc) (int Message, int Parameter); MessageProc _MessageHandler; //发送消息 void _PostMessage(int Message, int Parameter) { _MSG NewMsg; NewMsg.Message = Message; NewMsg.Parameter = Parameter; _MsgQueue.AddTail(NewMsg); } //读取消息队列中的新消息 bool _GetMessage(_MSG* pMsg) { while (_MsgQueue.IsEmpty()); _MSG LastMsg = _MsgQueue.RemoveHead(); *pMsg = LastMsg; if (pMsg->Message == MSG_QUIT) return false; return true; } //转换消息 void _TranslateMessage(_MSG* pMsg) { //x键为系统键 if (pMsg->Message == MSG_KEY && pMsg->Parameter == 'x') { pMsg->Message = MSG_CLOSE; return; } //将键盘消息转换成字符消息 if (pMsg->Message == MSG_KEY) { pMsg->Message = MSG_CHAR; } } //分发消息 void _DispatchMessage(_MSG* pMsg) { (*_MessageHandler)(pMsg->Message, pMsg->Parameter); } //消息循环,负责读取消息→分发消息 void MessageLoop(MessageProc handler) { _MessageHandler = handler; _MSG msg; while (_GetMessage(&msg)) { _TranslateMessage(&msg); _DispatchMessage(&msg); } } //对MSG_CHAR的相应 void OnChar(int charInput) { if (charInput == 'q') { cout << "byebye!"<< endl; _PostMessage(MSG_QUIT, 0); } cout << "you typed "<< (char)charInput << endl; } //对MSG_CLOSE的相应 void OnClose(int charInput) { cout << "bye bye!"<< endl; _PostMessage(MSG_QUIT, 0); } void MyMessageProc(int Message, int Parameter) { switch (Message) { case MSG_CHAR: OnChar(Parameter); break; case MSG_CLOSE: OnClose(Parameter); break; } } //消息发生器 UINT GatherMessages(LPVOID pParam) { while(*((bool*)pParam)) { //键盘输入 if (_kbhit()) { int ch = _getch(); _PostMessage(MSG_KEY, ch); } } return 0; } int main(void) { bool flag = true; //打开消息发生器 AfxBeginThread(GatherMessages, &flag); //开始消息循环处理 MessageLoop(MyMessageProc); flag = false; //AfxEndThread(0); return 0; } 

你可能感兴趣的:(struct,include)