Resize Form To Resolution

Sorry all...I've been very busy at work trying to rush product to the market. As soon as things slow down I'll finish up the class. I have a VB app that also needs the activeX control..I've still yet to study up on interopt to create the activeX control...I do have a C# class already complete..if you would like I could post it ..let me know. the font resizing is still a little quirky..if anyone has any solid info on resizing fonts..I would be greatly in your debt. Fonts are tricky as they are made up of variuos elements..and also depend on dpi. http://www.hut.fi/u/hsivonen/units.html and
http://msdn.microsoft.com/library/d...pp/GDIPlus/UsingGDIPlus/UsingTextandFonts.asp

TIA
 
I would appreciate it if you can post the C# class. I am afraid I may not be of much help with the fonts but I will try to play with your class and search over the web also to find some pointers. Thanks a lot!!
 
Copy this code out place in a new class name it Resize. See the comment below place the name space of your project name in place of the comment. Compile as a dll..reference your dll...create an instance of the class and simply call CallScale(this)..passing this, meaning your form, as the control...it will iterate through all controls on your form..including controls contained in other container controls. In the code resizing the fonts you'll notice a 5f..this float can be played with a bit to get different results..If you've any questions please reply!
C#:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace //Place your NameSpace here!!
{
	/// <summary>
	/// Summary description for Resize.
	/// </summary>
	public class Resize
	{
		public Resize()
		{
			//
			// TODO: Add constructor logic here
			//

		}

		//Scales the Form and all controlls, Resolution independant.
		#region Scaling

		public void CallScale(Control m_Ctrl)
		{
			if(Screen.PrimaryScreen.WorkingArea.Height != 768 || Screen.PrimaryScreen.WorkingArea.Width != 1024)
			{
				IterateControls(m_Ctrl);	
			}
		}

		private void IterateControls(Control ParentControl)
		{
			foreach(Control ctl in ParentControl.Controls)
			{
				ScaleForm(ctl);
				if(ctl.Controls.Count > 0)
				{
					IterateControls(ctl);
				}
			}
		}

		private void ScaleForm(Control Ncontrol)
		{
			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);
			}
				

		}

		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;
			}
		}
		#endregion

	}
}
 
Last edited by a moderator:
One more thing...this class if you've noticed...was designed to resize FROM 1024 X 768...If you have designed your application in another resolution simply replace all occurances of 1024 and 768 with the correct resolution
 
Wow it's been a year!

Sorry life has been very busy..anyhow I have almost no time in the past year to look into this code. I have fixed one little area that I came across..what happens if you have a label that is using Word wrapping?..so I re-thought the way I resized fonts in relation to the container control .. this code will still have issues at extremely high irregular resolutions ... and you should watch out for the text on cramped labels ... My solution to limited text space is somtimes I have to play with the Z order of a labeel as well as the length...meaning I might need to make the label longer ..and hide the extra length under adjacent controls..
C#:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace ResolutionResize
{
	/// <summary>
	/// Summary description for Resize.
	/// </summary>
	public class Resize
	{
		public Resize()
		{			
		}

		//Scales the Form and all controlls, Resolution independant.
		#region Scaling

		public void CallScale(Control m_Ctrl)
		{
			if(Screen.PrimaryScreen.WorkingArea.Height != 768 || Screen.PrimaryScreen.WorkingArea.Width != 1024)
			{
				IterateControls(m_Ctrl);	
			}
		}

		private void IterateControls(Control ParentControl)
		{
			foreach(Control ctl in ParentControl.Controls)
			{
				ScaleForm(ctl);
				if(ctl.Controls.Count > 0)
				{
					IterateControls(ctl);
				}
			}
		}

		private void ScaleForm(Control Ncontrol)
		{
			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.Height)
				{
					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);
			}
				

		}

		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;
			}
		}
		#endregion

	}
}
 
Last edited by a moderator:
having problem building the class file :(

Hie Drutchlin and all,

Hi, i have the problem as in the thread. I tried to build the class file using vs.net but having some errors (10 errors) : e.g.
" The type or namespace name 'Drawing' does not exist in the class or namespace 'System' (are you missing an assembly reference?) "

Can somebody help me with this problem? actually im developing using VB.NET. To convert this file class from C# to VB.NET, can someone show me the way or any tools to use ?

Much appreciation and thanks for replies and help. :-\

Steven,
VB.NET beginner!
 
Back
Top