【MFC】MFC消息响应机制|消息映射表|MFC程序框架/VC 程序运行框架/VC运行顺序 从哪运行 |VC程序快捷键的添加...

目录

MFC消息响应机制分析 

MFC消息响应机制分析http://www.cnblogs.com/dsky/archive/2012/05/28/2520853.html

【MFC 快捷键】VC程序快捷键的添加

【MFC 程序框架】MFC程序框架/VC 程序运行框架/VC运行顺序 从哪运行 


MFC消息响应机制分析 

MFC消息响应机制分析
icon-default.png?t=M666http://www.cnblogs.com/dsky/archive/2012/05/28/2520853.html

---- MFC是Windows下程序设计的最流行的一个类库,但是该类库比较庞杂,尤其是它的消息映射机制,更是涉及到很多低层的东西,我们在这里,对它的整个消息映射机制进行了系统的分析,可以帮助程序开发人员对MFC的消息映射机制有一个比较透彻的了解。

1.引言

---- VC++的MFC类库实际上是Windows下C++编程的一套最为流行的类库。MFC的框架结构大大方便了程序员的编程工作,但是为了更加有效、灵活的使用MFC编程,了解MFC的体系结构往往可以使编程工作事半功倍。它合理的封装了WIN32 API函数,并设计了一套方便的消息映射机制。但这套机制本身比较庞大和复杂,对它的分析和了解无疑有助于我们写出更为合理的高效的程序。这里我们简单的分析MFC的消息响应机制,以了解MFC是如何对Windows的消息加以封装,方便用户的开发。

2. SDK下的消息机制实现
---- 这里简单的回顾一下SDK下我们是如何进行Windows的程序开发的。一般来说,Windows的消息都是和线程相对应的。即Windows会把消息发送给和该消息相对应的线程。在SDK的模式下,程序是通过GetMessage函数从和某个线程相对应的消息队列里面把消息取出来并放到一个特殊的结构里面,一个消息的结构是一个如下的STRUCTURE。 
typedef struct tagMSG {
HWND hwnd;
UINT message; 
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt; 
}MSG;
---- 其中hwnd表示和窗口过程相关的窗口的句柄,message表示消息的ID号,wParam和lParam表示和消息相关的参数,time表示消息发送的时间,pt表示消息发送时的鼠标的位置。

---- 然后TranslateMessage函数用来把虚键消息翻译成字符消息并放到响应的消息队列里面,最后DispatchMessage函数把消息分发到相关的窗口过程。然后窗口过程根据消息的类型对不同的消息进行相关的处理。在SDK编程过程中,用户需要在窗口过程中分析消息的类型和跟消息一起的参数的含义,做不同的处理,相对比较麻烦,而MFC把消息调用的过程给封装起来,使用户能够通过ClassWizard方便的使用和处理Windows的各种消息。

3.MFC的消息实现机制
---- 我们可以看到,在MFC的框架结构下,可以进行消息处理的类的头文件里面都会含有DECLARE_MESSAGE_MAP()宏,这里主要进行消息映射和消息处理函数的声明。可以进行消息处理的类的实现文件里一般都含有如下的结构。 
BEGIN_MESSAGE_MAP(CInheritClass, CBaseClass)
//{{AFX_MSG_MAP(CInheritClass)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

---- 这里主要进行消息映射的实现和消息处理函数的实现。 
---- 所有能够进行消息处理的类都是基于CCmdTarget类的,也就是说CCmdTarget类是所有可以进行消息处理类的父类。CCmdTarget类是MFC处理命令消息的基础和核心。

---- 同时MFC定义了下面的两个主要结构:

AFX_MSGMAP_ENTRY
struct AFX_MSGMAP_ENTRY
{
UINT nMessage; // windows message
UINT nCode; // control code or WM_NOTIFY code
UINT nID; 
// control ID (or 0 for windows messages)
UINT nLastID; 
// used for entries specifying a range of control id's
UINT nSig; 
// signature type (action) or pointer to message #
AFX_PMSG pfn; // routine to call (or special value)
};
和AFX_MSGMAP
struct AFX_MSGMAP
{
#ifdef _AFXDLL
const AFX_MSGMAP* (PASCAL* pfnGetBaseMap)();
#else
const AFX_MSGMAP* pBaseMap;
#endif
const AFX_MSGMAP_ENTRY* lpEntries;
};

其中AFX_MSGMAP_ENTRY结构包含了
一个消息的所有相关信息,其中

nMessage为Windows消息的ID号
nCode为控制消息的通知码
nID为Windows控制消息的ID
nLastID表示如果是一个指定范围的消息被映射的话,
nLastID用来表示它的范围。
nSig表示消息的动作标识
AFX_PMSG pfn 它实际上是一个指向和该消息相应的执行函数的指针。

---- 而AFX_MSGMAP主要作用是两个,一:用来得到基类的消息映射入口地址。二:得到本身的消息映射入口地址。

---- 实际上,MFC把所有的消息一条条填入到AFX_MSGMAP_ENTRY结构中去,形成一个数组,该数组存放了所有的消息和与它们相关的参数。同时通过AFX_MSGMAP能得到该数组的首地址,同时得到基类的消息映射入口地址,这是为了当本身对该消息不响应的时候,就调用其基类的消息响应。

---- 现在我们来分析MFC是如何让窗口过程来处理消息的,实际上所有MFC的窗口类都通过钩子函数_AfxCbtFilterHook截获消息,并且在钩子函数_AfxCbtFilterHook中把窗口过程设定为AfxWndProc。原来的窗口过程保存在成员变量m_pfnSuper中。

---- 所以在MFC框架下,一般一个消息的处理过程是这样的。

函数AfxWndProc接收Windows操作系统发送的消息。

函数AfxWndProc调用函数AfxCallWndProc进行消息处理,这里一个进步是把对句柄的操作转换成对CWnd对象的操作。

函数AfxCallWndProc调用CWnd类的方法WindowProc进行消息处理。注意AfxWndProc和AfxCallWndProc都是AFX的API函数。而WindowProc已经是CWnd的一个方法。所以可以注意到在WindowProc中已经没有关于句柄或者是CWnd的参数了。

方法WindowProc调用方法OnWndMsg进行正式的消息处理,即把消息派送到相关的方法中去处理。消息是如何派送的呢?实际上在CWnd类中都保存了一个AFX_MSGMAP的结构,而在AFX_MSGMAP结构中保存有所有我们用ClassWizard生成的消息的数组的入口,我们把传给OnWndMsg的message和数组中的所有的message进行比较,找到匹配的那一个消息。实际上系统是通过函数AfxFindMessageEntry来实现的。找到了那个message,实际上我们就得到一个AFX_MSGMAP_ENTRY结构,而我们在上面已经提到AFX_MSGMAP_ENTRY保存了和该消息相关的所有信息,其中主要的是消息的动作标识和跟消息相关的执行函数。然后我们就可以根据消息的动作标识调用相关的执行函数,而这个执行函数实际上就是通过ClassWizard在类实现中定义的一个方法。这样就把消息的处理转化到类中的一个方法的实现上。举一个简单的例子,比如在View中对WM_LButtonDown消息的处理就转化成对如下一个方法的操作。 
void CInheritView::OnLButtonDown
(UINT nFlags, CPoint point) 
{
// TODO: Add your message 
handler code here and/or call default 
CView::OnLButtonDown(nFlags, point);
}

