From: Ramesh on
I am fairly new to UI programming using VC++. I am looking for ways to modify
a dialog box dynamically. In other words, I want to be able to add controls
to an exisitng dialog during run time. This is a simple dialog which will be
empty initially. Just before it is shown, I want to be able to add rows or
columns of check boxes with static texts next to them, based on some strings
read from the registry. Is this possible? How? Any code sample will be
helpful. Thanks in advance
From: Scott McPhillips [MVP] on
Ramesh wrote:

> I am fairly new to UI programming using VC++. I am looking for ways to modify
> a dialog box dynamically. In other words, I want to be able to add controls
> to an exisitng dialog during run time. This is a simple dialog which will be
> empty initially. Just before it is shown, I want to be able to add rows or
> columns of check boxes with static texts next to them, based on some strings
> read from the registry. Is this possible? How? Any code sample will be
> helpful. Thanks in advance

Yes, it is possible. Each of the control classes, such as CStatic and
CButton, has a Create function. Call it in OnInitDialog to dynamically
create the control. Use the ON_CONTROL_RANGE macro in your message map
to dispatch messages from the dynamically created controls.

You should also consider an easier way: You can put all the controls
that might be needed onto the dialog template at design time. In
OnInitDialog you can make each control visible or invisible by calling
its ShowWindow function.

--
Scott McPhillips [VC++ MVP]

From: Ramesh on
Scott,

Thanks very much for the quick reply. The reason I have to add them
dynamically as opposed to during design time is because the number strings
available in the registry is variable.

If you can throw some light (i.e., sample code) on how to add the event
handlers to the dynamically added controls that will be really helpful. I am
not familiar with the ON_CONTROL_RANGE macro.

Thanks again

K. Ramesh

"Scott McPhillips [MVP]" wrote:

> Ramesh wrote:
>
> > I am fairly new to UI programming using VC++. I am looking for ways to modify
> > a dialog box dynamically. In other words, I want to be able to add controls
> > to an exisitng dialog during run time. This is a simple dialog which will be
> > empty initially. Just before it is shown, I want to be able to add rows or
> > columns of check boxes with static texts next to them, based on some strings
> > read from the registry. Is this possible? How? Any code sample will be
> > helpful. Thanks in advance
>
> Yes, it is possible. Each of the control classes, such as CStatic and
> CButton, has a Create function. Call it in OnInitDialog to dynamically
> create the control. Use the ON_CONTROL_RANGE macro in your message map
> to dispatch messages from the dynamically created controls.
>
> You should also consider an easier way: You can put all the controls
> that might be needed onto the dialog template at design time. In
> OnInitDialog you can make each control visible or invisible by calling
> its ShowWindow function.
>
> --
> Scott McPhillips [VC++ MVP]
>
>
From: Scott McPhillips [MVP] on
Ramesh wrote:

> Scott,
>
> Thanks very much for the quick reply. The reason I have to add them
> dynamically as opposed to during design time is because the number strings
> available in the registry is variable.
>
> If you can throw some light (i.e., sample code) on how to add the event
> handlers to the dynamically added controls that will be really helpful. I am
> not familiar with the ON_CONTROL_RANGE macro.
>
> Thanks again
>
> K. Ramesh

If you know the maximum number of strings then dynamic creation is not
necessary. Just put the maximum number on the resource template. The
number of controls that you make visible can be dynamic (and the size of
the dialog can be dynamic).

To handle a message from dynamically created controls you first select
an unused range of ID values to be used for the controls. Define them
in resource.h:
#define IDC_RADIO_0 2000
#define IDC_RADIO_4

Add to message map:
....
ON_CONTROL_RANGE(BN_CLICKED, IDC_RADIO_0, IDC_RADIO_4, OnClickedRadio)
END_MESSAGE_MAP()

void CSomeView::OnClickedRadio(UINT nID)
{

}

--
Scott McPhillips [VC++ MVP]

From: Ramesh on
I will try out your suggestion Scott, thanks.

But then, the resource IDs should already have been defined during design
time, right? Is there no way we can define the whole thing during run time
i.e., adding the control, defining the resource ID, assigning event handler,
etc (I guess not)?

"Scott McPhillips [MVP]" wrote:

> Ramesh wrote:
>
> > Scott,
> >
> > Thanks very much for the quick reply. The reason I have to add them
> > dynamically as opposed to during design time is because the number strings
> > available in the registry is variable.
> >
> > If you can throw some light (i.e., sample code) on how to add the event
> > handlers to the dynamically added controls that will be really helpful. I am
> > not familiar with the ON_CONTROL_RANGE macro.
> >
> > Thanks again
> >
> > K. Ramesh
>
> If you know the maximum number of strings then dynamic creation is not
> necessary. Just put the maximum number on the resource template. The
> number of controls that you make visible can be dynamic (and the size of
> the dialog can be dynamic).
>
> To handle a message from dynamically created controls you first select
> an unused range of ID values to be used for the controls. Define them
> in resource.h:
> #define IDC_RADIO_0 2000
> #define IDC_RADIO_4
>
> Add to message map:
> ....
> ON_CONTROL_RANGE(BN_CLICKED, IDC_RADIO_0, IDC_RADIO_4, OnClickedRadio)
> END_MESSAGE_MAP()
>
> void CSomeView::OnClickedRadio(UINT nID)
> {
>
> }
>
> --
> Scott McPhillips [VC++ MVP]
>
>
 |  Next  |  Last
Pages: 1 2
Prev: linker problem
Next: Where to download new RichEd20.dll