CFrameWnd作为子窗口在CControlBar中创建的问题

duron1000 2007-03-07 11:36:01
TestControlBar.h

CFrameWnd m_wndFrame;

////////////
TestControlBar.cpp

int CTestControlBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CControlBar::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here

if(this->m_wndFrame.Create("BUTTON","Frame",WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),this,NULL,0,NULL)!=TRUE)
{
AfxMessageBox("Create frame error!");
return 0;
}
this->SendMessage(WM_SIZE,0,0);

return 0;
}

void CTestControlBar::OnSize(UINT nType, int cx, int cy)
{
CControlBar::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
if(this->m_wndFrame.m_hWnd )
{
CRect rc;
this->GetClientRect(&rc);
this->m_wndFrame.MoveWindow(rc,TRUE);
}
}

////////////
程序可正常显示m_wndFrame,但是程序退出时出错,调试了一下,可能是销毁m_wndFrame时出错了,但未找到确切的原因.CFrameWnd作为子窗口创建时,是不是要做某些特殊设置.我之所以将CFrameWnd作为CControlBar的子窗口创建,是想通过CFrameWnd使CControlBar看起来切分成2个视(在CFrameWnd::OnCreateClient中使用CSplitterWnd切分).当然,如果有直接的方法将CControlbar切分成2个视,那更好了.
...全文
468 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xjwchy 2010-03-18
  • 打赏
  • 举报
回复
我也找到原因了,谢谢啊
duron1000 2007-03-08
  • 打赏
  • 举报
回复
试了一下,将m_wndFrame定义为指针,就不会出错了.CrazyAzreal,你说的"你的m_wndFrame要定义为指针,释放的时候,只要DestroyWindow就行了,不要delete,",释放指的是在m_wndFrame的父窗口中释放吗?
如果m_wndFrame不定义为指针,则要按照jiangsheng说的改一下OnPostNCDestroy.
duron1000 2007-03-08
  • 打赏
  • 举报
回复
to CrazyAzreal:

我将m_wndFrame定义为指针试试看.
duron1000 2007-03-08
  • 打赏
  • 举报
回复
to jiangsheng:

我这样修改了一下
void CTestFrameWnd::PostNcDestroy()
{
// TODO: Add your specialized code here and/or call the base class
CWnd::PostNcDestroy();
//CFrameWnd::PostNcDestroy();
}
不会出错了.

////////////
To yjgx007:

应该是
void CTestControlBar::OnDestroy()
{
if (::IsWindow(m_wndFrame.GetSafeHwnd()))
m_wndFrame.DestroyWindow();
CControlBar::OnDestroy();
}
吧.试了一下,好象还是出错.
/////////////////////////
CrazyAzreal 2007-03-08
  • 打赏
  • 举报
回复
jiangsheng(蒋晟.Net[MVP]) 强。。`你是没有看程序的吧?
如jiangsheng说的``CFrameWnd在PostNcDestroy有一句delete this,所以,你的wndFrame要定义为指针,释放的时候,只要DestroyWindow就行了,不要delete,还有就是你要在对话框的构造里将wndFrame初始为NULL,在OnSize里,MoveWindow时,先判断wndFrame是否不为NULL`
duron1000 2007-03-08
  • 打赏
  • 举报
回复
试试看
duron1000 2007-03-08
  • 打赏
  • 举报
回复
多谢几位老大了.
蒋晟 2007-03-08
  • 打赏
  • 举报
回复
多看看微软的MFC、ATL和.Net框架的源代码,对自己写程序有好处
duron1000 2007-03-07
  • 打赏
  • 举报
回复
if(this->m_wndFrame.Create("BUTTON","Frame",WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),this,NULL,0,NULL)!=TRUE)
写错了,一句应改为
if(this->m_wndFrame.Create(NULL,"Frame",WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),this,NULL,0,NULL)!=TRUE)
yjgx007 2007-03-07
  • 打赏
  • 举报
回复
void CTestControlBar::OnDestroy()
{
if (::IsWindow(m_wndFrame.GetSafeHwnd()))
m_wndFrame.DestroyWindow();
CTestControlBar::OnDestroy();
}
蒋晟 2007-03-07
  • 打赏
  • 举报
回复
框架在销毁时默认是要delete this的,你把它作为栈上的成员变量来用的话会造成出错
你应该在堆上创建这个对象,或者重载OnPostNCDestroy,调用CWnd而不是CFrameWnd的基类实现。

15,976

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