注意这里CView::OnLButtonDown(nFlags, point)实际上就是调用CWnd的Default()方法。 而Default()方法所做的工作就是调用DefWindowProc对消息进行处理。这实际上是调用原来的窗口过程进行缺省的消息处理。

如果OnWndMsg方法没有对消息进行处理的话,就调用DefWindowProc对消息进行处理。这是实际上是调用原来的窗口过程进行缺省的消息处理。 
---- 所以如果正常的消息处理的话,MFC窗口类是完全脱离了原来的窗口过程,用自己的一套体系结构实现消息的映射和处理。即先调用MFC窗口类挂上去的窗口过程,再调用原先的窗口过程。并且用户面对和消息相关的参数不再是死板的wParam和lParam,而是和消息类型具体相关的参数。比如和消息WM_LbuttonDown相对应的方法OnLButtonDown的两个参数是nFlags和point。nFlags表示在按下鼠标左键的时候是否有其他虚键按下,point更简单,就是表示鼠标的位置。 
---- 同时MFC窗口类消息传递中还提供了两个函数,分别为WalkPreTranslateTree和PreTranslateMessage。我们知道利用MFC框架生成的程序,都是从CWinApp开始执行的,而CWinapp实际继承了CWinThread类。在CWinThread的运行过程中会调用窗口类中的WalkPreTranslateTree方法。而WalkPreTranslateTree方法实际上就是从当前窗口开始查找愿意进行消息翻译的类,直到找到窗口没有父类为止。在WalkPreTranslateTree方法中调用了PreTranslateMessage方法。实际上PreTranslateMessage最大的好处是我们在消息处理前可以在这个方法里面先做一些事情。举一个简单的例子,比如我们希望在一个CEdit对象里,把所有的输入的字母都以大写的形式出现。我们只需要在PreTranslateMessage方法中判断message是否为WM_CHAR,如果是的话,把wParam(表示键值)由小写字母的值该为大写字母的值就实现了这个功能。

---- 继续上面的例子,根据我们对MFC消息机制的分析,我们很容易得到除了上面的方法,我们至少还可以在另外两个地方进行操作。

---- 一:在消息的处理方法里面即OnChar中,当然最后我们不再调用CEdit::OnChar(nChar, nRepCnt, nFlags),而是直接调用DefWindowProc(WM_CHAR,nChar,MAKELPARAM (nRepCnt,nFlags))。因为从我们上面的分析可以知道CEdit::OnChar(nChar, nRepCnt, nFlags)实际上也就是对DefWindowProc方法的调用。

---- 二:我们可以直接重载DefWindowProc方法,对message类型等于WM_CHAR的,直接修改nChar的值即可。

4.小结
---- 通过对MFC类库的分析和了解,不仅能够使我们更好的使用MFC类库,同时,对于我们自己设计和实现框架和类,无疑也有相当大的帮助。

二.MFC的消息映射机制 

MFC的设计者们在设计MFC时,紧紧把握一个目标,那就是尽可能使得MFC的代码要小,速度尽可能快。为了这个目标,他们使用了许多技巧,其中很多技巧体现在宏的运用上,实现MFC的消息映射的机制就是其中之一。
  同MFC消息映射机制有关的宏有下面几个:
  DECLARE_MESSAGE_MAP()宏
  BEGIN_MESSAGE_MAP(theClass, baseClass)和END_MESSAGE_MAP()宏
  弄懂MFC消息映射机制的最好办法是将找出一个具体的实例,将这些宏展开,并找出相关的数据结构。
  DECLARE_MESSAGE_MAP()
   DECLARE_MESSAGE_MAP()宏的定义如下:
  #define DECLARE_MESSAGE_MAP() \
  private: \
  static const AFX_MSGMAP_ENTRY _messageEntries[]; \
  protected: \
  static AFX_DATA const AFX_MSGMAP messageMap; \
  virtual const AFX_MSGMAP* GetMessageMap() const; \
从上面的定义可以看出,DECLARE_MESSAGE_MAP()作下面三件事:
  定义一个长度不定的静态数组变量_messageEntries[];
  定义一个静态变量messageMap;
  定义一个虚拟函数GetMessageMap();
在DECLARE_MESSAGE_MAP()宏中,涉及到MFC中两个对外不

【MFC 快捷键】VC程序快捷键的添加

     VC中我们经常使用到快捷键,这里快捷键有多种,包括menu,button。另外还区分local的和global的,其中local的职能在当前程序有焦点(被激活)时有效,而global的,则无论什么时候都有效,测试local的要优先于global的,就是如果当前激活窗口的快捷键与未激活窗口的快捷键重叠,当前激活窗口优先响应。

    这里将快捷键分为menu和button两种。

    menu添加local快捷键又分为两种, 一种是直接与菜单项关联的加速键,另一种就是自定义的加速键.

// zz :http://www.cnblogs.com/thankgoodness/articles/1136617.html

第一种: 

首先在资源文件Accelerator中添加快捷键资源 ID选择你要关联菜单项的名称,然后再设置你的快捷键.

下一步,在.h文件中加入一个    HACCEL   hAccel;变量然后在OnInitDialog或初始化中加入 hAccel=::LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MENU_MAIN));  后面的参数改成加速键的资源文件名.

最后在PreTranslateMessage(MSG* pMsg) 中加入:

    if(::TranslateAccelerator(GetSafeHwnd(),hAccel,pMsg))  

        return   true;

这样 以后只要在Accelerator资源文件中添加快捷键就可以了

注意: 添加快捷键的名字一定要与菜单名称一样 这样才能响应.现在只需要在此菜单项中加入OnCommand消息的处理就可以了.

第二种: 还是在资源文件Accelerator中添加快捷键资源 ID自己定义一个.然后再设置你的快捷键.

下一步...就是在.h文件中定义一个快捷键对象

HACCEL m_hAccel;

然后在.cpp文件中初始

m_hAccel = ::LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_ACCELERATOR1));

IDR_ACCELERATOR1为你的加速资源名称.注意不是刚刚定义的加速键ID.

再添加PreTranslateMessage消息处理 在里面加入以下代码:

 //保存快捷键被启用

 if(m_hAccel   !=   NULL)  

 {  

  if (TranslateAccelerator(m_hWnd, m_hAccel, pMsg))  

   return   TRUE;   

 }  

再添加OnCommand消息处理 加入以下代码:

//响应加速键

  switch(LOWORD(wParam))  

  {  

  case  SHOW_DIAL0G:   //加速键ID  

//...添加处理语句

  break;  

  case   SHOW_DIALOG_02:   //加速键ID  

//...添加处理语句

  break;  

  }  

menu添加global快捷键,以对话框程序为例:

在OnInitDialog中添加注册热键的代码:

RegisterHotKey(GetSafeHwnd(),1001,NULL,VK_F2);//F2键

RegisterHotKey(GetSafeHwnd(),1002,MOD_ALT,'1');//Alt+1键

BEGIN_MESSAGE_MAP(CXXXDlg, CDialog)

...

END_MESSAGE_MAP()

中添加WM_HOTKEY的消息映射:

ON_MESSAGE(WM_HOTKEY,&CXXXDlg::OnHotKey)//快捷键消息映射手动加入

在头文件中添加OnHotKey的声明:

protected:

afx_msg LRESULT OnHotKey(WPARAM wParam,LPARAM lParam);//手动加入.

CPP中OnHotKey的实现代码:

//相应快捷键的消息映射

LRESULT CXXXDlg::OnHotKey(WPARAM wParam,LPARAM lParam)

