// class with calculate size and position of control.
class UIElement
{
// those properties get the Control current size, font, position
public Control Control { get; set; }
public Size OriginalSize { get; set; }
public Font OriginalFont { get; set; }
public Point OriginalPosition { get; set; }
public void AdjustSize(System.Drawing.Size originalSize, System.Drawing.Size destinationSize)
{
//we need to calculate destination size
System.Drawing.Size size = new Size();
size.Width = destinationSize.Width * OriginalSize.Width / originalSize.Width;
size.Height = destinationSize.Height * OriginalSize.Height / originalSize.Height;
Control.Size = size;
//now lets caculate positions
System.Drawing.Point position = new Point();
position.X = destinationSize.Width * OriginalPosition.X / originalSize.Width;
position.Y = destinationSize.Height * OriginalPosition.Y / originalSize.Height;
Control.Location = position;
Font font = new Font(OriginalFont.FontFamily, destinationSize.Height * OriginalFont.Size / originalSize.Height, OriginalFont.Style, OriginalFont.Unit);
//adjusting font size
Control.Font = font;
if (Control is DataGridView)
{
DataGridView grid = (DataGridView)Control;
grid.ColumnHeadersHeight = destinationSize.Height * 20 / originalSize.Height;
grid.RowTemplate.Height = destinationSize.Height * 22 / originalSize.Height;
}
}
}
public partial class FormBase : Form
{
List<UIElement> UIElements { get; set; }
public FormBase()
{
InitializeComponent();
//we need to store ids of all controls found on the form
UIElements = new List<UIElement>();
AddControls(this.Controls);
}
// recursive function to load all child controls.
public void AddControls(Control.ControlCollection controls)
{
foreach (Control control in controls)
{
UIElement element = new UIElement();
element.Control = control;
element.OriginalFont = control.Font;
element.OriginalPosition = control.Location;
element.OriginalSize = control.Size;
UIElements.Add(element);
if (control.Controls.Count > 0)
AddControls(control.Controls);
}
}
// add key board command to window, "Ctrl + A"
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Control | Keys.A:
// Execute the Ctrl + C button action
ProgramValues.IsAlarm = true;
// do some thing here ...
// .......
return true;
default:
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
// this function register to event ResizeEnd, may be also need called when the form load.
internal void FormBase_ResizeEnd(object sender, EventArgs e)
{
Size original = new Size(472, 388);
//Size original = ProgramValues.Original;
//making sure that new size is not less than original size
if (this.Size.Width < original.Width)
this.Size = original;
//scaling new size proportionally
this.Size = new Size(this.Size.Width, this.Size.Width * original.Height / original.Width);
//we need to adjust the size of all controls
foreach (UIElement element in UIElements)
{
element.AdjustSize(original, this.Size);
}
// remember the size to some where.
ProgramValues.CurrentSize = new Size(this.Size.Width, this.Size.Height);
if (this.Visible)
{
ProgramValues.CurrentLocation = new Point(this.Location.X, this.Location.Y);
}
}
}