Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Dynamically created controls and message maps in MFC

Using MFC, it is easy enough to create a dialog from a template created at
design time, and to route windows messages through the message map of a class
based on CDialog which embodies the functionality of the dialog, for controls
placed on the template at design time. For example, I can make a new dialog
template using the resource editor in VC++, draw a button on the template,
make an entry in the message map and have OnButtonClicked() called when I
click on the button at run time.

Now, my question is, how does one do this for controls that were not created
at design time? If I make a new button using the CButton MFC class and place
it on the dialog at run-time using CButton's Create method, how can I then
get this control to route its messages through my dialog's message map, thus
calling OnButtonClicked() as appropriate?

Thanks for reading this far, and any answers that might be forthcoming.

Travis Hall
[997 byte] By [Travis Hall] at [2007-11-10 12:52:38]
# 1 Re: Dynamically created controls and message maps in MFC
"Travis Hall" <hallt@qst.newsltd.com.au> wrote:
>
>Using MFC, it is easy enough to create a dialog from a template created
at
>design time, and to route windows messages through the message map of a
class
>based on CDialog which embodies the functionality of the dialog, for controls
>placed on the template at design time. For example, I can make a new dialog
>template using the resource editor in VC++, draw a button on the template,
>make an entry in the message map and have OnButtonClicked() called when
I
>click on the button at run time.
>
>Now, my question is, how does one do this for controls that were not created
>at design time? If I make a new button using the CButton MFC class and place
>it on the dialog at run-time using CButton's Create method, how can I then
>get this control to route its messages through my dialog's message map,
thus
>calling OnButtonClicked() as appropriate?
>
>Thanks for reading this far, and any answers that might be forthcoming.
>
>Travis Hall

Hi, Travis
All problems by dynamic creation of controls will be removed if to create
it as hidden and use the method CWnd::ShowWindow(SW_SHOW) of yours control.
Regards, Yuri
Yuri Kirilenko at 2007-11-11 20:52:46 >
# 2 Re: Dynamically created controls and message maps in MFC
Hi,
Add a new class to the project (i.e. CMyButton) derived from CButton. Add a
message handler to the class message map using the class wizard (Ctrl-W)
which can be OnButtonClicked.
Then add an instance of CMyButton to your CDialog derived class.
for example:
CMyDialog: public CDialog
{
...
protected:
CMyButton m_button1;
...
}
offcourse, you need to add the header file of CMyButton "mybutton.h" above
the declaration of CMyDialog
In the cpp file and in OnInitDialog call Create function for m_button1
creating it at run-time. Remember to add the following at OnSize message
handler for the dialog:
m_button1.MoveWindow(x1, y1, w, h)
where x1, y1, w and h are the left, top, width and height of your created
control.

Good luck.
Emad

Travis Hall <hallt@qst.newsltd.com.au> wrote in message
news:38e45723$1@news.dev-archive.com...
>
> Using MFC, it is easy enough to create a dialog from a template created at
> design time, and to route windows messages through the message map of a
class
> based on CDialog which embodies the functionality of the dialog, for
controls
> placed on the template at design time. For example, I can make a new
dialog
> template using the resource editor in VC++, draw a button on the template,
> make an entry in the message map and have OnButtonClicked() called when I
> click on the button at run time.
>
> Now, my question is, how does one do this for controls that were not
created
> at design time? If I make a new button using the CButton MFC class and
place
> it on the dialog at run-time using CButton's Create method, how can I then
> get this control to route its messages through my dialog's message map,
thus
> calling OnButtonClicked() as appropriate?
>
> Thanks for reading this far, and any answers that might be forthcoming.
>
> Travis Hall
Emad Steitieh at 2007-11-11 20:53:52 >