{

    if(wParam ==1001)

    { 

        MessageBox("热键F2已经按下!");

        //这里可以加上你按钮要执行的操作,或者直接调用按钮映射的消息函数

    }

    else if (wParam==1002)

    {

        MessageBox("热键ALT+1已经按下!");

    }

    return 0;

}

关闭对话框时使用

UnregisterHotKey(GetSafeHwnd(),1001);//注销F2键

UnregisterHotKey(GetSafeHwnd(),1002);//注销Alt+1键

注销热键.

    button添加locak快捷键可以直接在button的caption中用 &+'X' 即可,则按下 Ctrl + 'x' 即按下此按钮,起到快捷键的作用,另外还可以学习menu添加local快捷键的第二种方式,在PreTranslateMessage中添加处理函数,譬如:

BOOL   CYourDlg::PreTranslateMessage(MSG*   pMsg)    

  {  

          //   TODO:   Add   your   specialized   code   here   and/or   call   the   base   class  

          BOOL   bHandledMsg   =   FALSE;  

          switch   (pMsg->message)  

          {                                              

                  case   WM_KEYDOWN:  

                  {  

                          switch   (pMsg->wParam)  

                          {  

                                  case   VK_ESCAPE://ESC键  

                                          bHandledMsg   =   TRUE;  

                                          break;  

                                  case   13://回车  

                                          bHandledMsg   =   TRUE;  

                                          break;  

                                  default:   break;  

                          }   //   switch   (pMsg->wParam)  

                  }  

                  break;  

                  default:   break;                          

          }   //   switch   (pMsg->message)                                      

          return   (bHandledMsg   ?   TRUE   :   CDialog::PreTranslateMessage(pMsg));    

  }

http://topic.csdn.net/t/20010307/09/80404.html

初学者的困惑:快捷键(加速键accelerator)怎么不起作用?

原文:新浪博客

公开的数据结构
AFX_MSGMAP_ENTRY和AFX_MSGMAP。为了弄清楚消息映射,有必要考察一下这两个数据结构的定义。
  AFX_MSGMAP_ENTRY的定义
  struct AFX_MSGMAP_ENTRY
  {
   UINT nMessage; // windows message
   UINT nCode; // control code or WM_NOTIFY code
   UINT nID; // control ID (or 0 for windows messages)
   UINT nLastID; // used for entries specifying a range of control id's
   UINT nSig; // signature type (action) or pointer to message #
   AFX_PMSG pfn; // routine to call (or special value)
  };
结构中各项的含义注释已经说明得很清楚了,这里不再多述,从上面的定义你是否看出,AFX_MSGMAP_ENTRY结构实际上定义了消息和处理此消息的动作之间的映射关系。因此静态数组变量_messageEntries[]实际上定义了一张表,表中的每一项指定了相应的对象所要处理的消息和处理此消息的函数的对应关系,因而这张表也称为消息映射表。再看看AFX_MSGMAP的定义。
  (2)AFX_MSGMAP的定义
  struct AFX_MSGMAP
  {
   const AFX_MSGMAP* pBaseMap;
   const AFX_MSGMAP_ENTRY* lpEntries;
   };
不难看出,AFX_MSGMAP定义了一单向链表,链表中每一项的值是一指向消息映射表的指针(实际上就是_messageEntries的值)。通过这个链表,使得在某个类中调用基类的的消息处理函数很容易,因此,“父类的消息处理函数是子类的缺省消息处理函数”就“顺理成章”了。在后面的“MFC窗口的消息处理”一节中会对此作详细的讲解。

