您的位置:首页 > 其它

RTTI 与消息机制(VC_MFC)

2012-11-17 03:47 537 查看

目录

RTTI 运行时类型识别
MFC消息机制

消息路由



(本章节中例子都是用 VS2005 编译调试的)

RTTI 运行时类型识别(内容源自深入浅出MFC ,相关宏的知识点链接)

类的"类型识别录" CRuntimeClass 类:

在介绍动态识别技术时候首先要介绍一个结构,用于记录类的信息.(在 afx.h 头文件中定义)其成员定义与解释如下(MSDN参考链接)

Public Methods                                           
Name                  Description                         
CRuntimeClass::CreateObject      Creates an object during run time.
CRuntimeClass::FromName      Creates an object during run time using the familiar class name.
CRuntimeClass::IsDerivedFrom    Determines if the class is derived from the specified class.

Public Data Members                                        
Name                 Description                        
CRuntimeClass::m_lpszClassName   The name of the class.
CRuntimeClass::m_nObjectSize      The size of the object in bytes.
CRuntimeClass::m_pBaseClass      A pointer to the CRuntimeClass structure of the base class.
CRuntimeClass::m_pfnCreateObject   A pointer to the function that dynamically creates the object.
CRuntimeClass::m_pfnGetBaseClass   Returns the CRuntimeClass structure (only available when dynamically linked).
CRuntimeClass::m_wSchema      The schema number of the class.
CRuntimeClass::m_pClassInit      暂时不用管它(MSDN中没有声明,这个是在 CRuntimeClass 类定义中看到的所以蛮写进来)

这样我们就可以通过在类中添加这个类的静态对象(即 CRuntimeClass 成员变量)记录的信息来完成"类型识别录",然后在开辟一个全局的 CRuntime 指针指向这个"类型识别录"的尾部,如下图所示:

View Code

//childView.h
...
class CChildView : public CWnd
{
...
protected:
afx_msg void OnPaint();
protected:
static const AFX_MSGMAP* PASCAL GetThisMessageMap();
virtual const AFX_MSGMAP* GetMessageMap() const;
};

// ChildView.cpp
...
__pragma(warning( push )) \
__pragma(warning( disable : 4867 ))
const AFX_MSGMAP* CChildView::GetMessageMap() const
{ return GetThisMessageMap(); }
const AFX_MSGMAP* PASCAL CChildView::GetThisMessageMap()
{
typedef CChildView ThisClass;
typedef CWnd TheBaseClass;
static const AFX_MSGMAP_ENTRY _messageEntries[] =
{
    {WM_HELP, 0, 0, 0, AfxSig_bHELPINFO,(AFX_PMSG)(AFX_PMSGW)(static_cast< BOOL (AFX_MSG_CALL CWnd::*)(HELPINFO*) > ( &ThisClass :: OnHelpInfo))},
    {0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 }
};
static const AFX_MSGMAP messageMap =
{ &TheBaseClass::GetThisMessageMap, &_messageEntries[0] };
return &messageMap;
}
__pragma(warning( pop ))
....
void CChildView::OnPaint()
{
CPaintDC dc(this); // 用于绘制的设备上下文

// TODO: 在此处添加消息处理程序代码

// 不要为绘制消息而调用 CWnd::OnPaint()
}


消息结构如下图所示



MFC自定义消息:

第一步:定义消息(推荐用户自定义消息至少是WM_USER+100,因为很多新控件也要使用WM_USER消息).

#define WM_MY_MESSAGE (WM_USER+100)


第二步:声明成员函数(在类头文件的AFX_MSG块中说明消息处理函数):

afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);


第三步:实现成员函数(实现处理消息的成员函数,该函数使用WPRAM和LPARAM参数并返回LPESULT).

LRESULT CMainFrame::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
// TODO: 处理用户自定义消息
...
return 0;
}


第四步:添加消息映射(在用户类的消息块中,使用ON_MESSAGE宏指令将消息映射到消息处理函数中).

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
...
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MY_MESSAGE ,OnMyMessage)
END_MESSAGE_MAP()


消息路由(内容源自深入浅出MFC)

Windows消息分类



消息路由路径

从类对消息的传递看消息路由

一般的消息路由                                                 



WM_COMMAND 消息路由                                            



从函数调用看消息路由



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: