I am making a doc/view arch SDI application.
I invoke a COptionsDialog
in CSquaresView
.
void CSquaresView::OnOptions()
{
COptionsDialog dlg(this);
if (dlg.DoModal() == IDOK)
...
}
In COptionsDialog
I want to access CSquaresView
.
BOOL COptionsDialog::OnInitDialog()
{
CDialog::OnInitDialog();
CWnd *pParent = GetParent();
if (pParent) {
CSquaresView *pView = dynamic_cast<CSquaresView*>(pParent); //pView is always NULL
if (pView != NULL)
{
CSquaresDoc* pDoc = pView->GetDocument();
...
}
But I always get pView
as NULL
;
Please help me to solve this problem.
The observed behavior makes sense. A (modal) dialog's owner must be
an overlapped or pop-up window [...]; a child window cannot be an owner window.
CView
-derived class instances generally are child windows. As such they cannot be the owner of a (modal) dialog. When you pass a child window into the c'tor of a CDialog
-derived class, the system walks up the window hierarchy until it finds an overlapped or pop-up window, and uses that as the owner of the dialog. Regardless of whether you then call GetParent
, GetAncestor
, or CWnd::GetOwner
, it is this true owner (usually your CFrameWnd
-derived implementation) where window traversal starts.
Thus, you cannot generally use standard window traversal to find the window passed into a (modal) dialog's constructor. However, MFC records the CWnd
(-derived) class instance you pass into your COptionsDialog
constructor and stores it in a protected
member variable m_pParentWnd
, inherited from the CDialog
class.
As long as COptionsDialog
derives public
/protected
from CDialog
or CDialogEx
, the implementation can access this class member.
The following OnInitDialog
implementation will do what you're looking for:
BOOL COptionsDialog::OnInitDialog()
{
CDialog::OnInitDialog();
CSquaresView *pView = dynamic_cast<CSquaresView*>(m_pParentWnd);
if (pView != NULL)
{
CSquaresDoc* pDoc = pView->GetDocument();
...
}
There are other options available. For example, you could supply a COptionsDialog
constructor that takes both a CWnd*
and a CSquaresDoc*
, delegating the first onto the base class c'tor and storing the document pointer in a (private
) class member. This makes for code that's easier to follow in that it explicitly spells out, that the dialog depends on the document.