由上述可见,在类的头文件中主要定义了两个数据结构:消息映射表和单向链表。(孙建东总结)
  BEGIN_MESSAGE_MAP()和END_MESSAGE_MAP()
  它们的定义如下:
  #define BEGIN_MESSAGE_MAP(theClass, baseClass) \
  const AFX_MSGMAP* theClass::GetMessageMap() const \
  { return &theClass::messageMap; } \
  AFX_COMDAT AFX_DATADEF const AFX_MSGMAP theClass::messageMap = \
  { &baseClass::messageMap, &theClass::_messageEntries[0] }; \
  AFX_COMDAT const AFX_MSGMAP_ENTRY theClass::_messageEntries[] = \
  { \
   #define END_MESSAGE_MAP() \
   {0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \
   }; \
对应BEGIN_MESSAGE_MAP()的定义可能不是一下子就看得明白,不过不要紧,举一例子就很清楚了。对于BEGIN_MESSAGE_MAP(CView, CWnd),VC预编译器将其展开成下面的形式:
  const AFX_MSGMAP* CView::GetMessageMap() const
  { 
   return &CView::messageMap; 
   }
  AFX_COMDAT AFX_DATADEF const AFX_MSGMAP CView::messageMap =
  { 
   &CWnd::messageMap, 
   &CView::_messageEntries[0] 
  };
  AFX_COMDAT const AFX_MSGMAP_ENTRY CView::_messageEntries[] =
  {
  至于END_MESSAGE_MAP()则不过定义了一个表示映射表结束的标志项,我想大家对于这种简单的技巧应该是很熟悉的,无需多述。

到此为止,我想大家也已经想到了象ON_COMMAND这样的宏的具体作用了,不错它们只不过定义了一种类型的消息映射项,看看ON_COMMAND的定义:
  #define ON_COMMAND(id, memberFxn) \
  { WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSig_vv, (AFX_PMSG)&memberFxn },
   根据上面的定义,ON_COMMAND(ID_FILE_NEW, OnFileNew)将被VC预编译器展开
   如下:
  {WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSig_vv, 
  (AFX_PMSG)&OnFileNew},
到此,MFC的消息映射机制已经清楚了,现在提出并解答两个问题以作为对这一节的小结。
  为什么不直接使用虚拟函数实现消息处理函数呢?这是一个GOOD QUESTION。前面已经说过,MFC的设计者们在设计MFC时有一个很明确的目标,就是使得“MFC的代码尽可能小,速度尽可能快”,如果采用虚拟函数,那么对于所有的窗口消息,都必须有一个与之对应的虚拟函数,因而对每一个从CWnd派生的类而言,都会有一张很大的虚拟函数表vtbl。但是在实际应用中,一般只对少数的消息进行处理,大部分都交给系统缺省处理,所以表中的大部分项都是无用项,这样做就浪费了很多内存资源,这同MFC设计者们的设计目标是相违背的。当然,MFC所使用的方法只是解决这类问题的方式之一,不排除还有其他的解决方式,但就我个人观点而言,这是一种最好的解决方式,体现了很高的技巧性,值得我们学习。
  至于这第二个问题,是由上面的问题引申出来的。如果在子类和父类中出现了相同的消息出来函数,VC编译器会怎么处理这个问题呢?VC不会将它们看作错误,而会象对待虚拟函数类似的方式去处理,但对于消息处理函数(带afx_msg前缀),则不会生成虚拟函数表vtbl。

MFC下一个消息的处理过程是一般是这样的。
1、_AfxCbtFilterHook截获消息(这是一个钩子函数)
2、_AfxCbtFilterHook把窗口过程设定为AfxWndProc。
3、函数AfxWndProc接收Windows操作系统发送的消息。
4、函数AfxWndProc调用函数AfxCallWndProc进行消息处理。
5、函数AfxCallWndProc调用CWnd类的方法WindowProc进行消息处理。
如何添加自己的消息?
我们已经了解了WINDOW的消息机制,如何加入我们自己的消息呢?好我们来看
一个标准的消息处理程序是这个样子的
在 CWnd 类中预定义了标准 Windows 消息 (WM_XXXX  WM是WINDOW MESSAGE的缩写) 的默认处理程序。类库基于消息名命名这些处理程序。例如,WM_PAINT 消息的处理程序在 CWnd 中被声明为:
afx_msg void OnPaint();
afx_msg 关键字通过使这些处理程序区别于其他 CWnd 成员函数来表明 C++ virtual 关键字的作用。但是请注意,这些函数实际上并不是虚拟的,而是通过消息映射实现的。我们在本文的一开始便说明了为什么要这样做。
所有能够进行消息处理的类都是基于CCmdTarget类的,也就是说CCmdTarget类是所有可以进行消息处理类的父类。CCmdTarget类是MFC处理命令消息的基础和核心。
若要重写基类中定义的处理程序,只需在派生类中定义一个具有相同原型的函数,并创建此处理程序的消息映射项。我们通过ClassWizard可以建立大多数窗口消息或自定义的消息,通过ClassWizard可以自动建立消息映射,和消息处理函数的框架,我们只需要把我们要做的事情填空,添加你要做的事情到处理函数。这个非常简单,就不细说了。但是也许我们需要添加一些ClassWizard不支持的窗口消息或自定义消息,那么就需要我们亲自动手建立消息映射和消息处理的框架,通常步骤如下:
第一步:定义消息。Microsoft推荐用户自定义消息至少是WM_USER+100,因为很多新控件也要使用WM_USER消息。
#define WM_MYMESSAGE (WM_USER + 100)
第二步:实现消息处理函数。该函数使用WPRAM和LPARAM参数并返回LPESULT。
LPESULT CMainFrame::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
// TODO: 处理用户自定义消息,填空就是要填到这里。
return 0;
}
第三步:在类头文件的AFX_MSG块中说明消息处理函数:
// {{AFX_MSG(CMainFrame)
afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
第四步:在用户类的消息块中,使用ON_MESSAGE宏指令将消息映射到消息处理函数中。
ON_MESSAGE( WM_MYMESSAGE, OnMyMessage )

MFC消息机制简析_DyLan985的博客-CSDN博客

【MFC 程序框架】MFC程序框架/VC 程序运行框架/VC运行顺序 从哪运行 

2012-09-05 22:06:35

原文:MFC程序框架剖析_petergu1984的博客-CSDN博客

1:程序的“导火索”---theApp

CmyApp theApp;

在声明对象的同时,调用其构造函数。按C++的语法,首先要调用其基类Cwinapp的构造函数. 这个文件主要用于应用程序的一些初始化操作。

class CWinApp : public CWinThread

{

    DECLARE_DYNAMIC(CWinApp)

public:

// Constructor

    CWinApp(LPCTSTR lpszAppName = NULL); 

…………

}

CWinApp::CWinApp(LPCTSTR lpszAppName)

{

    if (lpszAppName != NULL)

       m_pszAppName = _tcsdup(lpszAppName);

    else

       m_pszAppName = NULL;

    // initialize CWinThread state

    AFX_MODULE_STATE* pModuleState = _AFX_CMDTARGET_GETSTATE();

    AFX_MODULE_THREAD_STATE* pThreadState = pModuleState->m_thread;

    ASSERT(AfxGetThread() == NULL);

    pThreadState->m_pCurrentWinThread = this;

    ASSERT(AfxGetThread() == this);

    m_hThread = ::GetCurrentThread();

    m_nThreadID = ::GetCurrentThreadId();

    // initialize CWinApp state

    ASSERT(afxCurrentWinApp == NULL); // only one CWinApp object please

    pModuleState->m_pCurrentWinApp = this;

    ASSERT(AfxGetApp() == this);

    // in non-running state until WinMain

    m_hInstance = NULL;

    m_hLangResourceDLL = NULL;

    m_pszHelpFilePath = NULL;

    m_pszProfileName = NULL;

    m_pszRegistryKey = NULL;

    m_pszExeName = NULL;

    m_pRecentFileList = NULL;

    m_pDocManager = NULL;

    m_atomApp = m_atomSystemTopic = NULL;

    m_lpCmdLine = NULL;

    m_pCmdInfo = NULL;

    // initialize wait cursor state

    m_nWaitCursorCount = 0;

    m_hcurWaitCursorRestore = NULL;

    // initialize current printer state

    m_hDevMode = NULL;

    m_hDevNames = NULL;

    m_nNumPreviewPages = 0;     // not specified (defaults to 1)

    // initialize DAO state

    m_lpfnDaoTerm = NULL;   // will be set if AfxDaoInit called

    // other initialization

    m_bHelpMode = FALSE;

    m_eHelpType = afxWinHelp;

    m_nSafetyPoolSize = 512;        // default size

}

2:theApp之后的隐藏代码,由他控制整个程序的流程。

_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

    LPTSTR lpCmdLine, int nCmdShow)

{

    // call shared/exported WinMain

    return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);

}

其中有宏定义:#define _tWinMain   wWinMain

int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

    LPTSTR lpCmdLine, int nCmdShow)

{

    ASSERT(hPrevInstance == NULL);

    int nReturnCode = -1;

    CWinThread* pThread = AfxGetThread();;// CWinApp是从CWinThread派生的,

    CWinApp* pApp = AfxGetApp();  //实质上就是pThread==pApp

    // AFX internal initialization

    if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))  //用于初始化

       goto InitFailure;

    // App global initializations (rare)

    if (pApp != NULL && !pApp->InitApplication())   //用于初始化

       goto InitFailure;

    // Perform specific initializations

    if (!pThread->InitInstance())   //注意多态性  virtual BOOL InitInstance();

                                   //又因为pThread==pApp,所以调用pApp->InitInstance()

    {

       if (pThread->m_pMainWnd != NULL)

       {

           TRACE(traceAppMsg, 0, "Warning: Destroying non-NULL m_pMainWnd/n");

           pThread->m_pMainWnd->DestroyWindow();

       }

       nReturnCode = pThread->ExitInstance();

       goto InitFailure;

    }

    nReturnCode = pThread->Run();  //控制消息循环

InitFailure:

#ifdef _DEBUG

    // Check for missing AfxLockTempMap calls

    if (AfxGetModuleThreadState()->m_nTempMapLock != 0)

    {

       TRACE(traceAppMsg, 0, "Warning: Temp map lock count non-zero (%ld)./n",

           AfxGetModuleThreadState()->m_nTempMapLock);

    }

    AfxLockTempMaps();

    AfxUnlockTempMaps(-1);

#endif

    AfxWinTerm();

    return nReturnCode;

}

由上面的程序可以看到几个很重要的函数

(1)AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))

       goto InitFailure;

 (2) pApp->InitApplication())

 (3) pThread->InitInstance()

 (4) pThread->Run()

其中1,2 也是完成程序的一些初始化工作,4 主要是为了处理消息,3呢,很关键,我们运行时看到的窗口就是从这里产生。下面一一介绍

3:程序自动产生的InitInstance()函数

以下是自动生成的InitInstance()源程序:

