创建了一个MDI的非Doc/View应用程序, 以下是四个文件, 两个类CChildFrame CForm// ChildFrm.h : CChildFrame 类的接口
//
#pragma once#include "Form.h"class CChildFrame : public CMDIChildWnd
{
DECLARE_DYNCREATE(CChildFrame)
public:
CChildFrame();
virtual ~CChildFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif// 生成的消息映射函数
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()private:
CForm m_form;
};
// ChildFrm.cpp : CChildFrame 类的实现
//
#include "stdafx.h"
#include "FormViewTest.h"#include "ChildFrm.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CChildFrame
IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd)BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd)
ON_WM_CREATE()
END_MESSAGE_MAP()// CChildFrame 构造/析构
CChildFrame::CChildFrame()
{
}CChildFrame::~CChildFrame()
{
}// CChildFrame 诊断
#ifdef _DEBUG
void CChildFrame::AssertValid() const
{
CMDIChildWnd::AssertValid();
}void CChildFrame::Dump(CDumpContext& dc) const
{
CMDIChildWnd::Dump(dc);
}
#endif //_DEBUG
int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// 创建一个视图以占用框架的工作区
if (!m_form.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
{
TRACE0("未能创建视图窗口\n");
return -1;
}
return 0;
}// Form .h : CForm 类的接口
//#pragma onceclass CForm : public CFormView
{
DECLARE_DYNCREATE(CForm)public:
CForm(); // 动态创建所使用的受保护的构造函数
virtual ~CForm();public:
enum { IDD = IDD_FORM };
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endifprotected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 DECLARE_MESSAGE_MAP()
public:
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
public:
virtual void OnInitialUpdate();// Form.cpp : 实现文件
//
#include "stdafx.h"
#include "FormViewTest.h"
#include "Form.h"
#include ".\form.h"// CForm
IMPLEMENT_DYNCREATE(CForm, CFormView)CForm::CForm()
: CFormView(CForm::IDD)
{
}CForm::~CForm()
{
}void CForm::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
}BEGIN_MESSAGE_MAP(CForm, CFormView)
END_MESSAGE_MAP()// CForm 诊断#ifdef _DEBUG
void CForm::AssertValid() const
{
CFormView::AssertValid();
}void CForm::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
// CForm 消息处理程序BOOL CForm::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
return CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}void CForm::OnInitialUpdate()
{ CFormView::OnInitialUpdate();
// CMDIChildWnd 缺省显示为缺省大小
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
// TODO: 在此添加专用代码和/或调用基类
}
//////////////////////////////////
将其中的m_form, 换成指针有没有问题了. 请问窗口关闭时其窗口类中的成员变量和成员指针变量的销毁是如何处理的, 原则是什么?
//
#pragma once#include "Form.h"class CChildFrame : public CMDIChildWnd
{
DECLARE_DYNCREATE(CChildFrame)
public:
CChildFrame();
virtual ~CChildFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif// 生成的消息映射函数
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()private:
CForm m_form;
};
// ChildFrm.cpp : CChildFrame 类的实现
//
#include "stdafx.h"
#include "FormViewTest.h"#include "ChildFrm.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CChildFrame
IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd)BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd)
ON_WM_CREATE()
END_MESSAGE_MAP()// CChildFrame 构造/析构
CChildFrame::CChildFrame()
{
}CChildFrame::~CChildFrame()
{
}// CChildFrame 诊断
#ifdef _DEBUG
void CChildFrame::AssertValid() const
{
CMDIChildWnd::AssertValid();
}void CChildFrame::Dump(CDumpContext& dc) const
{
CMDIChildWnd::Dump(dc);
}
#endif //_DEBUG
int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// 创建一个视图以占用框架的工作区
if (!m_form.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
{
TRACE0("未能创建视图窗口\n");
return -1;
}
return 0;
}// Form .h : CForm 类的接口
//#pragma onceclass CForm : public CFormView
{
DECLARE_DYNCREATE(CForm)public:
CForm(); // 动态创建所使用的受保护的构造函数
virtual ~CForm();public:
enum { IDD = IDD_FORM };
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endifprotected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 DECLARE_MESSAGE_MAP()
public:
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
public:
virtual void OnInitialUpdate();// Form.cpp : 实现文件
//
#include "stdafx.h"
#include "FormViewTest.h"
#include "Form.h"
#include ".\form.h"// CForm
IMPLEMENT_DYNCREATE(CForm, CFormView)CForm::CForm()
: CFormView(CForm::IDD)
{
}CForm::~CForm()
{
}void CForm::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
}BEGIN_MESSAGE_MAP(CForm, CFormView)
END_MESSAGE_MAP()// CForm 诊断#ifdef _DEBUG
void CForm::AssertValid() const
{
CFormView::AssertValid();
}void CForm::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
// CForm 消息处理程序BOOL CForm::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
return CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}void CForm::OnInitialUpdate()
{ CFormView::OnInitialUpdate();
// CMDIChildWnd 缺省显示为缺省大小
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
// TODO: 在此添加专用代码和/或调用基类
}
//////////////////////////////////
将其中的m_form, 换成指针有没有问题了. 请问窗口关闭时其窗口类中的成员变量和成员指针变量的销毁是如何处理的, 原则是什么?
BOOL CChildFrame::DestroyWindow()
{
// TODO: 在此添加专用代码和/或调用基类
if( m_form.IsWindowVisible() )
{
m_form.DestroyWindow();
}
return CMDIChildWnd::DestroyWindow();
}执行时在m_form.DestroyWindow();出错!
{
m_form.DestroyWindow();
}
BOOL CChildFrame::DestroyWindow()
{
// TODO: 在此添加专用代码和/或调用基类
m_form.DestroyWindow();
return CMDIChildWnd::DestroyWindow();
}错误信息为:
Debug Assertion Failed!
Program:d:\test\cpp\formviewtest\formviewtest\debug\FormViewTest.exe
File: dbgheap.c
Line: 1132Expression: _CrtIsValidHeapPointer(pUserData)....
* If this ASSERT fails, a bad pointer has been passed in. It may be
* totally bogus, or it may have been allocated from another heap.
* The pointer MUST come from the 'local' heap.
*/
_ASSERTE(_CrtIsValidHeapPointer(pUserData));
CForm::CForm()
: CFormView(CForm::IDD)
{
m_hWnd = 0;
}BOOL CChildFrame::DestroyWindow()
{
if( IsWindow(m_form.m_hWnd) )
m_form.DestroyWindow();
return CMDIChildWnd::DestroyWindow();
}CForm 和CChildFrame 析构中无代码
结果一样.请给些代码, 多谢!!!
{
if( m_form.m_hWnd || IsWindow(m_form.m_hWnd) )
m_form.DestroyWindow();
return CMDIChildWnd::DestroyWindow();
}
------------------------------------------------------------------------
That is point.Thanks!