Discussion:
Form scaling
(too old to reply)
David Ayre
2008-07-28 15:06:55 UTC
Permalink
Hi,

I use the following code to scale a form, along with all the
controls on it, to fit the screen:-

float iVScale = ((float)Screen->Height)/((float)Height);
float iHScale = ((float)Screen->Width)/((float)Width);
Top = 0;
Left = 0;
Width = Screen->Width;
Height = Screen->Height;
int iCount = ControlCount;
for(short i = 0;i < iCount;i++)
{
Controls[i]->Left *= iHScale;
Controls[i]->Width *= iHScale;
Controls[i]->Top *= iVScale;
Controls[i]->Height *= iVScale;
}
This works fine except when I have controls in a GroupBox. In
this case it scales the GroupBox but not the controls in it.
Is there any way I can scale the controls inside the GroupBox
as well?

Thanks,

David
Clayton Arends
2008-07-28 20:40:33 UTC
Permalink
Post by David Ayre
Is there any way I can scale the controls inside the GroupBox
as well?
Untested:

void ScaleControl(TControl* Control, double iHScale, double iVScale);
void ScaleControls(TWinControl* Control, double iHScale, double iVScale);

void ScaleControl(TControl* Control, double iHScale, double iVScale)
{
Control->Left *= iHScale;
Control->Width *= iHScale;
Control->Top *= iVScale;
Control->Height *= iVScale;

if (dynamic_cast <TWinControl*> (Control))
ScaleControls(static_cast <TWinControl*> (Control),
iHScale, iVScale);
}

void ScaleControls(TWinControl* Control, double iHScale, double iVScale)
{
for (int i = 0, iCount = Control->ControlCount; i < iCount; ++i)
ScaleControl(Control->Controls[i], iHScale, iVScale);
}

// scaling the form
double iVScale = ((double) Screen->Height) / ((double) Height);
double iHScale = ((double) Screen->Width) / ((double) Width);
Top = 0;
Left = 0;
Width = Screen->Width;
Height = Screen->Height;
ScaleControls(this, iHScale, iVScale);

(double) has far better precision than (float). Also, don't forget to
change font sizes (if desired).

HTH,
Clayton
David Ayre
2008-07-29 12:58:48 UTC
Permalink
Thanks Clayton,

I'll give that a try.

Cheers,

David
Post by Clayton Arends
void ScaleControl(TControl* Control, double iHScale, double iVScale);
void ScaleControls(TWinControl* Control, double iHScale, double iVScale);
void ScaleControl(TControl* Control, double iHScale, double iVScale)
{
Control->Left *= iHScale;
Control->Width *= iHScale;
Control->Top *= iVScale;
Control->Height *= iVScale;
if (dynamic_cast <TWinControl*> (Control))
ScaleControls(static_cast <TWinControl*> (Control),
iHScale, iVScale);
}
void ScaleControls(TWinControl* Control, double iHScale, double iVScale)
{
for (int i = 0, iCount = Control->ControlCount; i < iCount; ++i)
ScaleControl(Control->Controls[i], iHScale, iVScale);
}
// scaling the form
double iVScale = ((double) Screen->Height) / ((double) Height);
double iHScale = ((double) Screen->Width) / ((double) Width);
Top = 0;
Left = 0;
Width = Screen->Width;
Height = Screen->Height;
ScaleControls(this, iHScale, iVScale);
(double) has far better precision than (float). Also, don't forget to
change font sizes (if desired).
HTH,
Clayton
David Ayre
2008-07-29 14:58:35 UTC
Permalink
That works fine, thanks Clayton.

Cheers,

David
Post by Clayton Arends
void ScaleControl(TControl* Control, double iHScale, double iVScale);
void ScaleControls(TWinControl* Control, double iHScale, double iVScale);
void ScaleControl(TControl* Control, double iHScale, double iVScale)
{
Control->Left *= iHScale;
Control->Width *= iHScale;
Control->Top *= iVScale;
Control->Height *= iVScale;
if (dynamic_cast <TWinControl*> (Control))
ScaleControls(static_cast <TWinControl*> (Control),
iHScale, iVScale);
}
void ScaleControls(TWinControl* Control, double iHScale, double iVScale)
{
for (int i = 0, iCount = Control->ControlCount; i < iCount; ++i)
ScaleControl(Control->Controls[i], iHScale, iVScale);
}
// scaling the form
double iVScale = ((double) Screen->Height) / ((double) Height);
double iHScale = ((double) Screen->Width) / ((double) Width);
Top = 0;
Left = 0;
Width = Screen->Width;
Height = Screen->Height;
ScaleControls(this, iHScale, iVScale);
(double) has far better precision than (float). Also, don't forget to
change font sizes (if desired).
HTH,
Clayton
Loading...