BOOL CmyApp::InitInstance()

    {

    // 如果一个运行在 Windows XP 上的应用程序清单指定要

    // 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,

    //则需要 InitCommonControls()。否则,将无法创建窗口。

    InitCommonControls();

    CWinApp::InitInstance();

    // 初始化 OLE 库

    if (!AfxOleInit())

    {

       AfxMessageBox(IDP_OLE_INIT_FAILED);

       return FALSE;

    }

    AfxEnableControlContainer();

    // 标准初始化

    // 如果未使用这些功能并希望减小

    // 最终可执行文件的大小,则应移除下列

    // 不需要的特定初始化例程

    // 更改用于存储设置的注册表项

    // TODO: 应适当修改该字符串,

    // 例如修改为公司或组织名

    SetRegistryKey(_T("应用程序向导生成的本地应用程序"));

    LoadStdProfileSettings(4);  // 加载标准 INI 文件选项(包括 MRU)

    // 注册应用程序的文档模板。文档模板

    // 将用作文档、框架窗口和视图之间的连接

    CMultiDocTemplate* pDocTemplate;

    pDocTemplate = new CMultiDocTemplate(IDR_myTYPE,

       RUNTIME_CLASS(CmyDoc),

       RUNTIME_CLASS(CChildFrame), // 自定义 MDI 子框架

       RUNTIME_CLASS(CmyView));

    if (!pDocTemplate)

       return FALSE;

    AddDocTemplate(pDocTemplate);

    // 创建主 MDI 框架窗口

    CMainFrame* pMainFrame = new CMainFrame;

    if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))

       return FALSE;

    m_pMainWnd = pMainFrame;

    // 仅当具有后缀时才调用 DragAcceptFiles

    //  在 MDI 应用程序中,这应在设置 m_pMainWnd 之后立即发生

    // 分析标准外壳命令、DDE、打开文件操作的命令行

    CCommandLineInfo cmdInfo;

    ParseCommandLine(cmdInfo);

    // 调度在命令行中指定的命令。如果

    // 用 /RegServer、/Register、/Unregserver 或 /Unregister 启动应用程序,则返回FALSE。

    if (!ProcessShellCommand(cmdInfo))   //引发窗口注册

       return FALSE;

    // 主窗口已初始化,因此显示它并对其进行更新

    pMainFrame->ShowWindow(m_nCmdShow);

    pMainFrame->UpdateWindow();

    return TRUE;

}

其中,注册窗口用到了一下函数,比较长,如下:

BOOL AFXAPI AfxEndDeferRegisterClass(LONG fToRegister)

{

    // mask off all classes that are already registered

    AFX_MODULE_STATE* pModuleState = AfxGetModuleState();

    fToRegister &= ~pModuleState->m_fRegisteredClasses;

    if (fToRegister == 0)

       return TRUE;

    LONG fRegisteredClasses = 0;

    // common initialization

    WNDCLASS wndcls;

    memset(&wndcls, 0, sizeof(WNDCLASS));   // start with NULL defaults

    wndcls.lpfnWndProc = DefWindowProc;    //窗口处理函数

    wndcls.hInstance = AfxGetInstanceHandle();

    wndcls.hCursor = afxData.hcurArrow;

    INITCOMMONCONTROLSEX init;

    init.dwSize = sizeof(init);

    // work to register classes as specified by fToRegister, populate fRegisteredClasses as we go

    if (fToRegister & AFX_WND_REG)

    {

       // Child windows - no brush, no icon, safest default class styles

       wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;

       wndcls.lpszClassName = _afxWnd;

       if (AfxRegisterClass(&wndcls))

           fRegisteredClasses |= AFX_WND_REG;

    }

    if (fToRegister & AFX_WNDOLECONTROL_REG)

    {

       // OLE Control windows - use parent DC for speed

       wndcls.style |= CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;

       wndcls.lpszClassName = _afxWndOleControl;

       if (AfxRegisterClass(&wndcls))

           fRegisteredClasses |= AFX_WNDOLECONTROL_REG;

    }

    if (fToRegister & AFX_WNDCONTROLBAR_REG)

    {

       // Control bar windows

       wndcls.style = 0;   // control bars don't handle double click

       wndcls.lpszClassName = _afxWndControlBar;

       wndcls.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);

       if (AfxRegisterClass(&wndcls))

           fRegisteredClasses |= AFX_WNDCONTROLBAR_REG;

    }

    if (fToRegister & AFX_WNDMDIFRAME_REG)

    {

       // MDI Frame window (also used for splitter window)

       wndcls.style = CS_DBLCLKS;

       wndcls.hbrBackground = NULL;

       if (_AfxRegisterWithIcon(&wndcls, _afxWndMDIFrame, AFX_IDI_STD_MDIFRAME))

           fRegisteredClasses |= AFX_WNDMDIFRAME_REG;

    }

    if (fToRegister & AFX_WNDFRAMEORVIEW_REG)

    {

       // SDI Frame or MDI Child windows or views - normal colors

       wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;

       wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);

       if (_AfxRegisterWithIcon(&wndcls, _afxWndFrameOrView, AFX_IDI_STD_FRAME))

           fRegisteredClasses |= AFX_WNDFRAMEORVIEW_REG;

    }

    if (fToRegister & AFX_WNDCOMMCTLS_REG)

    {

       // this flag is compatible with the old InitCommonControls() API

       init.dwICC = ICC_WIN95_CLASSES;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WIN95CTLS_MASK);

       fToRegister &= ~AFX_WIN95CTLS_MASK;

    }

    if (fToRegister & AFX_WNDCOMMCTL_UPDOWN_REG)

    {

       init.dwICC = ICC_UPDOWN_CLASS;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_UPDOWN_REG);

    }

    if (fToRegister & AFX_WNDCOMMCTL_TREEVIEW_REG)

    {

       init.dwICC = ICC_TREEVIEW_CLASSES;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_TREEVIEW_REG);

    }

    if (fToRegister & AFX_WNDCOMMCTL_TAB_REG)

    {

       init.dwICC = ICC_TAB_CLASSES;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_TAB_REG);

    }

    if (fToRegister & AFX_WNDCOMMCTL_PROGRESS_REG)

    {

       init.dwICC = ICC_PROGRESS_CLASS;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_PROGRESS_REG);

    }

    if (fToRegister & AFX_WNDCOMMCTL_LISTVIEW_REG)

    {

       init.dwICC = ICC_LISTVIEW_CLASSES;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_LISTVIEW_REG);

    }

    if (fToRegister & AFX_WNDCOMMCTL_HOTKEY_REG)

    {

       init.dwICC = ICC_HOTKEY_CLASS;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_HOTKEY_REG);

    }

    if (fToRegister & AFX_WNDCOMMCTL_BAR_REG)

    {

       init.dwICC = ICC_BAR_CLASSES;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_BAR_REG);

    }

    if (fToRegister & AFX_WNDCOMMCTL_ANIMATE_REG)

    {

       init.dwICC = ICC_ANIMATE_CLASS;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_ANIMATE_REG);

    }

    if (fToRegister & AFX_WNDCOMMCTL_INTERNET_REG)

    {

       init.dwICC = ICC_INTERNET_CLASSES;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_INTERNET_REG);

    }

    if (fToRegister & AFX_WNDCOMMCTL_COOL_REG)

    {

       init.dwICC = ICC_COOL_CLASSES;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_COOL_REG);

    }

    if (fToRegister & AFX_WNDCOMMCTL_USEREX_REG)

    {

       init.dwICC = ICC_USEREX_CLASSES;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_USEREX_REG);

    }

    if (fToRegister & AFX_WNDCOMMCTL_DATE_REG)

    {

       init.dwICC = ICC_DATE_CLASSES;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_DATE_REG);

    }

    if (fToRegister & AFX_WNDCOMMCTL_LINK_REG)

    {

       init.dwICC = ICC_LINK_CLASS;

       fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_LINK_REG);

    }

    // save new state of registered controls

    pModuleState->m_fRegisteredClasses |= fRegisteredClasses;

    // special case for all common controls registered, turn on AFX_WNDCOMMCTLS_REG

    if ((pModuleState->m_fRegisteredClasses & AFX_WIN95CTLS_MASK) == AFX_WIN95CTLS_MASK)

    {

       pModuleState->m_fRegisteredClasses |= AFX_WNDCOMMCTLS_REG;

       fRegisteredClasses |= AFX_WNDCOMMCTLS_REG;

    }

    // must have registered at least as mamy classes as requested

    return (fToRegister & fRegisteredClasses) == fToRegister;

}

