Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How Dynamically Change Dialog Style Between CHILD & POPUP?

1,297 views
Skip to first unread message

Al Koch

unread,
Apr 23, 2002, 6:10:57 PM4/23/02
to
Hello,

I need to be able to change a dialog box's style between WS_CHILD and
(WS_POPUP | WS_DLGFRAME) at run time. (The dialog box is stored in the
resource as WS_CHILD so I can place it in a splitter pane and this is
working fine but I now need to also display the dialog box as a *MODAL*
Popup). I cannot seem to find a way to do this.

Here are some important items to keep in mind:
1) When the dialog box style is to be (WS_POPUP | WS_DLGFRAME) the dialog
must be created as *MODAL*.
2) PreCreateWindow() is not called in the creation of a dialog box.
3) CDialog::Create() is not called in the creation of a *MODAL* dialog box.

I believe that either CDialog::ModifyStyle() or the WinSDK ::SetWindowLong()
can be used to actually change the style bits, but the trick seems to be in
changing the style at the *RIGHT TIME* because:
1) This can't be done in PreCreateWindow() since it is never called.
2) It can't be done in Create() since that isn't called for a *MODAL*
dialog.
3) Changing the style in OnInitDialog() doesn't have any effect.

Searches of numerous support sites have revealed that a half dozen or so
people over the last few years have needed to do this but I haven't found a
single answer that works for the specific situation I have.

So, my questions are:
1) Can the style can be changed from WS_CHILD to (WS_POPUP | WS_DLGFRAME)?
The MSDN docs on Windows styles makes the comment that some styles can't be
changed after the "control has been created".
2) If it is possible to make this style change, how/*WHERE* is this done?

Thanks,
Al Koch
alkoch@subDimension_NO_SPAM_.com

Jeff Partch

unread,
Apr 23, 2002, 8:43:46 PM4/23/02
to
Hi!

I Haven't fully tested it, but would something like this work for you?...

#include <..\src\afximpl.h> // For DLGTEMPLATEEX
BOOL
PrepareDlgTemplate(
IN CONST LPCTSTR lpszResource,
IN CONST BOOL bChild
)
{
BOOL bRet = FALSE;
if (lpszResource)
{
if (HINSTANCE hInst = AfxFindResourceHandle(
lpszResource, RT_DIALOG))
{
if (HRSRC hResource = ::FindResource(
hInst, lpszResource, RT_DIALOG))
{
if (HGLOBAL hTemplate = LoadResource(
hInst, hResource))
{
union {
VOID* Void;
DLGTEMPLATE* Template;
DLGTEMPLATEEX* TemplateEx;
} p = { LockResource(hTemplate) };

if (p.Void)
{
DWORD dwSize = 0, *pdwStyle = NULL;
if (p.TemplateEx->signature == 0xFFFF)
{
dwSize = sizeof(DLGTEMPLATEEX);
pdwStyle = &p.TemplateEx->style;
}
else
{
dwSize = sizeof(DLGTEMPLATE);
pdwStyle = &p.Template->style;
}

if (pdwStyle && dwSize)
{
DWORD dwOldProtect;
if ((bRet = VirtualProtect(p.Void, dwSize,
PAGE_READWRITE, &dwOldProtect)))
{
if (bChild)
*pdwStyle |= WS_CHILD;
else
*pdwStyle &= ~WS_CHILD;

VirtualProtect(p.Void, dwSize,
dwOldProtect, &dwOldProtect);
}
}
}
}
}
}
}
return bRet;
}

Anyway...

HTH,

--
Jeff Partch


"Al Koch" <alkoch@subDimension-NO-SPAM-.com> wrote in message news:e1l5pOx6BHA.2516@tkmsftngp04...

Al Koch

unread,
Apr 24, 2002, 1:07:45 PM4/24/02
to
Hi Jeff,

Thanks very much for the detailed suggestion. In your code you have shown
how to load the dialog template and alter the style in memory. If I was
creating a modeless dialog box I could then use this modified dialog
template with CreateIndirect() and all would be well.

My problem , however, is that I need to create a *MODAL* dialog box. MFC
doesn't (appear to) offer anyway to create a *MODAL* dialog box by
specifying a modified template as you've described. The problem seems to be
that in an attempt to do all the hard work, MFC "hides" the actual creation
of the dialog box when it is *MODAL*. That is, the creation process in MFC
is:

CMyDialog MyDialog(IDD_MYDIALOG,pParent);
MyDialog.DoModal();

Since this sequence doesn't (seem to) offer any overrides where I can alter
the template, I am stumped. Any ideas?

Thanks,
Al
alk...@subDimensionREMOVEALLTHESECHARS.com


"Jeff Partch" <airb...@airmail.net> wrote in message
news:uEB$6jy6BHA.2268@tkmsftngp04...

Jeff Partch

unread,
Apr 24, 2002, 1:17:53 PM4/24/02
to
Hi!

Just so there is no misunderstanding, the point of the
sample was to modify *the* resource template, not a copy
an not to provide you with something you could pass to one
of the indirect dialog functions. No doubt you'll want to
modify the style bits more than I have shown, but calling
the function to set the WS_CHILD style will prepare the
template for a subsequent call to CDialog::Create, and
likewise calling it to remove the WS_CHILD style will
prepare it for CDialog::DoModal. Or that's my plan anyway.
Make sense?

Jeff...

>.
>

Jeff Partch

unread,
Apr 24, 2002, 11:19:40 PM4/24/02
to
Oh, and just as an FYI follow-up, Al -- take a look at CDialog::InitModalIndirect.

--
Jeff Partch


"Jeff Partch" <airb...@airmail.net> wrote in message

news:5f5001c1ebb3$f7c83110$9ee62ecf@tkmsftngxa05...

Al Koch

unread,
Apr 26, 2002, 2:01:45 PM4/26/02
to
Hi Jeff,

I sent a reply to the Group yesterday but it is no where in sight so I'll
try again.

I really blew it by not seeing the InitModalIndirect() - I have no idea how
I missed that but it was exactly what I was looking for. I already knew how
to change the style but I couldn't see how to "interrupt" the process of
creating a modal dialog box to make the change. That, of course, is what
InitModalIndirect() is all about!

Just, FYI, I had tried to go in "the other direction" and start with a
template that had been originally saved as Popup and during the creation of
the dialog as a modeless dialog box change the style to Child. This seemed
to "almost" work. I could get all of the style bits changed except 0800
0000 which is WS_CLIPSIBLINGS. I don't know why that was showing as being
set since it wasn't in the dialog box template in the .rc file but it always
showed up along with WS_POPUP. The problem is that this particular bit
would not go away, even when set with SetWindowLong(). Your suggestion
about InitModalIndirect() came along at just the right time! Now I can
start with a WS_CHILD dialog and convert it to WS_POPUP.

Thanks again for your help.

Regards,
Al Koch
alk...@subDimensionREMOVEALLTHESECHARS.com

"Jeff Partch" <airb...@airmail.net> wrote in message

news:#clZKgA7BHA.2080@tkmsftngp02...

Jeff Partch

unread,
Apr 27, 2002, 12:28:52 AM4/27/02
to
Thanks, Al! Glad you got it working.

--
Jeff Partch


"Al Koch" <alkoch@subDimension-NO-SPAM-.com> wrote in message news:ew3X7xU7BHA.2232@tkmsftngp05...

0 new messages