博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
浅谈 MFC 的子类化机制和该机制的一个应用(1) (转)
阅读量:2500 次
发布时间:2019-05-11

本文共 5287 字,大约阅读时间需要 17 分钟。

浅谈 MFC 的子类化机制和该机制的一个应用(1) (转)[@more@]

浅谈 MFC 的子类化机制和该机制的一个应用

众所周知:

afx_msg int CWnd::OnCreate( LPCREATESTRUCT lpCreateStruct );:namespace prefix = o ns = "urn:schemas--com::office" />

是一个经常被重载的 MFC 窗体,他负责处理窗体的 WM_CREATE 消息,这个消息的发送时机在窗体刚刚创建以后,CreateWindow(Ex) 返回之前。

 

可以发现在 MFC 里,和对话框也可以得到这个消息,例如 CEdit,CPrintDialog, CFileDialog,他们内部CreateWindowEx,PrintDlg,GetOpen(Save)FileName 完全掩盖了窗体创建的过程,这些函数返回时,窗体已经收到过 WM_CREATE 消息而且不会得到第二次通知。

 

因此,为了得到这些窗体的 WM_CREATE 通知,必须采用有点特殊的方法,能够在CreateWindowEx 返回之前就替换掉窗体的 WindowProc。

 

WH_C钩子是不错的选择。当一个窗体产生,CBTProc 会在WindowProc 收到 WM_CREATE之前得到 HCBT_CREATEWND 通知,如果此时子类化窗体,就能在子类化后的窗体过程中得到 WM_CREATE 通知。

 

MFC 的做法和这类似:

void AfxHookWindowCreate( CWnd *pWnd );

负责 WH_CBT 钩子,其参数 pWnd 指向一个创建中的 CWnd 实例,MFC 通过某种全局变量把这个实例的指针传给中的 CBTProc。

BOOL AfxUnhookWindowCreate();

它卸下 WH_CBT 钩子,并且复原 AfxHookWindowCreate 改变过的 MFC 全局状态 消息。

LRESULT CALLBACK _AfxCbtFilterHook(int code, WPARAM wParam, LPARAM lParam);

这个就是 MFC 安装的 CBTProc 回调。他只处理 HCBT_CREATEWND 通知,然后Attach 之前得到的 CWnd 实例到正在创建的窗体句柄、替换掉窗体的 WindowProc,最后 CallNextHookEx。

 

现在来考虑如何应用MFC给我们提供的这个便利。AfxHookWindowCreate 和AfxUnhookWindowCreate 之间创建的第一个非 IME(输入法)窗体可以被所给的 CWnd 实例子类化。我们可以这样调用一些 ,把自己的 CWnd 实例与 API 创建的窗体连结起来,如果我给的是一个 CWnd 派生类的实例,重载过的消息就可以改变原有窗体的行为。以下的代码示例如何按照这样的思路创建一个带 Dump 输出的 MessageBox:

 

ectratio="t">

CSDN_Dev_Image_a%20href=2003-7-31420440.png" o:title="DumpMsgBox">

 

class CDumpMsgBox : public CWnd 

{

  DECLARE_DYNAMIC(CDumpMsgBox)

 

// Constructors

public:

  CDumpMsgBox();

 

// Attributes

public:

  CEdit m_editDump;  // Dum edit control added to the message box.

 

  CMemFile m_fileDump;  // Dumping context's target file.

  CDumpContext m_dumpContext;  // The dump context .

 

// Operations

public:

  int essageBox(UINT nIDPrompt, UINT nType = MB_OK, UINT nIDHelp = (UINT)-1);

 

  AFX_INLINE CDumpContext& GetDumpContext()  {  return m_dumpContext; };

  AFX_INLINE void RemoveAll()  {  m_fileDump.SetLength(0);  };

 

// Implementations

public:

  virtual ~CDumpMsgBox();

  virtual void DoDumpObject(CObject* pDumpObject);

  virtual int DoMessageBox(LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0);

 

#ifdef _DE

  virtual void AssertValid() const;

  virtual void Dump(CDumpContext& dc) const;

#endif

 

protected:

  virtual BOOL OnDumpOut(LPSTR pszDumpBuffer, UINT nBufferSize);

 

// Overrs

  // ClassWizard generated virtual function overrides