他是在PreCreateWindow中调用的注册,

BOOL CFrameWnd::PreCreateWindow(CREATESTRUCT& cs)

{

    if (cs.lpszClass == NULL)

    {

       VERIFY(AfxDeferRegisterClass(AFX_WNDFRAMEORVIEW_REG));

       cs.lpszClass = _afxWndFrameOrView;  // COLOR_WINDOW background

    }

    if (cs.style & FWS_ADDTOTITLE)

       cs.style |= FWS_PREFIXTITLE;

    cs.dwExStyle |= WS_EX_CLIENTEDGE;

    return TRUE;

}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)

{

    if( !CFrameWnd::PreCreateWindow(cs) )

       return FALSE;

    // TODO: Modify the Window class or styles here by modifying

    //  the CREATESTRUCT cs

    return TRUE;

}

BOOL CView::PreCreateWindow(CREATESTRUCT & cs)

{

    ASSERT(cs.style & WS_CHILD);

    if (cs.lpszClass == NULL)

    {

       VERIFY(AfxDeferRegisterClass(AFX_WNDFRAMEORVIEW_REG));

       cs.lpszClass = _afxWndFrameOrView;  // COLOR_WINDOW background

    }

    if (cs.style & WS_BORDER)

    {

       cs.dwExStyle |= WS_EX_CLIENTEDGE;

       cs.style &= ~WS_BORDER;

    }

    return TRUE;

}

BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)

{

    // TODO: Modify the Window class or styles here by modifying

    //  the CREATESTRUCT cs

    return CView::PreCreateWindow(cs);

}

有几点要说明

(1)m_pMainWnd,头文件里并没有这个变量呀? 噢,这个在基类里定义的public 类型的变量,所以,他是通过继承得到的。

(2) 你注意过吗,当我们一点运行,会默认出来一个视图窗口,这是谁调用的呢?哦,就是下面这几行,它调用了opendocument() 函数。

CCommandLineInfo cmdInfo;

    ParseCommandLine(cmdInfo);

    if (!ProcessShellCommand(cmdInfo))

       return FALSE;

(3)这个函数有个关键的地方

CMultiDocTemplate* pDocTemplate;

    pDocTemplate = new CMultiDocTemplate(IDR_myTYPE,

       RUNTIME_CLASS(CmyDoc),

       RUNTIME_CLASS(CChildFrame), // 自定义 MDI 子框架

       RUNTIME_CLASS(CmyView));

    if (!pDocTemplate)

       return FALSE;

    AddDocTemplate(pDocTemplate);

通过它,把视图,窗口和文档结合起来。

4:主窗口CMainFrame的创建

(1)通过CMainFrame* pMainFrame = new CMainFrame;

 我们得到的是不断调用的基类

CMainFrame::CMainFrame()

{

    // TODO: 在此添加成员初始化代码

}

CMDIFrameWnd::CMDIFrameWnd()

{

    m_hWndMDIClient = NULL;

}

CFrameWnd::CFrameWnd()

{

    ASSERT(m_hWnd == NULL);

    m_nWindow = -1;                 // unknown window ID

    m_bAutoMenuEnable = TRUE;       // auto enable on by default

    m_lpfnCloseProc = NULL;

    m_hMenuDefault = NULL;

    m_hAccelTable = NULL;

    m_nIDHelp = 0;

    m_nIDTracking = 0;

    m_nIDLastMessage = 0;

    m_pViewActive = NULL;

    m_cModalStack = 0;              // initialize modality support

    m_phWndDisable = NULL;

    m_pNotifyHook = NULL;

    m_hMenuAlt = NULL;

    m_nIdleFlags = 0;               // no idle work at start

    m_rectBorder.SetRectEmpty();

    m_bHelpMode = HELP_INACTIVE;    // not in Shift+F1 help mode

    m_dwPromptContext = 0;

    m_pNextFrameWnd = NULL;         // not in list yet

    m_bInRecalcLayout = FALSE;

    m_pFloatingFrameClass = NULL;

    m_nShowDelay = -1;              // no delay pending

    AddFrameWnd();

}

(2)pMainFrame->LoadFrame(IDR_MAINFRAME)所引发的一系列

BOOL CMDIFrameWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,

    CWnd* pParentWnd, CCreateContext* pContext)

{

    if (!CFrameWnd::LoadFrame(nIDResource, dwDefaultStyle,

      pParentWnd, pContext))

       return FALSE;

    // save menu to use when no active MDI child window is present

    ASSERT(m_hWnd != NULL);

    m_hMenuDefault = ::GetMenu(m_hWnd);

    if (m_hMenuDefault == NULL)

       TRACE(traceAppMsg, 0, "Warning: CMDIFrameWnd without a default menu./n");

    return TRUE;

}

BOOL CFrameWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,

    CWnd* pParentWnd, CCreateContext* pContext)

{

    // only do this once

    ASSERT_VALID_IDR(nIDResource);

    ASSERT(m_nIDHelp == 0 || m_nIDHelp == nIDResource);

    m_nIDHelp = nIDResource;    // ID for help context (+HID_BASE_RESOURCE)

    CString strFullString;

    if (strFullString.LoadString(nIDResource))

       AfxExtractSubString(m_strTitle, strFullString, 0);    // first sub-string

    VERIFY(AfxDeferRegisterClass(AFX_WNDFRAMEORVIEW_REG));

    // attempt to create the window

    LPCTSTR lpszClass = GetIconWndClass(dwDefaultStyle, nIDResource);

    CString strTitle = m_strTitle;

    if (!Create(lpszClass, strTitle, dwDefaultStyle, rectDefault,

     pParentWnd, MAKEINTRESOURCE(nIDResource), 0L, pContext))

    {

       return FALSE;   // will self destruct on failure normally

    }

    // save the default menu handle

    ASSERT(m_hWnd != NULL);

    m_hMenuDefault = ::GetMenu(m_hWnd);

    // load accelerator resource

    LoadAccelTable(MAKEINTRESOURCE(nIDResource));

    if (pContext == NULL)   // send initial update

       SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE, TRUE);

    return TRUE;

}

BOOL CFrameWnd::Create(LPCTSTR lpszClassName,

    LPCTSTR lpszWindowName,

    DWORD dwStyle,

    const RECT& rect,

    CWnd* pParentWnd,

    LPCTSTR lpszMenuName,

    DWORD dwExStyle,

    CCreateContext* pContext)

