[dba-VS] Visual Basic 2012 - Change Form & Controls Based On Screen Resolution

Salakhetdinov Shamil mcp2004 at mail.ru
Sun Apr 5 20:08:59 CDT 2015


 Hi Paul --

I'm assuming you're asking about .NET WinForms.
The P.S. code I have "cooked" here should make the trick for your case.
But I must note (next time) you'd better try to design your forms based on lowest possible width and height values and use Dock and Anchor standard properties to automatically handle form's and controls' resizing/enlarging.
See also:   http://tinyurl.com/q3365r8

I, personally, have never used custom .NET WinForms forms'  resizing functionality during my 10+ years of .NET programming practice.

Thank you.

-- Shamil

P.S.

1. Create WindowsFormsApplication1 in Visual Studio;
2. Form1 will be automatically created;
3. Drag and drop a few test controls on Form1;
4. Replace Form1's code behind with the following code (note: there are two custom classes ControlResizer and FormControlsResizer - they can be put in separate source file(s) ):

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// define non-zero values for the following designWidth and designHeight variables 
// equal to the form's design time width and height only
// in the case if run-time system resolution could have less width and/or height
// dimensions than design time form's width and height
int designWidth = 800;
int designHeight = 600;
if (designWidth != this.Width || designHeight != this.Height)
{
// activate resizing functionality only when run-time's form instance actual width and height 
// have less width and/or height dimensions than design time form's width and height
_resizer = new FormControlsResizer(this, designWidth, designHeight);
}
}
private FormControlsResizer _resizer;
}
public class ControlResizer
{
protected Control _control;
protected Dictionary<string, ControlResizer> _controls;
public ControlResizer(Dictionary<string, ControlResizer> controls, Control control)
{
if (IsInDesignMode) return;
_controls = controls;
_control = control;
this.Name = control.Name;
this.BaseWidth = control.Width;
this.BaseHeight = control.Height;
this.BaseTopPosition = control.Top;
this.BaseLeftPosition = control.Left;
this.FontFamily = control.Font.FontFamily;
this.BaseFontSize = control.Font.SizeInPoints;
control.Dock = DockStyle.None;
control.Anchor = AnchorStyles.Top | AnchorStyles.Left;
foreach (Control childControl in control.Controls)
{
this.Name = makeMostlyUniqueName(control, childControl);
if (_controls.ContainsKey(this.Name)) continue;
_controls.Add(this.Name, new ControlResizer(_controls, childControl));
}
}
/// <remarks>
/// This method will result in names collisions for the rare(?) cases
/// of different custom controls having similar nested custom controls:
/// to avoid any probability of the names' collisions the whole controls
/// hierarchy should be taken into account when generating unique names.
/// </remarks> 
protected string makeMostlyUniqueName(Control parentControl, Control childControl)
{
return string.Format("{0}*{1}", parentControl.Name, childControl.Name); 
}
public string Name { get; private set; }
public double BaseWidth { get; protected set; }
public double BaseHeight { get; protected set; }
public double BaseTopPosition { get; private set; }
public double BaseLeftPosition { get; private set; }
public FontFamily FontFamily { get; private set; }
public float BaseFontSize { get; private set; }

public void Resize(double scaleWidth, double scaleHeight)
{
_control.Width = (int)(this.BaseWidth * scaleWidth);
_control.Height = (int)(this.BaseHeight * scaleHeight);
_control.Left = (int)(this.BaseLeftPosition * scaleWidth);
_control.Top = (int)(this.BaseTopPosition * scaleHeight);
_control.Font = new Font(this.FontFamily, (float)(this.BaseFontSize * scaleHeight));
}
public static bool IsInDesignMode
{
get
{
if (Application.ExecutablePath.IndexOf("devenv.exe", StringComparison.OrdinalIgnoreCase) > -1)
{
return true;
}
return false;
}
}
}
public class FormControlsResizer : ControlResizer
{
private Form _form;
public FormControlsResizer(Form form, int baseWidth = 0, int baseHeight = 0)
: base(new Dictionary<string, ControlResizer>(), form)
{
if (IsInDesignMode) return;

_form = form;
if (baseWidth > 0) this.BaseWidth = baseWidth;
if (baseHeight > 0) this.BaseHeight = baseHeight;
form.Resize += Form_Resize;
Form_Resize(this, new EventArgs());
}
private void Form_Resize(object sender, EventArgs e)
{
double scaleWidth = _form.Width / this.BaseWidth;
double scaleHeight = _form.Height / this.BaseHeight;
resizeControls(_form, scaleWidth, scaleHeight);
}
private void resizeControls(Control parentControl, double scaleWidth, double scaleHeight)
{
foreach (Control control in parentControl.Controls)
{
resizeControls(control, scaleWidth, scaleHeight);
string name = makeMostlyUniqueName(parentControl, control);
if (!_controls.ContainsKey(name)) continue; 
_controls[name].Resize(scaleWidth, scaleHeight);
}
}
}
}

Sunday, April  5, 2015 4:54 AM +01:00 from Paul Hartland <paul.hartland at googlemail.com>:
>going back to my earlier question, can anyone point me in the direction of
>some decent code etc that will resize the forms and controls please...
>
>Paul
<<< skipped >>>
>


More information about the dba-VS mailing list