Introduction
Designing Windows application that Fits in all resolutions will become necessary some times.Here i am explaining how you can do this.
Background
I have seen most of the applications that are designed in the previous time is of resolution 800 * 600 if you try to see that in resolution like 1200 * 800 it will not be that much clear and as the resolutions are increasing further it will become tough time for applications that are designed in lower resolutions.So i feel we Must develop our applications that can be well suited in all resolutions so that our client will not come with modification year to year as he is adopting all the technological changes.
Using the code
Here is how i Made my approach. First i started to get the Ratios of Designed Height to Current Height, and Designed Width to Current Width and using that ratios i am calculating the present height & width that must be in current resolution.But after that i came across Microsoft's AutoScale then i used that and it is working fine, But only one Problem is in Heigher Resolution i am not able to read the text on the form so i increased the font while setting the font family as the same.
I prefer to write the required form resizing code in another class and a method which contains all the required code. So that we can call that function in form load.I used this one in utility and called this because it will become large process if you want to modify this code as you need to modify in all forms. In my project i have 65 Windows Project modules so i used this as a Class.
Declare these Variables
float f_HeightRatio = new float();
float f_WidthRatio = new float();
ResizeForm(Form ObjForm, int DesignerHeight, int DesignerWidth)</code)
#region Code for Resizing and Font Change According to Resolution
int i_StandardHeight = DesignerHeight;
int i_StandardWidth = DesignerWidth;
int i_PresentHeight = Screen.PrimaryScreen.Bounds.Height;int i_PresentWidth = Screen.PrimaryScreen.Bounds.Width;f_HeightRatio = (float)((float)i_PresentHeight / (float)i_StandardHeight);
f_WidthRatio = (float)((float)i_PresentWidth / (float)i_StandardWidth);
ObjForm.AutoScaleMode = AutoScaleMode.None;ObjForm.Scale(new SizeF(f_WidthRatio, f_HeightRatio));
foreach (Control c in ObjForm.Controls)
{
if (c.HasChildren)
{
ResizeControlStore(c);
}
else
{
c.Font = new Font(c.Font.FontFamily, c.Font.Size * f_HeightRatio, c.Font.Style, c.Font.Unit, ((byte)(0)));
}
}
ObjForm.Font = new Font(ObjForm.Font.FontFamily, ObjForm.Font.Size * f_HeightRatio, ObjForm.Font.Style, ObjForm.Font.Unit, ((byte)(0)));
#endregion
ResizeControlStore(Control objCtl)
if (objCtl.HasChildren)
{
foreach (Control cChildren in objCtl.Controls)
{
if (cChildren.HasChildren)
{
ResizeControlStore(cChildren);
}
else
{
cChildren.Font = new Font(cChildren.Font.FontFamily, cChildren.Font.Size * f_HeightRatio, cChildren.Font.Style, cChildren.Font.Unit, ((byte)(0)));
}
}
objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * f_HeightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
}
else
{
objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * f_HeightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
}
How to use it in your application
- Step 1:Take the code file that is included in the source and add that in any of your comman class library(ex:Utility)
- Step 2:Add reference of the library to your project.
- Step 3:In the Form_Load function Create Instance of the class.
- Step 4:Call the Function ResizeForm by passing 3 Parameters(Form,DesignerHeight,DesignerWidth ex:objFormResizer(this,864,1152);
Points of Interest
Frankly speaking to complete this it is taken 3 days to me as the project that this is to be implemented consists of 60+ projects so i felt so sad and made my self to leave this task.(Previously Developers are designing the form in 3 to 4 common resolutions and in code they are putting these by copying from system default form generated code it is very large process and also if design changed and developer forgot to modify this then think of the form..) But due to this i came across so many design considerations in windows forms.
Thanks to all.
Developers and Senior Software people's suggestions are always accepted.I request the comments from all as this is my first posting.Please excuse me and come to me if any Mistakes are there.