Is this C#/Windows Forms?
Manual scaling is a pain in the neck. Realistically the only way to completely prevent any major rounding errors over the long-term lifetime of a form is to store the original locations and sizes of every control that you reposition/resize, and make all your reposition/resize code relative to the original position as opposed to the current position.
You'll end up with some fugliness like this:
public class Form1
{
private Size originalFormSize;
private List<Control> controlsToResize = new List<Control>();
private List<Point> originalLocations;
private List<Size> originalSizes;
public Form1()
{
InitializeComponent();
SaveOriginalSizes();
}
private void SaveOriginalSizes()
{
originalFormSize = Size;
controlsToResize.Add(panel1);
controlsToResize.Add(panel2);
...
originalLocations = new List<Point>(controlsToResize.Count);
originalSizes = new List<Size>(controlsToResize.Count);
foreach (Control c in controlsToResize)
{
originalLocations.Add(c.Location);
originalSizes.Add(c.Size);
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
float scaleX = (float)originalFormSize.Width / Size.Width;
float scaleY = (float)originalFormSize.Height / Size.Height;
for (int i = 0; i < controlsToResize.Count; i++)
{
Control c = controlsToResize[i];
UpdatePosition(c, originalLocations[i], scaleX, scaleY);
UpdateSize(c, originalSizes[i], scaleX, scaleY);
}
}
}
...Where obviously you would have to implement the UpdatePosition
and UpdateSize
methods on your own - I'm assuming you already have some sort of implementation.
Honestly, it's pretty god-awful. I would strongly recommend that instead of trying to do any of this, you revisit whatever requirements/design constraints are preventing you from using the Anchor
and Dock
properties. In all my years, I don't think I've ever heard of a legitimate reason for not using either these or the layout controls TableLayoutPanel
/FlowLayoutPanel
or both. Those are the way to deal with layouts in WinForms; anything else is a clumsy hack.