Problems with default button on a dynamically created dialog 
Author Message
 Problems with default button on a dynamically created dialog

Hi,
I am creating a dialog dynamically and can't get default buttons working
correctly.  I create a button with the BS_DEFPUSHBUTTON style, but it
loses the black, default-button border as soon as the user tabs to
another control.  I've tried calling SetDefID just before calling
RunModalLoop, but that has no effect.  In fact, calling GetDefID, even
immediately after a SetDefID, always returns 0.

What is different between a dialog created from a resource and one
created dynamically?  Or is there just something I might be leaving out
while creating it?

Any help would be appreciated.

John



Sun, 28 Dec 2003 03:58:40 GMT  
 Problems with default button on a dynamically created dialog
Hi,
  BS_DEFPUSHBUTTON   Creates a button that has a heavy black border. The
user can select this button by pressing the ENTER key. This style enables
the user to quickly select the most likely option (the default option).
That is the default behaviour that you will get but if the button has
"FOCUS" however if you tab to another control now the focus shifts to that
control and pressing enter key would not have the same effect. The default
push button in a dialog box is defined to be the button that is pressed
when the user chooses the ENTER key, provided that the input focus is not
on another button in the dialog box. The default push button is visually
distinguished from other buttons by a thick dark border.
If you want to change the default push button using SetDefID make sure you
set the focus to that button so that the default action can take place.

Regards
Chetan N Parmar
Microsoft



Sun, 28 Dec 2003 05:54:42 GMT  
 Problems with default button on a dynamically created dialog

Quote:

> Hi,
> I am creating a dialog dynamically and can't get default buttons working
> correctly.  I create a button with the BS_DEFPUSHBUTTON style, but it
> loses the black, default-button border as soon as the user tabs to
> another control.  I've tried calling SetDefID just before calling
> RunModalLoop, but that has no effect.  In fact, calling GetDefID, even
> immediately after a SetDefID, always returns 0.

> What is different between a dialog created from a resource and one
> created dynamically?  Or is there just something I might be leaving out
> while creating it?

> Any help would be appreciated.

> John

The default button is whichever one has the focus at first. All your code
does is determine which button has the focus when the dialog first appears.
When the user hits tab, it no longer has the focus. Pressing ENTER has the
same effect as clicking on the button that currently has the focus. You
could over-ride PreTranslateMessage() to get the desired result. The
following code makes the ENTER key tab to the next location. It's not what
you want but it may give you some ideas. You could, for example, intercept
the TAB key and convert it to something harmless so the user couldn't tab
off your default button. For completeness, you should also trap the
SHIFT-TAB key.

Hope this helps,

BOOL YourDialog::PreTranslateMessage(MSG* pMsg)
{

        if( pMsg->message == WM_KEYDOWN )
     {
         if( pMsg->wParam == VK_RETURN )
         {
             pMsg->wParam = VK_TAB;  // change return to tab
         }
     }   // else do nothing

        return CDialog::PreTranslateMessage(pMsg);

- Show quoted text -

Quote:
}



Sun, 28 Dec 2003 10:32:57 GMT  
 Problems with default button on a dynamically created dialog
The default button is whichever one has the BS_DEFPUSHBUTTON Button Style.


Quote:

> > Hi,
> > I am creating a dialog dynamically and can't get default buttons working
> > correctly.  I create a button with the BS_DEFPUSHBUTTON style, but it
> > loses the black, default-button border as soon as the user tabs to
> > another control.  I've tried calling SetDefID just before calling
> > RunModalLoop, but that has no effect.  In fact, calling GetDefID, even
> > immediately after a SetDefID, always returns 0.

> > What is different between a dialog created from a resource and one
> > created dynamically?  Or is there just something I might be leaving out
> > while creating it?

> > Any help would be appreciated.

> > John

> The default button is whichever one has the focus at first. All your code
> does is determine which button has the focus when the dialog first
appears.
> When the user hits tab, it no longer has the focus. Pressing ENTER has the
> same effect as clicking on the button that currently has the focus. You
> could over-ride PreTranslateMessage() to get the desired result. The
> following code makes the ENTER key tab to the next location. It's not what
> you want but it may give you some ideas. You could, for example, intercept
> the TAB key and convert it to something harmless so the user couldn't tab
> off your default button. For completeness, you should also trap the
> SHIFT-TAB key.

> Hope this helps,

> BOOL YourDialog::PreTranslateMessage(MSG* pMsg)
> {

> if( pMsg->message == WM_KEYDOWN )
>      {
>          if( pMsg->wParam == VK_RETURN )
>          {
>              pMsg->wParam = VK_TAB;  // change return to tab
>          }
>      }   // else do nothing

> return CDialog::PreTranslateMessage(pMsg);
> }



Sun, 28 Dec 2003 13:56:55 GMT  
 Problems with default button on a dynamically created dialog
Thank you for your response.

If you create a dialog with the resource editor with some buttons on it, one
of which has the default button style, and some other controls -- for example
an edit box control -- when you tab to the edit box control the default button
gets a dark border and if you press ENTER while in the edit box you get an
event for the default button.  This is the behavior I am trying to achieve.
Creating the dialog dynamically, I'm not getting it.

Following are some details on how I'm creating my dialog:
    In the h file for my dialog class I eliminated the line defining IDD
because I have no resource template.
    I changed the constructor for my dialog class to take no parameters
instead of a parameter for a pointer to the parent CWnd.
    I call CreatEx to create the dialog window.
    I create CButton, CEdit, etc. objects as children of my dialog window.
    I then show the dialog window (ShowWindow) and call RunModalLoop.

Does the behavior of the default button rely on a resource template and, if
so, is there some way of faking it out?  Any thoughts?

John Shell

Quote:

> Hi,
>   BS_DEFPUSHBUTTON   Creates a button that has a heavy black border. The
> user can select this button by pressing the ENTER key. This style enables
> the user to quickly select the most likely option (the default option).
> That is the default behaviour that you will get but if the button has
> "FOCUS" however if you tab to another control now the focus shifts to that
> control and pressing enter key would not have the same effect. The default
> push button in a dialog box is defined to be the button that is pressed
> when the user chooses the ENTER key, provided that the input focus is not
> on another button in the dialog box. The default push button is visually
> distinguished from other buttons by a thick dark border.
> If you want to change the default push button using SetDefID make sure you
> set the focus to that button so that the default action can take place.

> Regards
> Chetan N Parmar
> Microsoft



Mon, 29 Dec 2003 00:45:14 GMT  
 Problems with default button on a dynamically created dialog
Hi, John!

This may not be the answer your looking for, but have you thought about using an empty DLGTEMPLATE,
overriding DoModal and taking advantage of InitModalIndirect instead? An empty template is as simple
as...

    class CMyDynamicDlg
    {
        ...
        //{{AFX_VIRTUAL(CDynamicDlg)
        public:
        virtual int DoModal();
        protected:
        virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
        //}}AFX_VIRTUAL
    ...
    protected:
        struct NULLTEMPLATE : public DLGTEMPLATE
        {
            WORD no_menu;
            WORD no_class;
            WORD no_title;
            WORD no_font;
        } m_template;
    };

    CDynamicDlg::CDynamicDlg(CWnd* pParent /*=NULL*/)
     : CDialog((UINT)0, pParent)
    {
    }

    int CDynamicDlg::DoModal()
    {
        ZeroMemory(&m_template, sizeof(m_template));
        m_template.style = (DS_MODALFRAME|WS_POPUP|WS_CAPTION|WS_SYSMENU);
        InitModalIndirect(&m_template);
        return CDialog::DoModal();
    }

It seems like it would leave you no worse off as far as creating the child controls, sizing,
positioning, font setting and whatever, and seems to work just fine. I created a BS_DEFPUSHBUTTON,
and called SetDefID...

    BOOL CDynamicDlg::OnInitDialog()
    {
        CDialog::OnInitDialog();

        if (CFont* pFont = CFont::FromHandle((HFONT)GetStockObject(
            DEFAULT_GUI_FONT)))
        {
            SetFont(pFont);

            SetWindowText(_T("My Dialog"));
            MoveWindow(0, 0, 400, 200);
            CenterWindow(m_pParentWnd);

            if (m_buttonDefault.Create(
                _T("OK"),
                WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
                CRect(110,10,160,30),
                this,
                1002))
            {
                m_buttonDefault.SetFont(pFont);
                SetDefID(1002);
            }
            ...
        }
        ...
    }

Then all you have to do is invoke it as usual...

    #include "DynamicDlg.h"
    void CNg0711011View::OnButtonDynamic()
    {
        CDynamicDlg dlg(this);
        dlg.DoModal();
    }

HTH,

Jeff...
--
Please post all follow-ups to the newsgroup only.

Quote:

> Thank you for your response.

> If you create a dialog with the resource editor with some buttons on it, one
> of which has the default button style, and some other controls -- for example
> an edit box control -- when you tab to the edit box control the default button
> gets a dark border and if you press ENTER while in the edit box you get an
> event for the default button.  This is the behavior I am trying to achieve.
> Creating the dialog dynamically, I'm not getting it.

> Following are some details on how I'm creating my dialog:
>     In the h file for my dialog class I eliminated the line defining IDD
> because I have no resource template.
>     I changed the constructor for my dialog class to take no parameters
> instead of a parameter for a pointer to the parent CWnd.
>     I call CreatEx to create the dialog window.
>     I create CButton, CEdit, etc. objects as children of my dialog window.
>     I then show the dialog window (ShowWindow) and call RunModalLoop.

> Does the behavior of the default button rely on a resource template and, if
> so, is there some way of faking it out?  Any thoughts?

> John Shell


> > Hi,
> >   BS_DEFPUSHBUTTON   Creates a button that has a heavy black border. The
> > user can select this button by pressing the ENTER key. This style enables
> > the user to quickly select the most likely option (the default option).
> > That is the default behaviour that you will get but if the button has
> > "FOCUS" however if you tab to another control now the focus shifts to that
> > control and pressing enter key would not have the same effect. The default
> > push button in a dialog box is defined to be the button that is pressed
> > when the user chooses the ENTER key, provided that the input focus is not
> > on another button in the dialog box. The default push button is visually
> > distinguished from other buttons by a thick dark border.
> > If you want to change the default push button using SetDefID make sure you
> > set the focus to that button so that the default action can take place.

> > Regards
> > Chetan N Parmar
> > Microsoft



Mon, 29 Dec 2003 19:27:09 GMT  
 Problems with default button on a dynamically created dialog
Jeff:
Thanks for the suggestion.  That will work, however, the fact that all the controls need to be created
in the OnInitDialog function makes it a little ugly.  It would mean that the dialog class
(CMyDynamicDlg) would have to have a function that would be called for each control that is to go on the
dialog.  This function would have to store information about how to create the control, then
OnInitDialog would need to interpret that stored info and create the controls from it.  Not pretty!

I guess an ugly solution might be better than none at all, but maybe Chetan at Microsoft (or somebody
else) has another suggestion...

John

Quote:

> Hi, John!

> This may not be the answer your looking for, but have you thought about using an empty DLGTEMPLATE,
> overriding DoModal and taking advantage of InitModalIndirect instead? An empty template is as simple
> as...

>     class CMyDynamicDlg
>     {
>         ...
>         //{{AFX_VIRTUAL(CDynamicDlg)
>         public:
>         virtual int DoModal();
>         protected:
>         virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
>         //}}AFX_VIRTUAL
>     ...
>     protected:
>         struct NULLTEMPLATE : public DLGTEMPLATE
>         {
>             WORD no_menu;
>             WORD no_class;
>             WORD no_title;
>             WORD no_font;
>         } m_template;
>     };

>     CDynamicDlg::CDynamicDlg(CWnd* pParent /*=NULL*/)
>      : CDialog((UINT)0, pParent)
>     {
>     }

>     int CDynamicDlg::DoModal()
>     {
>         ZeroMemory(&m_template, sizeof(m_template));
>         m_template.style = (DS_MODALFRAME|WS_POPUP|WS_CAPTION|WS_SYSMENU);
>         InitModalIndirect(&m_template);
>         return CDialog::DoModal();
>     }

> It seems like it would leave you no worse off as far as creating the child controls, sizing,
> positioning, font setting and whatever, and seems to work just fine. I created a BS_DEFPUSHBUTTON,
> and called SetDefID...

>     BOOL CDynamicDlg::OnInitDialog()
>     {
>         CDialog::OnInitDialog();

>         if (CFont* pFont = CFont::FromHandle((HFONT)GetStockObject(
>             DEFAULT_GUI_FONT)))
>         {
>             SetFont(pFont);

>             SetWindowText(_T("My Dialog"));
>             MoveWindow(0, 0, 400, 200);
>             CenterWindow(m_pParentWnd);

>             if (m_buttonDefault.Create(
>                 _T("OK"),
>                 WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
>                 CRect(110,10,160,30),
>                 this,
>                 1002))
>             {
>                 m_buttonDefault.SetFont(pFont);
>                 SetDefID(1002);
>             }
>             ...
>         }
>         ...
>     }

> Then all you have to do is invoke it as usual...

>     #include "DynamicDlg.h"
>     void CNg0711011View::OnButtonDynamic()
>     {
>         CDynamicDlg dlg(this);
>         dlg.DoModal();
>     }

> HTH,

> Jeff...
> --
> Please post all follow-ups to the newsgroup only.



Sat, 03 Jan 2004 04:26:11 GMT  
 Problems with default button on a dynamically created dialog
Hi, John!

Quote:

> Jeff:
> Thanks for the suggestion.  That will work, however, the fact that all the controls need to be
created
> in the OnInitDialog function makes it a little ugly.  It would mean that the dialog class
> (CMyDynamicDlg) would have to have a function that would be called for each control that is to go
on the
> dialog.  This function would have to store information about how to create the control, then
> OnInitDialog would need to interpret that stored info and create the controls from it.  Not
pretty!

Sorry if I wasted you time, but your earlier description sounded to me like you were already
creating all of your controls dynamically...

    In the h file for my dialog class I eliminated the line defining IDD because I have no resource
template.
    I changed the constructor for my dialog class to take no parameters...
    I call CreatEx to create the dialog window.
    I create CButton, CEdit, etc. objects as children of my dialog window.
    I then show the dialog window (ShowWindow) and call RunModalLoop.

...so I'm not certain how my solution complicates matters in that respect. Is it just the location
in OnInitDialog that bothers you? Or am I misunderstanding you entirely?

Jeff...
--
Please post all follow-ups to the newsgroup only.



Sat, 03 Jan 2004 06:42:41 GMT  
 Problems with default button on a dynamically created dialog
Hi Jeff,
You didn't waste my time.  I didn't mean to slam your solution and meant no disrespect.  What I mean by
creating the controls dynamically is that I don't know in advance what controls are to go on the dialog
or with what parameters they are to be created.  That is why it is messy to create them in the
OnInitDialog.

To give more detail of my situation:  We have an interpreter that processes code written in a high
level language of our own creation.  We want to give this language the ability to put up dialogs.  I
made a command for this language to create a dialog, a set of commands to create controls as children
of that dialog and a command to run the dialog modally.  So, I don't know anything about the dialog or
controls until the interpreter processes the commands.  That is why I said I would need a function
(that the interpreter would call) to store information about each control and OnInitDialog would need
to read that information and create the controls from it.  I can't create the controls until the dialog
(their parent) is a valid window, which isn't until DoModal is called.

So far, this is the only solution I've found that will work for this situation.  There are things going
on inside CDialog of which I don't have enough of an understanding to be able to override and do
myself.

I apologize for not being clearer in earlier posts.  I hope this one eliminates the ambiguities.

Thanks,
John

Quote:

> Hi, John!


> > Jeff:
> > Thanks for the suggestion.  That will work, however, the fact that all the controls need to be
> created
> > in the OnInitDialog function makes it a little ugly.  It would mean that the dialog class
> > (CMyDynamicDlg) would have to have a function that would be called for each control that is to go
> on the
> > dialog.  This function would have to store information about how to create the control, then
> > OnInitDialog would need to interpret that stored info and create the controls from it.  Not
> pretty!

> Sorry if I wasted you time, but your earlier description sounded to me like you were already
> creating all of your controls dynamically...

>     In the h file for my dialog class I eliminated the line defining IDD because I have no resource
> template.
>     I changed the constructor for my dialog class to take no parameters...
>     I call CreatEx to create the dialog window.
>     I create CButton, CEdit, etc. objects as children of my dialog window.
>     I then show the dialog window (ShowWindow) and call RunModalLoop.

> ...so I'm not certain how my solution complicates matters in that respect. Is it just the location
> in OnInitDialog that bothers you? Or am I misunderstanding you entirely?

> Jeff...
> --
> Please post all follow-ups to the newsgroup only.



Sun, 04 Jan 2004 02:44:14 GMT  
 Problems with default button on a dynamically created dialog
Then it seems that CDialog::InitModalIndirect is intended for that purpose.


Quote:
> Hi Jeff,
> You didn't waste my time.  I didn't mean to slam your solution and meant

no disrespect.  What I mean by
Quote:
> creating the controls dynamically is that I don't know in advance what

controls are to go on the dialog
Quote:
> or with what parameters they are to be created.  That is why it is messy

to create them in the
Quote:
> OnInitDialog.

> To give more detail of my situation:  We have an interpreter that

processes code written in a high
Quote:
> level language of our own creation.  We want to give this language the

ability to put up dialogs.  I
Quote:
> made a command for this language to create a dialog, a set of commands to

create controls as children
Quote:
> of that dialog and a command to run the dialog modally.  So, I don't know

anything about the dialog or
Quote:
> controls until the interpreter processes the commands.  That is why I said

I would need a function
Quote:
> (that the interpreter would call) to store information about each control

and OnInitDialog would need
Quote:
> to read that information and create the controls from it.  I can't create

the controls until the dialog
Quote:
> (their parent) is a valid window, which isn't until DoModal is called.

> So far, this is the only solution I've found that will work for this

situation.  There are things going
Quote:
> on inside CDialog of which I don't have enough of an understanding to be

able to override and do
Quote:
> myself.

> I apologize for not being clearer in earlier posts.  I hope this one

eliminates the ambiguities.
Quote:

> Thanks,
> John


> > Hi, John!




Quote:
> > > Jeff:
> > > Thanks for the suggestion.  That will work, however, the fact that all

the controls need to be
Quote:
> > created
> > > in the OnInitDialog function makes it a little ugly.  It would mean

that the dialog class
Quote:
> > > (CMyDynamicDlg) would have to have a function that would be called for

each control that is to go
Quote:
> > on the
> > > dialog.  This function would have to store information about how to

create the control, then
Quote:
> > > OnInitDialog would need to interpret that stored info and create the

controls from it.  Not
Quote:
> > pretty!

> > Sorry if I wasted you time, but your earlier description sounded to me

like you were already
Quote:
> > creating all of your controls dynamically...

> >     In the h file for my dialog class I eliminated the line defining IDD

because I have no resource
Quote:
> > template.
> >     I changed the constructor for my dialog class to take no
parameters...
> >     I call CreatEx to create the dialog window.
> >     I create CButton, CEdit, etc. objects as children of my dialog
window.
> >     I then show the dialog window (ShowWindow) and call RunModalLoop.

> > ...so I'm not certain how my solution complicates matters in that

respect. Is it just the location
Quote:
> > in OnInitDialog that bothers you? Or am I misunderstanding you entirely?

> > Jeff...
> > --
> > Please post all follow-ups to the newsgroup only.



Sun, 04 Jan 2004 05:03:07 GMT  
 Problems with default button on a dynamically created dialog
Hi, John!

