In this article we are going to see the how to resize the winforms and there controls and the font size based on the resolution of the system. For that we are going to create a component which will support the design time , where we can supply the value to the component in design time.
Resize Manager a component which will resize the form and the controls along with font size. For do this create a two properties in menu item which will accept the form as container and type of resolution at the design time.
For ex : if we design a winforms in 1024x768 then if we run this application in 1366x768 resolution system the resize manager will auto resize the forms and controls along with font size.
For this create a Form and drag the Four buttons inside a panel and now drag and drop the resize manager component from the toolbox and set the property of container with form and set the Resolution of current system.
Above image shows how the component sees in Toolbox of visual studio,
After drag and drop the component and select the properties, now the above image specifies what are the values to be specified, Set the Form1 in the Container and R1600X900 in Resolution.
Code for Resize Manager :
Resize Manager Reads the each and every control and set the value of size and font value from the base size.
Form Desinger :
namespace ResizeCom
{
partial class Form1
{
/// <summary>
/// Required designer
variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources
being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise,
false.</param>
protected override void Dispose(bool
disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region
Windows Form Designer generated code
/// <summary>
/// Required method for
Designer support - do not modify
/// the contents of this
method with the code editor.
/// </summary>
private void
InitializeComponent()
{
this.components = new
System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager
resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.button1 = new
System.Windows.Forms.Button();
this.button2 = new
System.Windows.Forms.Button();
this.button3 = new
System.Windows.Forms.Button();
this.button4 = new
System.Windows.Forms.Button();
this.button5 = new
System.Windows.Forms.Button();
this.button6 = new
System.Windows.Forms.Button();
this.button7 = new
System.Windows.Forms.Button();
this.button8 = new
System.Windows.Forms.Button();
this.resizeManager1 = new
ResizeCom.ResizeManager(this.components);
((System.ComponentModel.ISupportInitialize)(this.resizeManager1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button1.BackgroundImage")));
this.button1.BackgroundImageLayout =
System.Windows.Forms.ImageLayout.Stretch;
this.button1.FlatAppearance.BorderSize = 0;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new
System.Drawing.Point(64, 66);
this.button1.Name = "button1";
this.button1.Size = new
System.Drawing.Size(101, 88);
this.button1.TabIndex = 0;
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.BackgroundImage = global::ResizeCom.Properties.Resources.icon3;
this.button2.BackgroundImageLayout =
System.Windows.Forms.ImageLayout.Stretch;
this.button2.FlatAppearance.BorderSize = 0;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new
System.Drawing.Point(278, 66);
this.button2.Name = "button2";
this.button2.Size = new
System.Drawing.Size(101, 88);
this.button2.TabIndex = 1;
this.button2.UseVisualStyleBackColor = true;
//
// button3
//
this.button3.BackgroundImage = global::ResizeCom.Properties.Resources.icon2;
this.button3.BackgroundImageLayout =
System.Windows.Forms.ImageLayout.Stretch;
this.button3.FlatAppearance.BorderSize = 0;
this.button3.FlatStyle
= System.Windows.Forms.FlatStyle.Flat;
this.button3.Location = new
System.Drawing.Point(171, 66);
this.button3.Name = "button3";
this.button3.Size = new
System.Drawing.Size(101, 88);
this.button3.TabIndex = 2;
this.button3.UseVisualStyleBackColor = true;
//
// button4
//
this.button4.BackgroundImage = global::ResizeCom.Properties.Resources.icon4;
this.button4.BackgroundImageLayout =
System.Windows.Forms.ImageLayout.Stretch;
this.button4.FlatAppearance.BorderSize = 0;
this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button4.Location = new
System.Drawing.Point(385, 66);
this.button4.Name = "button4";
this.button4.Size = new
System.Drawing.Size(101, 88);
this.button4.TabIndex = 3;
this.button4.UseVisualStyleBackColor = true;
//
// button5
//
this.button5.BackColor = System.Drawing.SystemColors.Highlight;
this.button5.FlatAppearance.BorderSize = 0;
this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button5.Font = new
System.Drawing.Font("Rockwell",
12F);
this.button5.Location = new
System.Drawing.Point(278, 160);
this.button5.Name = "button5";
this.button5.Size = new
System.Drawing.Size(101, 88);
this.button5.TabIndex = 6;
this.button5.Text = "LogOff";
this.button5.UseVisualStyleBackColor = false;
//
// button6
//
this.button6.BackgroundImage = global::ResizeCom.Properties.Resources.icon5;
this.button6.BackgroundImageLayout =
System.Windows.Forms.ImageLayout.Stretch;
this.button6.FlatAppearance.BorderSize = 0;
this.button6.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button6.Location = new
System.Drawing.Point(171, 161);
this.button6.Name = "button6";
this.button6.Size = new
System.Drawing.Size(101, 88);
this.button6.TabIndex = 5;
this.button6.UseVisualStyleBackColor = true;
//
// button7
//
this.button7.BackColor = System.Drawing.Color.MidnightBlue;
this.button7.BackgroundImageLayout =
System.Windows.Forms.ImageLayout.Stretch;
this.button7.FlatAppearance.BorderSize = 0;
this.button7.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button7.Font = new
System.Drawing.Font("Rockwell",
12F);
this.button7.ForeColor = System.Drawing.SystemColors.Control;
this.button7.Location = new
System.Drawing.Point(64, 160);
this.button7.Name = "button7";
this.button7.Size = new
System.Drawing.Size(101, 88);
this.button7.TabIndex = 4;
this.button7.Text = "Start
Up";
this.button7.UseVisualStyleBackColor = false;
//
// button8
//
this.button8.BackgroundImage = global::ResizeCom.Properties.Resources.icon6;
this.button8.BackgroundImageLayout =
System.Windows.Forms.ImageLayout.Stretch;
this.button8.FlatAppearance.BorderSize = 0;
this.button8.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button8.Font = new
System.Drawing.Font("Rockwell",
12F);
this.button8.Location = new
System.Drawing.Point(505, 66);
this.button8.Name = "button8";
this.button8.Size = new
System.Drawing.Size(156, 182);
this.button8.TabIndex = 7;
this.button8.UseVisualStyleBackColor = true;
//
// resizeManager1
//
this.resizeManager1.Container = this;
this.resizeManager1.Resolution = ResizeCom.ScreenResolution.R1600X900;
//
// Form1
//
this.AutoScaleDimensions = new
System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new
System.Drawing.Size(728, 365);
this.Controls.Add(this.button8);
this.Controls.Add(this.button5);
this.Controls.Add(this.button6);
this.Controls.Add(this.button7);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Font = new
System.Drawing.Font("Microsoft
Sans Serif", 8F);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new
System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.resizeManager1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private ResizeManager
resizeManager1;
private System.Windows.Forms.Button
button5;
private System.Windows.Forms.Button
button6;
private System.Windows.Forms.Button
button7;
private System.Windows.Forms.Button
button4;
private System.Windows.Forms.Button
button3;
private System.Windows.Forms.Button
button2;
private System.Windows.Forms.Button
button1;
private System.Windows.Forms.Button
button8;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace ResizeCom
{
public partial class ResizeManager
: Component,ISupportInitialize
{
private ScreenResolution
resolution;
public ScreenResolution
Resolution
{
set { resolution = value;
}
get { return
resolution; }
}
private ContainerControl container = null;
public ContainerControl
Container
{
set
{
container = value;
}
get {
return container;
}
}
public ResizeManager()
{
InitializeComponent();
}
public ResizeManager(IContainer
container)
{
container.Add(this);
InitializeComponent();
}
public void BeginInit()
{
}
public void EndInit()
{
Size current = Screen.PrimaryScreen.Bounds.Size;
Size original = ReValue(Resolution);
if (current.Height != original.Height ||
current.Width != original.Width)
{
int per=CalculateResolution(current);
Resize(Resolution,Container,per);
}
}
public int
CalculateResolution(Size display)
{
return (int)(Enum.Parse(typeof(ScreenResolution),("R"
+ display.Width + "X" +
display.Height)));
}
public Size
ReValue(ScreenResolution sol)
{
string []si = sol.ToString().Substring(1).Split('X');
return new Size(Convert.ToInt32(si[0]),
Convert.ToInt32(si[1]));
}
public void Resize(ScreenResolution resolution,object element,int
percentage)
{
double value = (double)((percentage)
% 100) / 100;
if (element is ContainerControl || element is ScrollableControl)
{
ArrayList arr;
if (element is
ContainerControl)
{
ContainerControl containercntrl =
(element as ContainerControl);
arr = new ArrayList(containercntrl.Controls);
containercntrl.Width = containercntrl.Width + (int)((float)containercntrl.Width*value);
containercntrl.Height = containercntrl.Height + (int)((float)containercntrl.Height
* value);
//containercntrl.Font = new
Font(containercntrl.Font.FontFamily, containercntrl.Font.Size +
(int)(containercntrl.Font.Size * value));
}
else
{
ScrollableControl scrollcntrl =
(element) as ScrollableControl;
arr = new ArrayList(scrollcntrl.Controls);
scrollcntrl.Width = scrollcntrl.Width + (int)((float)scrollcntrl.Width * value);
scrollcntrl.Height = scrollcntrl.Height + (int)((float)scrollcntrl.Height * value);
scrollcntrl.Location = new Point(scrollcntrl.Location.X + (int)((float)scrollcntrl.Location.X
* value), scrollcntrl.Location.Y + (int)((float)scrollcntrl.Location.Y * value));
//scrollcntrl.Font = new
Font(scrollcntrl.Font.FontFamily, scrollcntrl.Font.Size +
(int)(scrollcntrl.Font.Size * value));
}
foreach
(Control cntrl in
arr)
{
cntrl.Width = cntrl.Width + (int)((float)cntrl.Width * value);
cntrl.Height = cntrl.Height +(int) ((float)cntrl.Height * value);
cntrl.Font =new Font(cntrl.Font.FontFamily,cntrl.Font.Size
+(int) (cntrl.Font.Size *value));
cntrl.Location = new Point(cntrl.Location.X + (int)((float)cntrl.Location.X * value), cntrl.Location.Y + (int)((float)cntrl.Location.Y
* value));
if (cntrl.Controls.Count > 0)
{
Resize(Resolution,
cntrl, percentage);
}
}
}
}
}
public enum ScreenResolution : int
{
R1920X1080=125,
R1600X900=150,
R1366X768=100,
}
}
To change the Percentage of current resolution change the values in Enum.
OUTPUT:
Form for R1024X768:
Form for R1366X768 :
From this article you can learn how to change the form size and the font size of each controls based on resolution.
No comments:
Post a Comment