-
April 10th, 2016, 08:35 PM
#1
-
April 15th, 2016, 12:35 PM
#2
Re: Mystery of the Modeless Dialog
Out of curiosity, why do you name your local variable m_pmodeless?
The 'm_p' prefix implies a class member field that is a pointer.
Is this code within a class that contains a m_pmodeless field? If so, is the value getting stored in a class field?
Some folks get a little upset when naming conventions are mentioned, but following them often prevents these types of issues.
-
May 31st, 2016, 10:33 AM
#3
Re: Mystery of the Modeless Dialog
It has no much sense to create a modeless dialog either using a locally defined smart pointer or instantiate a local object. In both cases it will be destroyed when goes out of function scope. Moreover you'll get (at least in the last MFC versions) a warning from the CDialog's destructor:
Code:
... \dlgcore.cpp(136) : AppMsg - Warning: calling DestroyWindow in CDialog::~CDialog
... \dlgcore.cpp(137) : AppMsg - OnDestroy or PostNcDestroy in derived class will not be called.
That's to avoid wondering/having headaches if somehow handle WM_DESTROY and/or override PostNcDestroy in the CDialog-derived class, write some cool code in there and nothing happens.
Just put a member of child dialog type in its parent class then create it first time it's necessary to be shown.
Something like in the next example:
Code:
class CParentDialog : public CDialog
{
CChildDialog m_dlgChild;
// ...
};
Code:
void CParentDialog::ShowModelessDialog()
{
if (! ::IsWindow(m_dlgChild.GetSafeHwnd()))
m_dlgChild.Create(CMyDialog::IDD, this);
m_dlgChild.ShowWindow(SW_SHOW);
}
void CParentDialog::HideModelessDialog()
{
if (::IsWindow(m_dlgChild.GetSafeHwnd()))
m_dlgChild.ShowWindow(SW_HIDE);
}
Nothing more. Further, the parent will take care to cleanly destroy its children when it's being destroyed itself.
An aditional note, completing Arjay's post: a smart pointer defined as above is in fact an object so, to increase the code readability and avoid any confusion, you can prefix it with "sp" (if it's locally defined), or with "m_sp" (if it's a class member).
Last edited by ovidiucucu; May 31st, 2016 at 10:54 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|