Just for the record, you've not hurt my feelings. It was sort  of a half-baked solution in any
event. The quote-unquote official way to go about this is -- I guess, to create your DLGTEMPLATE
dynamically along with enough room for a DLGITEMTEMPLATE for each control and then fill everything
in and pass the whole deal to the InitModalIndirect function. The only catch is that you have to get
the DWORD and WORD alignment just right. It's a pain but it can be made to work. The MSDN Article
Q140725 - DOCERR: Incorrect DialogBoxIndirect() Code in Win32 SDK Docs, may offer some help here as
well as the MSDN sample DLGTEMPL. And somewhere out there -- CodeGuru or MS, theres a class
implementation for this but I can't remember just where. Sorry I couldn't be more help, but anyway,
good luck with it.

Jeff...
--
Please post all follow-ups to the newsgroup only.

Quote:

> Hi Jeff,
> You didn't waste my time.  I didn't mean to slam your solution and meant no disrespect.  What I
mean by
> creating the controls dynamically is that I don't know in advance what controls are to go on the
dialog
> or with what parameters they are to be created.  That is why it is messy to create them in the
> OnInitDialog.

> To give more detail of my situation:  We have an interpreter that processes code written in a high
> level language of our own creation.  We want to give this language the ability to put up dialogs.
I
> made a command for this language to create a dialog, a set of commands to create controls as
children
> of that dialog and a command to run the dialog modally.  So, I don't know anything about the
dialog or
> controls until the interpreter processes the commands.  That is why I said I would need a function
> (that the interpreter would call) to store information about each control and OnInitDialog would
need
> to read that information and create the controls from it.  I can't create the controls until the
dialog
> (their parent) is a valid window, which isn't until DoModal is called.

> So far, this is the only solution I've found that will work for this situation.  There are things
going
> on inside CDialog of which I don't have enough of an understanding to be able to override and do
> myself.

> I apologize for not being clearer in earlier posts.  I hope this one eliminates the ambiguities.

> Thanks,
> John


> > Hi, John!


> > > Jeff:
> > > Thanks for the suggestion.  That will work, however, the fact that all the controls need to be
> > created
> > > in the OnInitDialog function makes it a little ugly.  It would mean that the dialog class
> > > (CMyDynamicDlg) would have to have a function that would be called for each control that is to
go
> > on the
> > > dialog.  This function would have to store information about how to create the control, then
> > > OnInitDialog would need to interpret that stored info and create the controls from it.  Not
> > pretty!

> > Sorry if I wasted you time, but your earlier description sounded to me like you were already
> > creating all of your controls dynamically...

> >     In the h file for my dialog class I eliminated the line defining IDD because I have no
resource
> > template.
> >     I changed the constructor for my dialog class to take no parameters...
> >     I call CreatEx to create the dialog window.
> >     I create CButton, CEdit, etc. objects as children of my dialog window.
> >     I then show the dialog window (ShowWindow) and call RunModalLoop.

> > ...so I'm not certain how my solution complicates matters in that respect. Is it just the
location
> > in OnInitDialog that bothers you? Or am I misunderstanding you entirely?

> > Jeff...
> > --
> > Please post all follow-ups to the newsgroup only.



Sun, 04 Jan 2004 06:34:24 GMT  
 Problems with default button on a dynamically created dialog
Jeff:
I found the dynamic dialog class at Codeguru like you mentioned and it's very nice.  However, it needed
a little modification to get a default button working.  (BTW:  It uses the method I mentioned of having
a function that stores info about how to create the controls and then creates them--using
CWnd::CreateEx--in OnInitDialog.)  For anyone interested, the modifications I made were to add a
GetStyle and a GetID function to the class used for storing the control info and then added the
following lines after the call in OnInitDialog that creates each control:
    if (pDynDialogItemEx->GetStyle() & BS_DEFPUSHBUTTON)
        SetDefID(pDynDialogItemEx->GetID());

I haven't tried your suggested method of making DLGITEMTEMPLATEs yet, but taking the Codeguru code seems
like a quicker, simpler solution.

One thing I found while working through this is:  A button with an ID of IDOK always acts as a default
button, so if you want a default button on a dialog you can just make it with an ID of IDOK (and the
above modifications to the Codeguru code become unnecessary).

