www.gusucode.com > VC++将对话框做为MDI子窗口的示例程序-源码程序 > VC++将对话框做为MDI子窗口的示例程序-源码程序/code/MDIDialog.cpp
// MDIDialog.cpp : implementation file // Download by http://www.NewXing.com #include "stdafx.h" #include "TSML.h" #include "MDIDialog.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMDIDialog dialog CMDIDialog::CMDIDialog(LPCTSTR lpszTemplateName,CWnd* pParent/* = NULL*/) : CDialog(lpszTemplateName, pParent) { bInserted=0; } CMDIDialog::CMDIDialog(UINT nIDTemplate,CWnd* pParent /*= NULL*/) : CDialog(nIDTemplate, pParent) { //{{AFX_DATA_INIT(CMDIDialog) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT bInserted=0; } void CMDIDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMDIDialog) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CMDIDialog, CDialog) //{{AFX_MSG_MAP(CMDIDialog) ON_WM_DESTROY() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMDIDialog message handlers void CMDIDialog::OnCancel() { if(bInserted) DestroyWindow(); // CDialog::OnCancel();//已销毁,不能再销毁 } void CMDIDialog::OnOK() { if(bInserted) DestroyWindow(); // CDialog::OnOK();//已销毁,不能再销毁 } void CMDIDialog::OnDestroy() { CDialog::OnDestroy(); // TODO: Add your message handler code here if(bInserted) delete(this); } void CMDIDialog::DoInsert() { CMDIFrameWnd *pWnd=(CMDIFrameWnd *) ::AfxGetMainWnd(); CWnd *pChildWnd = FromHandle(pWnd->m_hWndMDIClient); bInserted=1; Create(IDD_DIALOG_CHILD,NULL); SetParent(pChildWnd); if(GetStyle()&DS_CENTER) CenterWindow(); SetFocus();//OK ShowWindow(SW_SHOW); UpdateWindow(); } void CMDIDialog::CenterWindow(CWnd *pAlternateOwner) { CWnd* pWnd = ::AfxGetMainWnd(); CRect rect; int nx, ny; if ( bInserted ) if ( pAlternateOwner != NULL )//如非空,则有参照窗,直接做可也 CDialog::CenterWindow(pAlternateOwner); else { pWnd = AfxGetMainWnd(); pWnd -> GetClientRect(&rect); nx = rect.Width() / 2; ny = rect.Height() / 2; GetWindowRect(&rect); nx -= rect.Width()/2; ny -= rect.Height()/2; if ( nx <0 ) { nx=0; ny /=2; } if ( ny <0 ) { ny=0; nx/=2; } SetWindowPos(NULL, nx, ny, 0, 0, SWP_NOSIZE); } else CDialog::CenterWindow(pAlternateOwner); } BOOL CMDIDialog::IsWndInserted() { return bInserted; }