简单的消息映射表

模范MFC的消息映射表基础,建立一个针对不用命令动作的消息模型
当然,这个只是最基本的应用,真正MFC的映射表比这可复杂多了...
但是,以此为基础就能开发出适用自己的消息模型,希望对大家有用

===================================================

#include <iostream>

class MyClass{
public:
 // 定义一张结构表 表示命令与对应的处理函数
 struct iCommandMap{
  // 命令
  int command;
  // 处理命令的函数指针
  void (MyClass::*pfn)(int param);
 };

 // 定义一个2个命令的表
 const static iCommandMap iCmd[2];


 // 对应命令的处理函数
 void OnVoid1(int param) { std::cout << "I am void1 : param is " << param; }
 void OnVoid2(int param) { std::cout << "I am void2 : param is " << param; }
};

// 将命令与处理函数存入结构数组中
const MyClass::iCommandMap MyClass::iCmd[] = {
 0x01,  OnVoid1,
 0x02,  OnVoid2,
};

// 简化计算数组长度的宏
#define dim(x) (sizeof(x) / sizeof(x[0]))

 

int _tmain(int argc, _TCHAR* argv[])
{
 // 声明对象
 MyClass mc;

 // 命令
 int command = 0x01;  //改成 2 试试?
 int param = 1234; //传入函数的参数

 // 遍历命令表 调用与命令对应的函数指针
 for(int i = 0; i < dim(mc.iCmd); i ++){
  if(mc.iCmd[i].command == command)
   (mc.*MyClass::iCmd[i].pfn)(param);
 }

 return 0;

消息映射表的问题

07-27

Win32 App里面输入以下代码rnrn#include rnclass CMyWnd:public CFrameWndrnrnprivate:rn char* ShowText;rnpublic:rn afx_msg void OnPaint();rn afx_msg void OnLButtonDown();rn DECLARE_MESSAGE_MAP()rn;rnrnBEGIN_MESSAGE_MAP(CMyWnd,CFrameWnd)rn ON_WM_PAINT()rn ON_WM_LBUTTONDOWN()rnEND_MESSAGE_MAP()rnrnvoid CMyWnd::OnPaint()rnrn CPaintDC dc(this);rn dc.TextOut(20,20,ShowText);rnrnrnvoid CMyWnd::OnLButtonDown()rnrn ShowText="Message_mapped Program";rn InvalidateRect(Null,true);rnrnrnclass CMyApp:public CWinApprnrnpublic:rn BOOL InitInstance();rn;rnrnBOOL CMyApp::InitInstance()rnrn CMyWnd* pMainWnd=new CMyWnd;rn pMainWnd->Create(0,"MFC");rn pMainWnd->ShowWindow(m_nCmdShow);rn pMainWnd->UpdateWindow();rn m_pMainWnd=pMainWnd;rn return true;rnrnrnCMyApp MyApp;rnrn编译之前选择Project|settings菜单,静态实用MFC类库rnrn这是一个书本上的原题,编译之后出现如下错误:rnrnCompiling...rnmain.cpprnE:\MFC\MFC_Eg0205\main.cpp(10) : error C2018: unknown character '0xa3'rnE:\MFC\MFC_Eg0205\main.cpp(10) : error C2018: unknown character '0xbb'rnE:\MFC\MFC_Eg0205\main.cpp(12) : error C2143: syntax error : missing ';' before '*'rnE:\MFC\MFC_Eg0205\main.cpp(12) : error C2501: 'GetMessageMap' : missing storage-class or type specifiersrnE:\MFC\MFC_Eg0205\main.cpp(12) : error C2556: 'int *__thiscall CMyWnd::GetMessageMap(void) const' : overloaded function differs only by return type from 'const struct AFX_MSGMAP *__thiscall CMyWnd::GetMessageMap(void) const'rn E:\MFC\MFC_Eg0205\main.cpp(9) : see declaration of 'GetMessageMap'rnE:\MFC\MFC_Eg0205\main.cpp(12) : error C2373: 'GetMessageMap' : redefinition; different type modifiersrn E:\MFC\MFC_Eg0205\main.cpp(9) : see declaration of 'GetMessageMap'rnE:\MFC\MFC_Eg0205\main.cpp(12) : error C2143: syntax error : missing ';' before 'tag::id'rnE:\MFC\MFC_Eg0205\main.cpp(12) : error C2734: 'AFX_MSGMAP' : const object must be initialized if not externrnE:\MFC\MFC_Eg0205\main.cpp(12) : error C2371: 'AFX_MSGMAP' : redefinition; different basic typesrn E:\MFC\MFC_Eg0205\main.cpp(12) : see declaration of 'AFX_MSGMAP'rnE:\MFC\MFC_Eg0205\main.cpp(12) : fatal error C1004: unexpected end of file foundrnError executing cl.exe.rnrnmain.obj - 10 error(s), 0 warning(s)rnrn请帮忙看一看是不是缺少了什么?

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试