{

    HMENU hMenu = NULL;

    if (lpszMenuName != NULL)

    {

       // load in a menu that will get destroyed when window gets destroyed

       HINSTANCE hInst = AfxFindResourceHandle(lpszMenuName, RT_MENU);

       if ((hMenu = ::LoadMenu(hInst, lpszMenuName)) == NULL)

       {

           TRACE(traceAppMsg, 0, "Warning: failed to load menu for CFrameWnd./n");

           PostNcDestroy();            // perhaps delete the C++ object

           return FALSE;

       }

    }

    m_strTitle = lpszWindowName;    // save title for later

    if (!CreateEx(dwExStyle, lpszClassName, lpszWindowName, dwStyle,

      rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,

      pParentWnd->GetSafeHwnd(), hMenu, (LPVOID)pContext))

    {

       TRACE(traceAppMsg, 0, "Warning: failed to create CFrameWnd./n");

       if (hMenu != NULL)

           DestroyMenu(hMenu);

       return FALSE;

    }

    return TRUE;

}

因为CFrameWnd没有重新写CreateEX,所以是调用的基类的CreateEx的函数:

BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName,

    LPCTSTR lpszWindowName, DWORD dwStyle,

    int x, int y, int nWidth, int nHeight,

    HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam)

{

    // allow modification of several common create parameters

    CREATESTRUCT cs;

    cs.dwExStyle = dwExStyle;

    cs.lpszClass = lpszClassName;

    cs.lpszName = lpszWindowName;

    cs.style = dwStyle;

    cs.x = x;

    cs.y = y;

    cs.cx = nWidth;

    cs.cy = nHeight;

    cs.hwndParent = hWndParent;

    cs.hMenu = nIDorHMenu;

    cs.hInstance = AfxGetInstanceHandle();

    cs.lpCreateParams = lpParam;

    if (!PreCreateWindow(cs))

    {

       PostNcDestroy();

       return FALSE;

    }

    AfxHookWindowCreate(this);

    HWND hWnd = ::CreateWindowEx(cs.dwExStyle, cs.lpszClass,

           cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,

           cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);

#ifdef _DEBUG

    if (hWnd == NULL)

    {

       TRACE(traceAppMsg, 0, "Warning: Window creation failed: GetLastError returns 0x%8.8X/n",

           GetLastError());

    }

#endif

    if (!AfxUnhookWindowCreate())

       PostNcDestroy();        // cleanup if CreateWindowEx fails too soon

    if (hWnd == NULL)

       return FALSE;

    ASSERT(hWnd == m_hWnd); // should have been set in send msg hook

    return TRUE;

}

可以看到,::CreateWindowEx是一个全局的函数,在其中触发WM_CREATE消息,进而调用我们自己定义的CMainFrame::OnCreate()

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

    if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)

       return -1;

    if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP

       | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||

       !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))

    {

       TRACE0("未能创建工具栏/n");

       return -1;      // 未能创建

    }

    if (!m_wndStatusBar.Create(this) ||   

       !m_wndStatusBar.SetIndicators(indicators,

       sizeof(indicators)/sizeof(UINT)))

    {

       TRACE0("未能创建状态栏/n");

       return -1;      // 未能创建

    }

     //TODO: 如果不需要工具栏可停靠,则删除这三行

    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);

    EnableDocking(CBRS_ALIGN_ANY);

    DockControlBar(&m_wndToolBar);

    return 0;

}

5:运行后为什么能产生CMainFarme,文档 ,视图以及视图外包围的farme呢?

你注意过吗,当我们一点运行,会默认出来一个视图窗口,这是谁调用的呢?哦,就是下面这几行,它调用void CWinApp::OnFileNew()函数.

CCommandLineInfo cmdInfo;

    ParseCommandLine(cmdInfo);

    if (!ProcessShellCommand(cmdInfo))

       return FALSE;

你可以自己跟踪以下。这里有个小技巧,你可以在BOOL CmyDoc::OnNewDocument()处设立断点,然后“跳出”,最后你会达到起始点!ProcessShellCommand(cmdInfo)

void CWinApp::OnFileNew()

{

    if (m_pDocManager != NULL)

       m_pDocManager->OnFileNew();    //接着调用下面的函数

}

void CDocManager::OnFileNew()

{

    if (m_templateList.IsEmpty())

    {

       TRACE(traceAppMsg, 0, "Error: no document templates registered with CWinApp./n");

       AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);

       return;

    }

    CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetHead();

    if (m_templateList.GetCount() > 1)

    {

       // more than one document template to choose from

       // bring up dialog prompting user

       CNewTypeDlg dlg(&m_templateList);

       INT_PTR nID = dlg.DoModal();

       if (nID == IDOK)

           pTemplate = dlg.m_pSelectedTemplate;

       else

           return;     // none - cancel operation

    }

    ASSERT(pTemplate != NULL);

    ASSERT_KINDOF(CDocTemplate, pTemplate);

    pTemplate->OpenDocumentFile(NULL);  //接着调用下面的函数

       // if returns NULL, the user has already been alerted

}

CDocument* CMultiDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,

   BOOL bMakeVisible)

{

    CDocument* pDocument = CreateNewDocument();  //产生模板控制之一:文档

    if (pDocument == NULL)

    {

       TRACE(traceAppMsg, 0, "CDocTemplate::CreateNewDocument returned NULL./n");

       AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);

       return NULL;

    }

    ASSERT_VALID(pDocument);

    BOOL bAutoDelete = pDocument->m_bAutoDelete;

    pDocument->m_bAutoDelete = FALSE;   // don't destroy if something goes wrong

   CFrameWnd* pFrame = CreateNewFrame(pDocument, NULL);  //产生新的:框架 

    pDocument->m_bAutoDelete = bAutoDelete;

    if (pFrame == NULL)

    {

       AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);

       delete pDocument;       // explicit delete on error

       return NULL;

    }

    ASSERT_VALID(pFrame);

    if (lpszPathName == NULL)

    {

       // create a new document - with default document name

       SetDefaultTitle(pDocument);

       // avoid creating temporary compound file when starting up invisible

       if (!bMakeVisible)

           pDocument->m_bEmbedded = TRUE;

       if (!pDocument->OnNewDocument())

       {

           // user has be alerted to what failed in OnNewDocument

           TRACE(traceAppMsg, 0, "CDocument::OnNewDocument returned FALSE./n");

           pFrame->DestroyWindow();

           return NULL;

       }

       // it worked, now bump untitled count

       m_nUntitledCount++;

    }

    else

    {

       // open an existing document

       CWaitCursor wait;

       if (!pDocument->OnOpenDocument(lpszPathName))

       {

           // user has be alerted to what failed in OnOpenDocument

           TRACE(traceAppMsg, 0, "CDocument::OnOpenDocument returned FALSE./n");

           pFrame->DestroyWindow();

           return NULL;

       }

       pDocument->SetPathName(lpszPathName);

    }

    InitialUpdateFrame(pFrame, pDocument, bMakeVisible);

    return pDocument;

}

我需要对上述函数进行进一步说明

(1)CreateNewDocument()产生文档

CDocument* CDocTemplate::CreateNewDocument()

{

    // default implementation constructs one from CRuntimeClass

    if (m_pDocClass == NULL)

    {

       TRACE(traceAppMsg, 0, "Error: you must override CDocTemplate::CreateNewDocument./n");

       ASSERT(FALSE);

       return NULL;

    }

    CDocument* pDocument = (CDocument*)m_pDocClass->CreateObject();

    if (pDocument == NULL)

    {

       TRACE(traceAppMsg, 0, "Warning: Dynamic create of document type %hs failed./n",

           m_pDocClass->m_lpszClassName);

       return NULL;

    }

    ASSERT_KINDOF(CDocument, pDocument);

    AddDocument(pDocument);

    return pDocument;

}