Thanks tons for all the help and thanks to Marcel Scher{*filter*}se for the code at Codeguru.

John Shell

Quote:

> Hi, John!

> Just for the record, you've not hurt my feelings. It was sort  of a half-baked solution in any
> event. The quote-unquote official way to go about this is -- I guess, to create your DLGTEMPLATE
> dynamically along with enough room for a DLGITEMTEMPLATE for each control and then fill everything
> in and pass the whole deal to the InitModalIndirect function. The only catch is that you have to get
> the DWORD and WORD alignment just right. It's a pain but it can be made to work. The MSDN Article
> Q140725 - DOCERR: Incorrect DialogBoxIndirect() Code in Win32 SDK Docs, may offer some help here as
> well as the MSDN sample DLGTEMPL. And somewhere out there -- CodeGuru or MS, theres a class
> implementation for this but I can't remember just where. Sorry I couldn't be more help, but anyway,
> good luck with it.

> Jeff...
> --
> Please post all follow-ups to the newsgroup only.



Mon, 05 Jan 2004 21:48:28 GMT  
 Problems with default button on a dynamically created dialog
Hi, John!

Glad you found a solution that works for you. Don't work to hard trying the DLGITEMTEMPLATE
approach -- unless you just want the experience.

Jeff...
--
Please post all follow-ups to the newsgroup only.

Quote:

> Jeff:
> I found the dynamic dialog class at Codeguru like you mentioned and it's very nice.  However, it
needed
> a little modification to get a default button working.  (BTW:  It uses the method I mentioned of
having
> a function that stores info about how to create the controls and then creates them--using
> CWnd::CreateEx--in OnInitDialog.)  For anyone interested, the modifications I made were to add a
> GetStyle and a GetID function to the class used for storing the control info and then added the
> following lines after the call in OnInitDialog that creates each control:
>     if (pDynDialogItemEx->GetStyle() & BS_DEFPUSHBUTTON)
>         SetDefID(pDynDialogItemEx->GetID());

> I haven't tried your suggested method of making DLGITEMTEMPLATEs yet, but taking the Codeguru code
seems
> like a quicker, simpler solution.

> One thing I found while working through this is:  A button with an ID of IDOK always acts as a
default
> button, so if you want a default button on a dialog you can just make it with an ID of IDOK (and
the
> above modifications to the Codeguru code become unnecessary).

> Thanks tons for all the help and thanks to Marcel Scher{*filter*}se for the code at Codeguru.

> John Shell


> > Hi, John!

> > Just for the record, you've not hurt my feelings. It was sort  of a half-baked solution in any
> > event. The quote-unquote official way to go about this is -- I guess, to create your DLGTEMPLATE
> > dynamically along with enough room for a DLGITEMTEMPLATE for each control and then fill
everything
> > in and pass the whole deal to the InitModalIndirect function. The only catch is that you have to
get
> > the DWORD and WORD alignment just right. It's a pain but it can be made to work. The MSDN
Article
> > Q140725 - DOCERR: Incorrect DialogBoxIndirect() Code in Win32 SDK Docs, may offer some help here
as
> > well as the MSDN sample DLGTEMPL. And somewhere out there -- CodeGuru or MS, theres a class
> > implementation for this but I can't remember just where. Sorry I couldn't be more help, but
anyway,
> > good luck with it.

> > Jeff...
> > --
> > Please post all follow-ups to the newsgroup only.



Tue, 06 Jan 2004 05:59:32 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Dynamically Created Checkbox Button Does Not Appear on Dialog

2. Problems with List box in dynamically created dialog box

3. Problems with List box in dynamically created dialog box

4. How do you change the default button dynamically?

5. Dynamically creating buttons and firing events

6. Different Font when creating buttons dynamically

7. URGENT - Dynamically creating radio buttons in Visual C++ 6.0 (MFC)

8. Where is my dynamically created button?????

9. REQ::How Can I create A Button Dynamically

10. Creating a button dynamically

11. Dynamically Created Buttons and their Fonts

12. create dynamically a button in a FormView

 

 
Powered by phpBB® Forum Software