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

worldblog發表於2008-01-21
淺談 MFC 的子類化機制和該機制的一個應用(2) (轉)[@more@]

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

 :namespace prefix = o ns = "urn:schemas--com::office" />

BOOL CDumpMsgBox::OnDumpOut(LPSTR pszDumpBuffer, UINT nBufferSize)

{

  ASSERT(pszDumpBuffer != NULL && nBufferSize != 0);

  ASSERT(AfxIsValidString(pszDumpBuffer, nBufferSize));

 

  // calculate 'n' occur times in buffer.

  int nPosition, nLine = 0;

  for(nPosition = 0; nPosition < (int)nBufferSize; nPosition ++)

  {

  if (pszDumpBuffer[nPosition] == '')

  break;

  if (pszDumpBuffer[nPosition] == 'n')

  nLine ++;

  }

 

  // alloc enough memory for convert.

  TCHAR *pszBuffer = (TCHAR*)malloc(sizeof(TCHAR) * (nPosition + nLine + 64));

 

  // convert all 'n' 'r' or 'nr' to 'rn':

  TCHAR *pszConvert = pszBuffer;

  for (nPosition = 0; nPosition < (int)nBufferSize; nPosition ++)

  {

  if (pszDumpBuffer[nPosition] == '')

  break;

  if (pszDumpBuffer[nPosition] == 'r')

  continue;

  if (pszDumpBuffer[nPosition] == 'n')

  {

  *pszConvert = _T('r');

  pszConvert ++;

  }

  *pszConvert = (TCHAR)pszDumpBuffer[nPosition];

  pszConvert ++;

  }

 

  // set end of string.

  *pszConvert = '';

 

  // set converted dump buffer to the edit control.

  m_editDump.AssertValid();

  m_editDump.SetWindowText(pszBuffer);

 

  free(pszBuffer);

  return TRUE;

}

 

int CDumpMsgBox::essageBox(UINT nIDPrompt, UINT nType, UINT nIDHelp)

{

  CString string;

  if (!string.LoadString(nIDPrompt))

  {

  TRACE1("CDumpMsgBox::DoMessageBox error: failed to load message box prompt string 0x%04x.n",

  nIDPrompt);

  ASSERT(FALSE);

  }

  if (nIDHelp == (UINT)-1)

  nIDHelp = nIDPrompt;

  return DoMessageBox(string, nType, nIDHelp);

}

 

int CDumpMsgBox::DoMessageBox(LPCTSTR lpszText, UINT nType, UINT nIDHelp)

{

  int nRetCode;

 

  // not call PreCreateWindow to initialize CREATESTRUCT data.

  AfxHookWindowCreate(this);

  nRetCode = AfxMessageBox(lpszText, nType, nIDHelp);

 

  if (!AfxUnhookWindowCreate())

  PostNcDestroy();

 

  return nRetCode;

}

 

void CDumpMsgBox::DoDump(CObject* pDumpObject)

{

  m_fileDump.AssertValid();  // dump target must exist.

 

  ASSERT(pDumpObject != NULL);

  CRuntimeClass* pClass = pDumpObject->GetRuntimeClass();

 

  if (pClass == NULL)

  return;  // unexpect runtime-class value.

 

  if (AfxIsValidAddress(pDumpObject, pClass->m_nObjectSize, TRUE))

  {

#ifdef _DE

  pDumpObject->Dump(m_dumpContext);

#else // ! _DEBUG

  m_dumpContext << "a " << pClass->m_lpszClassName <<

  " at " << (void*)pDumpObject;

  m_dumpContext << "nHex Dumps: " << pClass->m_nObjectSize <

  m_dumpContext.HexDump(_T("HEX"), (BYTE*)pDumpObject, pClass->m_nObjectSize, 8);

#endif  // _DEBUG

  }

}

 

#ifdef _DEBUG

void CDumpMsgBox::AssertValid() const

{

  CWnd::AssertValid();

 

  if (m_editDump.GetSafeHwnd())

  m_editDump.AssertValid();

 

  ASSERT(AfxIsValidAddress(&m_dumpContext, sizeof(CDumpContext), TRUE));

 

  m_fileDump.AssertValid();

}

 

void CDumpMsgBox::Dump(CDumpContext& dc) const

{

  if (m_hWnd == NULL)

  {

  CObject::Dump(dc);

  dc << "m_hWnd = NULLn";

  }

  else

  CWnd::Dump(dc);

 

  if (0 < dc.GetDepth())

  {

  dc << "include ";

  if (NULL == m_editDump.GetSafeHwnd())

  {

  m_editDump.CObject::Dump(dc);

  dc << "m_hWnd = NULLn";

  }

  else

  m_editDump.Dump(dc);

 

  dc << "include ";

  m_fileDump.Dump(dc);

  }

}

#endif  // _DEBUG

 

程式碼較為簡單,為 MessageBox 增加了一個 Edit Control ,屬性為 ES_MULTILINE ,ES_READONLY, ES_AUTOVSCROLL,並且擁有 WS_EX_CLIENTEDGE 屬性。

 

該 Edit Control 的內容透過 CMemFile 和 CDumpContext 生成。構造 CDumpContext( CFile* pFile ) 可以生成一個以 CMemFile 為目標的 CDumpContext 例項,然後 CObject::Dump,最後透過 CMemFile::Detach 得到包含輸出字串的緩衝區。

 

以下是 CDumpMsgBox 的應用示例:

 

CDumpMsgBox msgBox;  // declare intsance

 

msgBox.DoDumpObject(&msgBox);  // dump itself

 

msgBox.DoMessageBox(_T("This message box used class CDumpMsgBox."), MB_OKCANCEL);  // display

 

以上程式碼在 MS Visual C++ 6.0, 下透過。

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-998025/,如需轉載,請註明出處,否則將追究法律責任。

相關文章