Scaling for different resolutions.
Scaling for different resolutions.
Scaling for different resolutions.
Scaling for different resolutions.
Scaling for different resolutions.
Scaling for different resolutions. Scaling for different resolutions. Scaling for different resolutions. Scaling for different resolutions. Scaling for different resolutions. Scaling for different resolutions. Scaling for different resolutions. Scaling for different resolutions.
Scaling for different resolutions. Scaling for different resolutions.
Scaling for different resolutions.
Go Back  Xtreme Visual Basic Talk > > > Scaling for different resolutions.


Reply
 
Thread Tools Display Modes
  #1  
Old 09-26-2010, 11:54 AM
artax artax is offline
Newcomer
 
Join Date: Sep 2010
Location: Middle East
Posts: 15
Default Scaling for different resolutions.


Please forgive this newbie if this question has already been addressed; I have looked and not found an answer on the forum. I was developing commercially in Visual Basic up to about 10 years ago before a change of career, and now I'm back and seeing how much has changed!

I am in the early stages of developing a forms-based application to display a series of fairly complex interactive technical diagrams. The diagrams include vector graphics, .jpg images, text boxes, buttons and a few other controls. Some parts of the image will be zoomable to show more detail to the user.

I would very much like to make the application scale itself automatically to work full-screen on any PC regardless of native resolution. This will require the containing form to scale itself (easy enough) but also all the components and controls and fonts within to scale and position themselves correctly.

Is this possible without fearsomely complex and detailed coding?

Hope you can help......and happy to be here!
Reply With Quote
  #2  
Old 09-27-2010, 03:17 AM
Flyguy's Avatar
FlyguyScaling for different resolutions. Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 19,165
Default

When using VB6 there is no other solution then coding all resizing handling yourself...
Reply With Quote
  #3  
Old 09-30-2010, 01:09 AM
artax artax is offline
Newcomer
 
Join Date: Sep 2010
Location: Middle East
Posts: 15
Default

Thanks for that. Sorry, I missed a vital piece of information in my question; I'm using Visual Basic in Visual Studio 2010. I see that there are some auto-scaling functions included, but I've not yet managed to get it working to solve this problem.
Reply With Quote
  #4  
Old 09-30-2010, 11:15 AM
AtmaWeapon's Avatar
AtmaWeaponScaling for different resolutions. AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I don't have a lot of experience with designing auto-scaling forms, but I have a good deal of knowledge, some anecdotes from friends, and some advice.

For the most part, the auto-scaling in WinForms is automatic. Your form has an AutoScaleMode property that dictates how it will scale itself. Understanding how it works is kind of difficult, but here's a shot.

When the mode is None, no scaling takes place (this is pretty obvious). A notable exception: there is an AutoScale property that is a remnant of .NET 1.x and if it is set to True you will get behavior similar to if AutoScaleMode is Font. If that property is False, you'll get what you expect.

What happens here if the user's resolution increases? Fonts will get bigger but controls won't, so odds are the text won't fit properly in controls.

When the mode is Font, the base unit of scale is the font of the controls. This was the technique used in .NET 1.1 and is still the default for compatibility, but isn't always right. When this mode is in play, some information about the pixel size of font on your display is saved in your code. When the application runs on a machine with a different resolution (or even if the project is opened in VS on a machine with a different resolution!), then .NET compares the current size of the font to the original size and scales all controls using this information. For buttons and other text-oriented controls, this is fine. I've heard that occasionally this can result in controls overlapping each other, but I haven't done much experimentation to determine if this is true. The documentation points out this mode is not as useful for graphics-based applications. What it means is font sizes can vary in a non-deterministic way as the resolution changes, so scaled images may not be a size that makes sense.

What happens in this mode if the user's resolution increases? If the original font was 25 pixels tall and now the font is 40 pixels tall, that's a 60% increase. A button that used to be 42 pixels tall will likely now be 68 pixels tall. Your form will grow to accomodate the new size of controls.

When the mode is Dpi, only the change in the system's DPI is considered when scaling the form. For text, this can be inappropriate. A font might increase its size by 25% in response to a 20% DPI change due to readability concerns. For images, this is very appropriate since the image will be scaled exactly as much as the resolution has changed.

What happens in this mode if the resolution increases? All fonts will change their size by some amount dictated by the font designer. All controls will change their size by an amount relative to the change in DPI. This means it's still possible for fonts to end up too big. However, images will scale appropriately.

You can mix and match these modes; every container control has its own setting. If a control's AutoScaleMode is set to Inherit, it will use whatever its parent specifies; there's no telling what a Form does if you set it to Inherit.

