Resize Form To Resolution

Top Score Score

Newcomer
Joined
Aug 25, 2003
Messages
17
Hi,

How can I make it so that no matter what screen resolution my application runs on, that it resizes the forms and all the controls to work with that resolution.

I am so close to getting it out onto the market but this one thing is holding me up.

The application was build on 1024 by 768 and needs to run on 800 by 600.

Can someone please give me a hand.

Thanks.....Dale
 
I think with this you'll have some troubles... specially making the controls smaller...

The point is that certain controls are not user dimensionable (as long as I know), like the ComboBox for example! You'll allways have to play with the font size.

This method brings a new issue... the height of the combobox actualy really changes in integer gaps...

If the issue is to have a windows always maximized in diferent resolutions it's a easy task, but if you want to let the user resize the windows freelly you'll have trouble...
 
I didn't read the complete article but what I undestood from it and from what Top Score asked it's not quite the same thing...

I think he wants to resize the complete layout, including the font size on the controls... like chaging the size but keeping the aspect ratio...

Am I wrong?

And that aspect ratio is the thing that brings more troubles... :)

If put to work wold be a very nice tool ...!
 
Hey,

I have seen that article a dozen times, I have read through it but it is not exactly what I need, you are right.

Yeah the fonts don't need resizing, they may I will just have to check. Although it might be best If I did??

But yeah the whole form including the controlls.

I have tried other methods like anhoring, when I put in on another computer the form I tested it on just goes blank and every loses its size back down to a small form. And this is a full screen form :confused:

And just to confirm it is when the window automatically adjusts to the size of the screen, not for user resizing.

I use VB.Net 2002

I aggree if this could work then it would fix alot of peoples problems, thats for sure.

I await the reply

Dale
 
Last edited:
I really don't know how you'll do this...

The only real problem is in the controls that are not sizable...
I told you... the textbox and the combobox are the two better examples! You can't change their height without changing their font size!

I think you can ajust the components on 800x600 and 1024x768 and make some kind of ratio to apply to the components sizes to make them grow or shrink with the form size beetwin theese too values... get it?

I think its the only way...
 
AlexCode said:

I think you can ajust the components on 800x600 and 1024x768 and make some kind of ratio to apply to the components sizes to make them grow or shrink with the form size beetwin theese too values... get it?

Yeah this is basicaly what i want to do. I am still learning .Net and I am only 16, are you able to give me the sort of code that I could use??

Thanks.........Dale
 
Resizing with a Ratio

I am working on a class that iterates through every control on a form, resizing and repositioning every control. I wouldn't mind a little help in the Font department though. I'm sooo close to finding an equation to universally resize the fonts by breaking the font into it's composite peices ..resizing then reassembling them.
Anyhow to get you pointed in the right direction say you designed the forms in 1024 X 768..and you are going to switch resolution to 800 X 600...1024/800 = 1.28 and 768/600 = 1.28 now you have your 2 ratios for both width and height. You can use these ratios to resize (current height = 100, so 100 / 1.28 = 78.125 <-- your new height) and (top = 548, so 548 / 1.28 = 428.125)...Now as you know sizes often are expressed in floats so you'll have to do quite a bit of casting and numerical conversions..as you'll see when I post my code later. And of course when I refine it I will post it on Winforms.com for your coding pleasure =D.
 
I think this should get you started...
It's far from "final version" but will give you something to start!

Visual Basic:
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Label1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(32, 32)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(136, 24)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "AlexCode"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(632, 446)
        Me.Controls.Add(Me.Label1)
        Me.MinimumSize = New System.Drawing.Size(640, 480)
        Me.Name = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    'All the standard resolutions have the same ratio... so a choose one of them
    Dim ratio As Double = 640 / 480     'the result it's = 1,3(3)
    Dim fSize As Single = 8.25!         'This is the start size os the label font

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize

        'Preserve Form ratio... it flicks a bit :(
        'This it's needed because I just couldn't resize the font just horizontally or just vertically
        'it has to be both the same time...
        Me.Height = Me.Width / ratio

        fSize = 8.25! * (Me.Width / 640)

        'To set a new font size you have to set the complete font property...
        Me.Label1.Font = New System.Drawing.Font _
            ("Tahoma", fSize, System.Drawing.FontStyle.Regular, _
            System.Drawing.GraphicsUnit.Point, CType(0, Byte))

    End Sub

End Class


Feel free to ask anything...

and btw, sorry if I didn't reply you but I didn't get your last post on my e-mail...

:D give it a shot!
 
OK as promised here's my super sloppy Resizing Code...I've added a few lines that basically just resizes your text to fit in your controls. This would not be a good idea if your using word wrapping..Most likely you'll want to comment it out. Speaking of comments ..this is a work in progress and I'm not one for inserting comments in my test code. Hope you all can Disect reconnect and resurrect this snippet to serve your resizing needs. When I have completed my code including placing it in a more readable and usefull Class..I'll repost.
C#:
//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;
			}
		}
 
Last edited by a moderator:
P.S. I place this code in my constructor of my form..it should detect the current screen size excluding your Task bar and any other utility bars i.e. MSOffice bar etc..I don't suggest ever calling this code from a paint event as it will most likely cause an infinite loop or other unexpected results
 
Any questions or comments ,other than why I have posted 4 times in a row when I could have obviously included everything in one post ;)~, are welcome. SIA If I don't always have time to reply to questions
 
For your question about the Drutchlin code being .net or not I think you'll have some huge problems understanding it! :D

Anyways... It's C# code! And place it where you usually put your Subs and Functions... It isn't Design Generated code...
 
That's right it is C#.. you should be able to use most of the functionality if you convert it into VB ...I have almost finished a class...all you'll have to do is reference it create an instance and call a method..I will try to have it up in a couple of days
 
I haven't ever started learning C# yet, but I know there are aa few convertors around, anyone recomend one??

Thanks for all the help, it will help me and many others.

Dale
 
There's really some convertors but if you know vb you know C#...
It's just a matter of learning few declaration diferences... a good thing about learning that diferences is that you'll learn another language...

Second thing is that the convertors aren't fail prof...
 
Hey Drutchlin, Was just wondering if you had an opportunity to post the class for resizing a windows form based on screen resolution. In your earlier posts you had mentioned that you were working on a class and would post it in couple of days. Instead of reinventing the wheel if you have one already finished was thinking of using it. Thanks a lot!!
 
Back
Top