  //{

{AFX_VIRTUAL(CChildFrame)

  //}}AFX_VIRTUAL

 

// Generated message map functions

protected:

  //{

{AFX_MSG(CMainFrame)

  afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

  // NOTE - the ClassWizard will add and remove member functions here.

  //  DO NOT EDIT what you see in these blocks of generated code!

  //}}AFX_MSG

  DECLARE_MESSAGE_MAP()

};

 

// Identify of the edit control CDumpMsgBox::m_editDump.

#define IDC_DUMPMSGBOX_EDITBOX  1047

 

//

// CDumpMsgBox

 

IMPLEMENT_DYNAMIC(CDumpMsgBox, CWnd)

 

BEGIN_MESSAGE_MAP(CDumpMsgBox, CWnd)

  //{

{AFX_MSG_MAP(CWnd)

  // NOTE - the ClassWizard will add and remove mapping macrhere.

  //  DO NOT EDIT what you see in these blocks of generated code !

  ON_WM_CREATE()

  //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

// CDumpMsgBox construction/destruction

 

CDumpMsgBox::CDumpMsgBox() : m_fileDump(512),

   m_dumpContext(&m_fileDump)

{

  m_fileDump.AssertValid();

 

  m_dumpContext.SetDepth(1);

  m_dumpContext << "Dump From Objects: ======================================== ";

}

 

CDumpMsgBox::~CDumpMsgBox()

{

  BYTE* pszDumpBuffer = (BYTE*)m_fileDump.Detach();

 

  if (pszDumpBuffer)

  free(pszDumpBuffer);

 

  m_dumpContext.m_pFile = NULL;

}

 

// CDumpMsgBox Implementations

 

int CDumpMsgBox::OnCreate(LPCREATESTRUCT lpcs)

{

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

  {

  TRACE0("CDumpMsgBox::OnCreate error: call CWnd::OnCreate return FALSE. ");

  return -1;

  }

 

  CRect rectBox;

  GetWindowRect(&rectBox);

 

  // adjust message box sizes to fill a dump edit control.

  if (rectBox.Width() < 350)

  rectBox.right = rectBox.left + 350;

  rectBox.bottom += 130;

  MoveWindow(&rectBox, FALSE);

 

  // create edit control to display dump texts

  ScreenToClient(&rectBox);

  CRect rectEdit(rectBox.left + 8, rectBox.bottom - 132,

  rectBox.right - 8, rectBox.bottom - 8);

  if (!m_editDump.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_TABSTOP |

  ES_MULTILINE | ES_READONLY | ES_AUTOVSCROLL,

  rectEdit, this, IDC_DUMPMSGBOX_EDITBOX))

  {

  TRACE0("CDumpMsgBox::OnCreate error: m_editDump.Create return FALSE. ");

  return -1;

  }

 

  // set WS_EX_CLIENTEDGE style to edit control (let it has a drop edge)

  m_editDump.ModifyStyleEx(0L, WS_EX_CLIENTEDGE,

  SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);

 

  // set edit control's font to DEFAULT_GUI_FONT.

  CFont Font;

  Font.Attach((HFONT)GetStockObject(DEFAULT_GUI_FONT));

  m_editDump.SetFont(&Font);

 

  // write end-dump text and flush all data to file

  m_dumpContext << " ======================================== Dump End";

  m_dumpContext.Flush();

 

  m_fileDump.Write("", sizeof(CHAR));

 

  UINT nBufferSize = m_fileDump.GetLength();

  LPSTR pszDumpBuffer = (LPSTR)m_fileDump.Detach();

 

  if (!OnDumpOut(pszDumpBuffer, nBufferSize))

  {

  TRACE0("CDumpMsgBox::OnCreate error: OnDumpOut return FALSE. ");

 

  m_fileDump.Attach((BYTE*)pszDumpBuffer, nBufferSize, 512);

  return -1;

  }

 

  // attach used dump buffer for next dump.

  m_fileDump.Attach((BYTE*)pszDumpBuffer, nBufferSize, 512);

 

  return 0;

}

 

未完,剩余部分请参考 浅谈 MFC 的子类化机制和该机制的一个应用(2)。

 

 

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10748419/viewspace-958419/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10748419/viewspace-958419/

你可能感兴趣的文章
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
MySQL innert join、left join、right join等理解
查看>>
sdc时序约束
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>
docker-daemon.json各配置详解
查看>>
Docker(一)使用阿里云容器镜像服务
查看>>
Docker(三) 构建镜像
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>