Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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..

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

}
}

Edited by PlausiblyDamp
  • 7 months later...
Posted

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!

Posted

Most likelly you don't have the System.Drawing reference.

Just go to the References and add it.

 

Alex :p

Software bugs are impossible to detect by anybody except the end user.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...