天天看點

淺談 MFC 的子類化機制和該機制的一個應用(1) 淺談 MFC 的子類化機制和該機制的一個應用

淺談

MFC

的子類化機制和該機制的一個應用

衆所周知:

afx_msg int

CWnd::OnCreate( LPCREATESTRUCT lpCreateStruct

);

是一個經常被重載的

窗體函數,他負責處理窗體的

WM_CREATE

消息,這個消息的發送時機在窗體剛剛建立以後,

CreateWindow(Ex)

傳回之前。

可以發現在

裡,系統控件和對話框也可以得到這個消息,例如

CEdit

CPrintDialog

CFileDialog

,他們内部調用

CreateWindowEx

PrintDlg

GetOpen

Save

FileName

完全掩蓋了窗體建立的過程,這些函數傳回時,窗體已經收到過

消息而且不會得到第二次通知。

是以,為了得到這些窗體的

通知,必須采用有點特殊的方法,能夠在

CreateWindowEx

傳回之前就替換掉窗體的

WindowProc

使用 WH_CBT

鈎子是不錯的選擇。當一個窗體産生,

CBTProc

會在

WindowProc

收到

WM_CREATE

之前得到

HCBT_CREATEWND

通知,如果此時子類化窗體,就能在子類化後的窗體過程中得到

通知。

MFC

的做法和這類似:

void

AfxHookWindowCreate( CWnd *pWnd );

負責安裝

WH_CBT

鈎子,其參數

pWnd

指向一個建立中的

CWnd

執行個體,

通過某種全局變量把這個執行個體的指針傳給執行中的

CBTProc

BOOL AfxUnhookWindowCreate();

它卸下

鈎子,并且複原

AfxHookWindowCreate

改變過的

全局狀态

消息。

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

這個就是

安裝的

CBTProc

回調。他隻處理

通知,然後

Attach

之前得到的

執行個體到正在建立的窗體句柄、替換掉窗體的

,最後

CallNextHookEx

現在來考慮如何應用

MFC

給我們提供的這個便利。

AfxHookWindowCreate

AfxUnhookWindowCreate

之間建立的第一個非

IME

(輸入法)窗體可以被所給的

執行個體子類化。我們可以這樣調用一些

API

,把自己的

執行個體與

API

建立的窗體連結起來,如果我給的是一個

派生類的執行個體,重載過的消息就可以改變原有窗體的行為。以下的代碼示例如何按照這樣的思路建立一個帶

Dump

輸出的

MessageBox

class

CDumpMsgBox : public

{

DECLARE_DYNAMIC(CDumpMsgBox)

// Constructors

public:

CDumpMsgBox();

// Attributes

CEdit m_editDump;

// Dumping edit control added to the message box.

CMemFile m_fileDump;

// Dumping context's target file.

CDumpContext m_dumpContext;

// The dump context object.

// Operations

int

DoMessageBox(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

virtual

~CDumpMsgBox();

virtual void

DoDumpObject(CObject* pDumpObject);

virtual

int

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

#ifdef

_DEBUG

AssertValid() const;

Dump(CDumpContext& dc) const

#endif

protected:

BOOL OnDumpOut(LPSTR pszDumpBuffer, UINT nBufferSize);

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CChildFrame)

//}}AFX_VIRTUAL

// Generated message map functions

//{{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 macros here.

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: \r\n========================================\r\n";

}

CDumpMsgBox::~CDumpMsgBox()

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

if

(pszDumpBuffer)

free(pszDumpBuffer);

m_dumpContext.m_pFile = NULL;

// CDumpMsgBox Implementations

CDumpMsgBox::OnCreate(LPCREATESTRUCT lpcs)

if

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

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

-1;

CRect rectBox;

GetWindowRect(&rectBox);

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

(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);

(!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.\n");

// 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 << "\r\n========================================\r\nDump End";

m_dumpContext.Flush();

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

UINT nBufferSize = m_fileDump.GetLength();

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

(!OnDumpOut(pszDumpBuffer, nBufferSize))

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

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

// attach used dump buffer for next dump.

0;

未完,剩餘部分請參考

的子類化機制和該機制的一個應用(

2

)。