//Scales the Form and all controlls, Resolution independant.
#region Scaling
private void CallScale()
{
if(Screen.PrimaryScreen.WorkingArea.Height != 768 || Screen.PrimaryScreen.WorkingArea.Width != 1024)
{
IterateControls(this);
}
}
private void IterateControls(Control ParentControl)
{
foreach(Control ctl in ParentControl.Controls)
{
FinanceScale(ctl,112);
if(ctl.Controls.Count > 0)
{
IterateControls(ctl);
}
}
}
private void FinanceScale(Control Ncontrol, int intModifier)
{
double ratioWidth = (double)1024/(double)Screen.PrimaryScreen.WorkingArea.Width;
double ratioHeight = (double)768/(double)Screen.PrimaryScreen.WorkingArea.Height;
ratioWidth = ratioWidth * 100;
ratioHeight = ratioHeight * 100;
Control ctrl = Ncontrol;
Application.DoEvents();
if(ctrl.GetType().ToString() != "System.Windows.Forms.Label" && ctrl.GetType().ToString() != "System.Windows.Forms.Button")
{
ScaleBackGround((int)((double)ratioWidth),(int)((double)ratioHeight),ctrl.BackgroundImage,ctrl);
}
else
{
switch(ctrl.GetType().ToString())
{
case "System.Windows.Forms.Label":
Label lblImg = (System.Windows.Forms.Label)ctrl;
if(lblImg.Image != null)
{
ScaleLabel((int)((double)ratioWidth),(int)((double)ratioHeight),lblImg.Image,lblImg);
}
break;
case "System.Windows.Forms.Button":
Button btnImg = (System.Windows.Forms.Button)ctrl;
if(btnImg.Image != null)
{
ScaleBackGround((int)((double)ratioWidth),(int)((double)ratioHeight),btnImg.BackgroundImage,btnImg);
}
if(btnImg.BackgroundImage != null)
{
ScaleButton((int)((double)ratioWidth),(int)((double)ratioHeight),btnImg.Image,btnImg);
}
break;
}
}
float fl = (float)ctrl.Font.Size;
float flNew_emSize = 0;
if(Screen.PrimaryScreen.WorkingArea.Height <= 768 && Screen.PrimaryScreen.WorkingArea.Width <= 1024 && ratioHeight != ratioWidth)
{
flNew_emSize = fl * (float)(ratioWidth /ratioHeight);
}
else if(Screen.PrimaryScreen.WorkingArea.Height <= 768 && Screen.PrimaryScreen.WorkingArea.Width <= 1024)
{
if(ctrl.GetType().ToString() == "System.Windows.Forms.ComboBox")
{
flNew_emSize = ((fl * 100) / (float)ratioHeight) + 5f;
}
else
{
flNew_emSize = (fl * 100) / (float)ratioHeight;
}
}
else if(Screen.PrimaryScreen.WorkingArea.Height != 768)
{
flNew_emSize = fl * (float)(ratioHeight / ratioWidth);
}
else
{
flNew_emSize = fl;
}
//resize controls to get accurate width
ctrl.Height = Convert.ToInt32(Convert.ToDouble(ctrl.Height*100) / ratioHeight);
ctrl.Width = Convert.ToInt32(Convert.ToDouble(ctrl.Width*100) / ratioWidth);
ctrl.Left = Convert.ToInt32(Convert.ToDouble(ctrl.Left*100) / ratioWidth);
ctrl.Top = Convert.ToInt32(Convert.ToDouble(ctrl.Top*100) / ratioHeight);
ctrl.Font = new Font(ctrl.Font.FontFamily,(float)flNew_emSize,ctrl.Font.Style);
if(ctrl.Text != "")
{
Brush b = Brushes.Black;
string s = ctrl.Text;
while(((float)s.Length * ctrl.Font.Size) > ctrl.Width)
{
ctrl.Font = new Font(ctrl.Font.FontFamily,ctrl.Font.Size - 1,ctrl.Font.Style);
}
IntPtr ptr = ctrl.Handle;
Graphics g = Graphics.FromHwnd(ptr);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawString(s,ctrl.Font,b,ctrl.ClientRectangle);
}
}
#endregion
public static void ScaleLabel(int RatioWidth,int RatioHeight,Image imImage, Label lblImage)
{
if(imImage != null)
{
//int ScaleValue = Ratio;
Bitmap SourceBitmap = new Bitmap(imImage);
Bitmap TargetBitmap = new Bitmap((SourceBitmap.Width * 100) / RatioWidth,(SourceBitmap.Height * 100)/RatioHeight);
Graphics bmpGraphics = Graphics.FromImage(TargetBitmap);
//Set Drawing Quality
bmpGraphics.InterpolationMode = InterpolationMode.Bicubic;
bmpGraphics.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle compressionRectangle = new Rectangle(0, 0, ((SourceBitmap.Width * 100) / RatioWidth), ((SourceBitmap.Height * 100) / RatioHeight));
bmpGraphics.DrawImage(SourceBitmap, compressionRectangle);
lblImage.Image = TargetBitmap;
}
}
public static void ScaleButton(int RatioWidth,int RatioHeight,Image imImage, Button btnImage)
{
if(imImage != null)
{
//int ScaleValue = Ratio;
Bitmap SourceBitmap = new Bitmap(imImage);
Bitmap TargetBitmap = new Bitmap((SourceBitmap.Width * 100) / RatioWidth,(SourceBitmap.Height * 100)/RatioHeight);
Graphics bmpGraphics = Graphics.FromImage(TargetBitmap);
//Set Drawing Quality
bmpGraphics.InterpolationMode = InterpolationMode.Bicubic;
bmpGraphics.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle compressionRectangle = new Rectangle(0, 0, ((SourceBitmap.Width * 100) / RatioWidth), ((SourceBitmap.Height * 100) / RatioHeight));
bmpGraphics.DrawImage(SourceBitmap, compressionRectangle);
btnImage.Image = TargetBitmap;
}
}
public static void ScaleBackGround(int RatioWidth, int RatioHeight,Image imImage, Control ctrl)
{
if(imImage != null)
{
//int ScaleValue = Ratio;
Bitmap SourceBitmap = new Bitmap(imImage);
Bitmap TargetBitmap = new Bitmap((SourceBitmap.Width * 100) / RatioWidth,(SourceBitmap.Height * 100)/RatioHeight);
Graphics bmpGraphics = Graphics.FromImage(TargetBitmap);
//Set Drawing Quality
bmpGraphics.InterpolationMode = InterpolationMode.Bicubic;
bmpGraphics.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle compressionRectangle = new Rectangle(0, 0, ((SourceBitmap.Width * 100) / RatioWidth), ((SourceBitmap.Height * 100) / RatioHeight));
bmpGraphics.DrawImage(SourceBitmap, compressionRectangle);
ctrl.BackgroundImage = TargetBitmap;
}
}