class CDumpMsgBox : public CWnd
{
DECLARE_DYNAMIC(CDumpMsgBox)
// Constructors
public:
CDumpMsgBox();
// Attributes
public:
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
public:
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
public:
virtual ~CDumpMsgBox();
virtual void DoDumpObject(CObject* pDumpObject);
virtual int DoMessageBox(LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0);
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
virtual BOOL OnDumpOut(LPSTR pszDumpBuffer, UINT nBufferSize);
// Overrides
// 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 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
int CDumpMsgBox::OnCreate(LPCREATESTRUCT lpcs)
{
if (CWnd::OnCreate(lpcs) == -1)
{
TRACE0("CDumpMsgBox::OnCreate error: call CWnd::OnCreate return FALSE.\n");
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.\n");
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 << "\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();
if (!OnDumpOut(pszDumpBuffer, nBufferSize))
{
TRACE0("CDumpMsgBox::OnCreate error: OnDumpOut return FALSE.\n");
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)。