There's some knowledge crucial to using these scaling modes and keeping your form layout somewhat consistent. You need to know how to use the Anchor and Dock properties to indicate how each control should move when the form is resized. I find it a little easier to use a TableLayoutPanel; it interacts with these properties but also feels like I get more control. If you don't set these properties properly, your controls won't fill the extra space when the form is resized, and the odds that your controls will overlap increases.

When drawing vector graphics, you can take DPI into account by checking the DpiX and DpiY properties of the Graphics object you are using to draw. If you are interested in making elements match some physical size onscreen this can be invaluable. Here's an example of a form that displays its physical dimensions (in inches) as a caption *and* draws a 2-inch line centered in its client area no matter what display resolution is in use:
Code:
Public Class Form1

    Protected Overrides Sub OnSizeChanged(ByVal e As System.EventArgs)
        MyBase.OnSizeChanged(e)

        Dim dpiX As Single = 0
        Dim dpiY As Single = 0
        ' We don't have a convenient graphics context, so create one and dispose of it when we're done
        Using gfx As Graphics = Me.CreateGraphics()
            dpiX = gfx.DpiX
            dpiY = gfx.DpiY
        End Using

        Dim sizePixels As Size = Me.ClientSize
        ' pixels / (pixels/inch) = (pixels * inch) / (pixels) = inch
        Dim sizeInches As New SizeF(sizePixels.Width / dpiX, sizePixels.Height / dpiY)

        Me.Text = String.Format("{0} x {1} inches.", sizeInches.Width, sizeInches.Height)
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        ' Horizontal line will manually calculate pixel width for 2 inches
        ' Get the pixel length of a 2-inch line
        ' inch * (pixel / inch) = pixel
        Dim lengthInPixels As Double = 2 * e.Graphics.DpiX
        e.Graphics.DrawLine(Pens.Green, 0, 25, CInt(lengthInPixels), 25)

        ' Setting the PageUnit property means everything will be in terms of a different unit. Note this
        ' affect *every* number used by the graphics methods, so if you want something in terms of pixels
        ' you still have to calculate.
        '
        ' For example, let's draw a line 3 pixels thick that starts at the end of the previous line.
        ' pixel / (pixel / inch) = inch
        Dim lineWidth As Single = 3 / e.Graphics.DpiY
        ' Line ends at (25 pixels, 2 inches)
        Dim x1 As Single = 2
        Dim y1 As Single = 25 / e.Graphics.DpiY
        Dim x2 As Single = 2
        Dim y2 As Single = y1 + 2
        e.Graphics.PageUnit = GraphicsUnit.Inch
        Using p As New Pen(Color.Red, lineWidth)
            e.Graphics.DrawLine(p, x1, y1, x2, y2)
        End Using

        ' Obviously, this makes working with pen thicknesses a little finicky, but it makes your coordinate
        ' system nicer.
    End Sub

End Class
The comments are informative, pay attention to them.

There's another possibility. WPF is a different graphical framework. In general, it behaves more like a vector-based graphics system. There's one interesting implication of choosing WPF: it uses "WPF units" instead of pixels for its page unit. A WPF unit is resolution-dependent so that 96 units always equal 1 inch. If you make a button that is 48 x 96 on a machine with a 96 DPI resolution, it will display as 1/2" x 1". If you move that program to a machine with 120 DPI resolution, it will display as 1/2" x 1". Obviously, this can be desirable.

In the end, I'm not sure there's a generally good way to design a resolution-independent application. These are some things to keep in mind; it's up to you to test your applications on many resolutions.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 09-30-2010, 09:21 PM
artax artax is offline
Newcomer
 
Join Date: Sep 2010
Location: Middle East
Posts: 15
Default

Thank you so much for your thorough answer.

I was hoping you'd say something like "Just set Form1.AutoScaleEverything to True" .... but maybe that's for VB 2011....!

I will work through this slowly using your notes as a starting point.

Thanks again!
Reply With Quote
Reply

Tags
resolution, scaling, screen size


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Scaling for different resolutions.
Scaling for different resolutions.
Scaling for different resolutions. Scaling for different resolutions.
Scaling for different resolutions.
Scaling for different resolutions.
Scaling for different resolutions. Scaling for different resolutions. Scaling for different resolutions. Scaling for different resolutions. Scaling for different resolutions. Scaling for different resolutions. Scaling for different resolutions.
Scaling for different resolutions.
Scaling for different resolutions.
 
Scaling for different resolutions.
Scaling for different resolutions.
 
-->