Coordinates of dynamically created controls in a dialog - Programmer
This is a discussion on Coordinates of dynamically created controls in a dialog - Programmer ; Windows XP SP2, MS Visual C++ .NET
I'm creating a dialog-based application and I want to create some
controls (static text labels and buttons) on it dynamically, during
runtime. Therefore, I need to set the coordinates and sizes of these
...
-
Coordinates of dynamically created controls in a dialog
Windows XP SP2, MS Visual C++ .NET
I'm creating a dialog-based application and I want to create some
controls (static text labels and buttons) on it dynamically, during
runtime. Therefore, I need to set the coordinates and sizes of these
controls. For this purpose, I created some dummy controls in
appropriate places statically, using Visual Studio resource editor, and
then looked up the resulting resource file (.rc). There are the
coordinates of the controls, e.g.,
LTEXT "Channel",IDC_STATIC,15,60,34,12
Now, I need to translate these coordinates into the parameters passed
to the "Create" function. I did this in the "OnInitDialog()" function:
CRect cr;
cr = CRect (15,60,34,12); // coordinates from the .rc file
MapDialogRect (&cr);
//lbl_channel_no defined as the dialog class member
lbl_channel_no[0].Create (_T("CHANNEL NUMBER"), WS_CHILD | WS_VISIBLE,
cr, this);
However, the above does not create any controls in the dialog window.
It seems that I convert the coordinates incorrectly. What do I do wrong
in the above code?
-
Re: Coordinates of dynamically created controls in a dialog
The third and forth numbers are width and height.
try
> CRect cr;
> cr = CRect (15,60,49,72); // coordinates from the .rc file
AliR.
wrote in message
news:1122471671.971464.20510@o13g2000cwo.googlegro ups.com...
> Windows XP SP2, MS Visual C++ .NET
>
> I'm creating a dialog-based application and I want to create some
> controls (static text labels and buttons) on it dynamically, during
> runtime. Therefore, I need to set the coordinates and sizes of these
> controls. For this purpose, I created some dummy controls in
> appropriate places statically, using Visual Studio resource editor, and
> then looked up the resulting resource file (.rc). There are the
> coordinates of the controls, e.g.,
>
> LTEXT "Channel",IDC_STATIC,15,60,34,12
>
> Now, I need to translate these coordinates into the parameters passed
> to the "Create" function. I did this in the "OnInitDialog()" function:
>
> CRect cr;
> cr = CRect (15,60,34,12); // coordinates from the .rc file
> MapDialogRect (&cr);
> //lbl_channel_no defined as the dialog class member
> lbl_channel_no[0].Create (_T("CHANNEL NUMBER"), WS_CHILD | WS_VISIBLE,
> cr, this);
>
> However, the above does not create any controls in the dialog window.
> It seems that I convert the coordinates incorrectly. What do I do wrong
> in the above code?
>
-
Re: Coordinates of dynamically created controls in a dialog
wrote in message
news:1122471671.971464.20510@o13g2000cwo.googlegro ups.com...
> Windows XP SP2, MS Visual C++ .NET
>
> I'm creating a dialog-based application and I want to create some
> controls (static text labels and buttons) on it dynamically, during
> runtime. Therefore, I need to set the coordinates and sizes of these
> controls. For this purpose, I created some dummy controls in
> appropriate places statically, using Visual Studio resource editor, and
> then looked up the resulting resource file (.rc). There are the
> coordinates of the controls, e.g.,
>
> LTEXT "Channel",IDC_STATIC,15,60,34,12
>
> Now, I need to translate these coordinates into the parameters passed
> to the "Create" function. I did this in the "OnInitDialog()" function:
>
> CRect cr;
> cr = CRect (15,60,34,12); // coordinates from the .rc file
> MapDialogRect (&cr);
> //lbl_channel_no defined as the dialog class member
> lbl_channel_no[0].Create (_T("CHANNEL NUMBER"), WS_CHILD | WS_VISIBLE,
> cr, this);
>
> However, the above does not create any controls in the dialog window.
> It seems that I convert the coordinates incorrectly. What do I do wrong
> in the above code?
>
>
Simple way to do it is
1) assign a unique label to the control, e.g. IDC_STATIC_CHANNEL
2) In OnInitDialog:
CRect rectChannel;
GetDlgItem(IDC_STATIC_CHANNEL)->GetWindowRect(&rectChannel);
ScreenToClient (&rectChannel);
// Now rectChannel contains the pixel coordinates of the
IDC_STATIC_CHANNEL control,
// in the dialog's client coordinates (i.e. the same coordinate
system you use if you were to Create()
// the static control dynamically
Regards,
David
http://www.dcsoft.com