[Effective WX] 理解wxWindow中增加和去除/销毁子窗口的过程
2012-08-15 10:02
288 查看
wxWindow中增加和去除/销毁子窗口的过程:
a) 增加一个子窗口
b) 销毁所有的子窗口
上面直接delete一个子窗口的指针。它会触发子窗口的析构。
c) wxWindows的reparent
如果没有指定parent,那么就将这个窗口添加到全局的toplevel windows列表中。
a) 增加一个子窗口
void wxWindowBase::AddChild(wxWindowBase *child) { wxCHECK_RET( child, wxT("can't add a NULL child") ); // this should never happen and it will lead to a crash later if it does // because RemoveChild() will remove only one node from the children list // and the other(s) one(s) will be left with dangling pointers in them wxASSERT_MSG( !GetChildren().Find((wxWindow*)child), _T("AddChild() called twice") ); GetChildren().Append((wxWindow*)child); child->SetParent(this); }
b) 销毁所有的子窗口
bool wxWindowBase::DestroyChildren() { wxWindowList::compatibility_iterator node; for ( ;; ) { // we iterate until the list becomes empty node = GetChildren().GetFirst(); if ( !node ) break; wxWindow *child = node->GetData(); // note that we really want to call delete and not ->Destroy() here // because we want to delete the child immediately, before we are // deleted, and delayed deletion would result in problems as our (top // level) child could outlive its parent delete child; wxASSERT_MSG( !GetChildren().Find(child), wxT("child didn't remove itself using RemoveChild()") ); } return true; }
上面直接delete一个子窗口的指针。它会触发子窗口的析构。
// reset the dangling pointer our parent window may keep to us if ( m_parent ) { m_parent->RemoveChild(this); }析构过程中会调用父窗口的RemoveChild函数,用来去除该子窗口,从父窗口的子窗口链表中。
void wxWindowBase::RemoveChild(wxWindowBase *child) { wxCHECK_RET( child, wxT("can't remove a NULL child") ); GetChildren().DeleteObject((wxWindow *)child); child->SetParent(NULL); }
c) wxWindows的reparent
如果没有指定parent,那么就将这个窗口添加到全局的toplevel windows列表中。
bool wxWindowBase::Reparent(wxWindowBase *newParent) { wxWindow *oldParent = GetParent(); if ( newParent == oldParent ) { // nothing done return false; } // unlink this window from the existing parent. if ( oldParent ) { oldParent->RemoveChild(this); } else { wxTopLevelWindows.DeleteObject((wxWindow *)this); } // add it to the new one if ( newParent ) { newParent->AddChild(this); } else { wxTopLevelWindows.Append((wxWindow *)this); } return true; }
相关文章推荐
- MFC窗口销毁过程
- MFC中窗口的销毁过程
- MFC窗口销毁过程
- MFC窗口销毁过程
- vc对话框窗口销毁过程
- 基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南二——理解消息循环和窗口过程
- 总结MFC窗口销毁过程
- MFC 窗口销毁过程
- 【转】MFC窗口销毁过程
- MFC 窗口过程理解
- MFC窗口销毁过程
- 总结MFC窗口销毁过程
- MFC窗口销毁过程
- MFC窗口销毁过程
- MFC窗口销毁过程
- MFC应用程序消息处理及其窗口创建和销毁过程函数调用顺序
- MFC应用程序消息处理及其窗口创建和销毁过程函数调用顺序
- [Effective WX] 理解wxWindow与wxSizer的关系
- [Effective WX] 理解wxWindow中的各种size
- MFC应用程序消息处理及其窗口创建和销毁过程函数调用顺序