(2)CreateNewFrame(pDocument, NULL),产生框架和视图

CFrameWnd* CDocTemplate::CreateNewFrame(CDocument* pDoc, CFrameWnd* pOther)

{

    if (pDoc != NULL)

       ASSERT_VALID(pDoc);

    // create a frame wired to the specified document

    ASSERT(m_nIDResource != 0); // must have a resource ID to load from

    CCreateContext context;

    context.m_pCurrentFrame = pOther;

    context.m_pCurrentDoc = pDoc;

    context.m_pNewViewClass = m_pViewClass;

    context.m_pNewDocTemplate = this;

    if (m_pFrameClass == NULL)

    {

       TRACE(traceAppMsg, 0, "Error: you must override CDocTemplate::CreateNewFrame./n");

       ASSERT(FALSE);

       return NULL;

    }

    CFrameWnd* pFrame = (CFrameWnd*)m_pFrameClass->CreateObject();

    if (pFrame == NULL)

    {

       TRACE(traceAppMsg, 0, "Warning: Dynamic create of frame %hs failed./n",

           m_pFrameClass->m_lpszClassName);

       return NULL;

    }

    ASSERT_KINDOF(CFrameWnd, pFrame);

    if (context.m_pNewViewClass == NULL)

       TRACE(traceAppMsg, 0, "Warning: creating frame with no default view./n");

    // create new from resource

    if (!pFrame->LoadFrame(m_nIDResource,

         WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, &context))

    {

       TRACE(traceAppMsg, 0, "Warning: CDocTemplate couldn't create a frame./n");

       // frame will be deleted in PostNcDestroy cleanup

       return NULL;

    }

    // it worked !

    return pFrame;

}

在此函数中,不仅Frame被动态创建出来,其对应的窗口也以LoadFrame产生出来。其中参数context帮助产生了视图。别管怎么产生了,我跟踪不到,反正就是这个时候把框架和视图一起产生了出来。此时激发了WN_CREATE消息,从而引发CFrameWnd::OnCreate函数。我分别把相应的函数写在下面

CFrameWnd::OnCreate------CFrameWnd::OnCreateHelper------CFrameWnd:OnCreateClient-----CFrameWnd::CreateView*/

int CFrameWnd::OnCreate(LPCREATESTRUCT lpcs)

{

    CCreateContext* pContext = (CCreateContext*)lpcs->lpCreateParams;

    return OnCreateHelper(lpcs, pContext);

}

int CFrameWnd::OnCreateHelper(LPCREATESTRUCT lpcs, CCreateContext* pContext)

{

    if (CWnd::OnCreate(lpcs) == -1)

       return -1;

    // create special children first

    if (!OnCreateClient(lpcs, pContext))

    {

       TRACE(traceAppMsg, 0, "Failed to create client pane/view for frame./n");

       return -1;

    }

    // post message for initial message string

    PostMessage(WM_SETMESSAGESTRING, AFX_IDS_IDLEMESSAGE);

    // make sure the child windows have been properly sized

    RecalcLayout();

    return 0;   // create ok

}

BOOL CFrameWnd::OnCreateClient(LPCREATESTRUCT, CCreateContext* pContext)

{

    // default create client will create a view if asked for it

    if (pContext != NULL && pContext->m_pNewViewClass != NULL)

    {

       if (CreateView(pContext, AFX_IDW_PANE_FIRST) == NULL)

           return FALSE;

    }

    return TRUE;

}

CWnd* CFrameWnd::CreateView(CCreateContext* pContext, UINT nID)

{

    ASSERT(m_hWnd != NULL);

    ASSERT(::IsWindow(m_hWnd));

    ASSERT(pContext != NULL);

    ASSERT(pContext->m_pNewViewClass != NULL);

    // Note: can be a CWnd with PostNcDestroy self cleanup

    CWnd* pView = (CWnd*)pContext->m_pNewViewClass->CreateObject();

    if (pView == NULL)

    {

       TRACE(traceAppMsg, 0, "Warning: Dynamic create of view type %hs failed./n",

           pContext->m_pNewViewClass->m_lpszClassName);

       return NULL;

    }

    ASSERT_KINDOF(CWnd, pView);

    // views are always created with a border!

    if (!pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,

       CRect(0,0,0,0), this, nID, pContext))

    {

       TRACE(traceAppMsg, 0, "Warning: could not create view for frame./n");

       return NULL;        // can't continue without a view

    }

    if (pView->GetExStyle() & WS_EX_CLIENTEDGE)

    {

       // remove the 3d style from the frame, since the view is

       //  providing it.

       // make sure to recalc the non-client area

       ModifyStyleEx(WS_EX_CLIENTEDGE, 0, SWP_FRAMECHANGED);

    }

    return pView;

}

6:窗口的现实和刷新(激发paint事件)

pMainFrame->ShowWindow(m_nCmdShow);

    pMainFrame->UpdateWindow();

7:pThread->Run()

// main running routine until thread exits

int CWinThread::Run()

{

    ASSERT_VALID(this);

    _AFX_THREAD_STATE* pState = AfxGetThreadState();

    // for tracking the idle time state

    BOOL bIdle = TRUE;

    LONG lIdleCount = 0;

    // acquire and dispatch messages until a WM_QUIT message is received.

    for (;;)

    {

       // phase1: check to see if we can do idle work

       while (bIdle &&

           !::PeekMessage(&(pState->m_msgCur), NULL, NULL, NULL, PM_NOREMOVE))

       {

           // call OnIdle while in bIdle state

           if (!OnIdle(lIdleCount++))

              bIdle = FALSE; // assume "no idle" state

       }

       // phase2: pump messages while available

       do

       {

           // pump message, but quit on WM_QUIT

           if (!PumpMessage())

              return ExitInstance();

           // reset "no idle" state after pumping "normal" message

           //if (IsIdleMessage(&m_msgCur))

           if (IsIdleMessage(&(pState->m_msgCur)))

           {

              bIdle = TRUE;

              lIdleCount = 0;

           }

       } while (::PeekMessage(&(pState->m_msgCur), NULL, NULL, NULL, PM_NOREMOVE));

    }

}

BOOL CWinThread::PumpMessage()

{

  return AfxInternalPumpMessage();

}

BOOL AFXAPI AfxInternalPumpMessage()

{

    _AFX_THREAD_STATE *pState = AfxGetThreadState();

    if (!::GetMessage(&(pState->m_msgCur), NULL, NULL, NULL))

    {

#ifdef _DEBUG

       TRACE(traceAppMsg, 1, "CWinThread::PumpMessage - Received WM_QUIT./n");

           pState->m_nDisablePumpCount++; // application must die

#endif

       // Note: prevents calling message loop things in 'ExitInstance'

       // will never be decremented

       return FALSE;

    }

#ifdef _DEBUG

  if (pState->m_nDisablePumpCount != 0)

    {

      TRACE(traceAppMsg, 0, "Error: CWinThread::PumpMessage called when not permitted./n");

      ASSERT(FALSE);

    }

#endif

#ifdef _DEBUG

    _AfxTraceMsg(_T("PumpMessage"), &(pState->m_msgCur));

#endif

  // process this message

    if (pState->m_msgCur.message != WM_KICKIDLE && !AfxPreTranslateMessage(&(pState->m_msgCur)))

    {

      ::TranslateMessage(&(pState->m_msgCur));

      ::DispatchMessage(&(pState->m_msgCur));

    }

  return TRUE;

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值