-
December 19th, 2009, 09:18 PM
#1
How to make a Resolution Independent Application?
The development machine's and client machine's screen resolution are very likely to vary. I want a code that will RE-SIZE and RE-POSITION each and every control so that no matter what the development machine's resolution is, the application will look exactly the same on the client machine irrespective of its screen resolution.
These two links are as close as I get to the answer, but they are not very clear.
(1) http://vbcity.com/forums/p/68832/276540.aspx#276540
(2) http://vbcity.com/forums/p/88845/371135.aspx#371135
I would be grateful if someone would clarify this for me.
P.S. I don't know if its okay to post links of a different forum... Im a noob so please overlook it as it wont happen again if its forbidden.
-
December 20th, 2009, 04:15 PM
#2
Re: How to make a Resolution Independent Application?
If you DOCK controls, then they resize with the form
-
December 20th, 2009, 09:57 PM
#3
Re: How to make a Resolution Independent Application?
Docking, doesn't work exactly for many forms. It's somewhat useful though.
The TableLayoutPanel control, and FlowLayoutPanel control, can help resize the controls automatically when the form is resized, but they've had a few nasty bugs in VB2005, so back up all your work first. I don't think they fixed the bugs.
The second link looked pretty good, for resizing controls too.
I usually loop through the controls like this, and customize the size and position to avoid the LayoutPanel bugs.
To resize the main form, there are a few ways to do it.
Here is a simple threaded way.
Code:
Private kRes As New Threading.Thread(AddressOf KeepResolution)
Dim perX, perY As Double, prvheight, prvWidth As Int32
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
kRes.Start()
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
kRes.Abort()
End Sub
Private Sub KeepResolution()
On Error Resume Next
Dim desktopSize As Size = Windows.Forms.SystemInformation.PrimaryMonitorSize
prvheight = desktopSize.Height
prvWidth = desktopSize.Width
perX = Me.Width / prvWidth
perY = Me.Height / prvheight
Do
desktopSize = Windows.Forms.SystemInformation.PrimaryMonitorSize
If desktopSize.Height <> prvheight OrElse desktopSize.Width <> prvWidth Then
prvheight = desktopSize.Height
prvWidth = desktopSize.Width
Me.Width = CInt(prvWidth * perX)
Me.Height = CInt(prvheight * perY)
End If
Threading.Thread.Sleep(1000)
Loop
End Sub
-
October 12th, 2011, 11:59 AM
#4
Re: How to make a Resolution Independent Application?
Getting a resolution independent code bothered me for a long time. I purchased two different commercial products, but they did not work with Windows 7.
I finally figured it out for myself and thought that I would post it here for others. This is for Windows Forms.
If you are Maximizing your screen try this then set your form’s WindowState to Minimized in design. The reason is that when the controls are resized, the screen will ‘flicker’ if it is visible. I tried setting the forms .visible = false, but this didn’t work in Windows 7 when I tried to make it visible again (froze up – not sure why).
If you are not maximizing, then there is two lines of code at the end here that will adjust for this – it is marked
Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call SetResolution()
End sub
Private Sub form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
‘ set the windowstate to Maximized
Me.WindowState = FormWindowState.Maximized
‘if you are not maximizing your screen and are using ‘normal’ for you ‘windowstate, you can 'ignor this section
End sub
Private Sub SetResolution()
‘ set resolution sub checks all the controls on the screen. Containers (tabcontrol, panel, ‘groupbox, tablelayoutpanel) do not resize on general control search for the form – so ‘they have to be done separate by name
Dim perX, perY As Double, prvheight, prvWidth As Int32
Dim shoAdd As Short
Dim desktopSize As Size = Windows.Forms.SystemInformation.PrimaryMonitorSize
prvheight = desktopSize.Height
prvWidth = desktopSize.Width
‘ in Windows 7, in the display section of the control panel, a user can ‘be set to see their screen larger – the settings are 100%, 125%, and ‘150%. In my programs preferences, I allow my software user to select ‘if they are using the 125% or the 150% settings. I set the global ‘p_shoWhatSize (short) varible to 1 for 125% and 2 for 150% screen. ‘This section ajusts for this
If p_shoWhatSize = 1 Then
prvheight = prvheight * 0.8
prvWidth = prvWidth * 0.8
End If
If p_shoWhatSize = 2 Then
prvheight = prvheight * 0.6666
prvWidth = prvWidth * 0.6666
End If
‘ the development resolution for my project is 1024 x 768 – change this ‘to your development resolution
' get new 'ratio' for screen
perX = prvWidth / 1024
perY = prvheight / 768
‘ listboxes don’t resize vertically correctly for all resolutions due ‘to the font size. shoAdd is used to ‘tweek’ the size of the list ‘boxes to help adjust for this – requires some testing on your screens ‘in different resolutions. I have some set at 10 and some as high as ‘14.
If prvheight > 768 Then shoAdd = Int((prvheight - 768) / 12)
Dim shoFont As Short
' if res is 1024 x 768 then perX and PerY will equal 1
If perX <> 1 Or perY <> 1 Then
For Each ctl As Control In Me.Controls
‘ if you change the fonts of panels or groupbox containers, it messes
‘ with the controls in those containers. Therefore, I skip the font
‘ resize for these
If UCase(ctl.GetType.ToString) <> "SYSTEM.WINDOWS.FORMS.PANEL" _
And UCase(ctl.GetType.ToString) <> "SYSTEM.WINDOWS.FORMS.GROUPBOX" Then
shoFont = ctl.Font.Size * perY
ctl.Font = New Font(ctl.Font.FontFamily, shoFont, ctl.Font.Style)
End If
'get new location
ctl.Location = New Point(ctl.Location.X * perX, ctl.Location.Y * perY)
If UCase(ctl.GetType.ToString) = "SYSTEM.WINDOWS.FORMS.LISTBOX" Then
ctl.Height = ctl.Size.Height * perY + shoAdd
ctl.Width = ctl.Size.Width * perX
Else
' get new height & width
ctl.Height = ctl.Size.Height * perY
ctl.Width = ctl.Size.Width * perX
End If
Application.DoEvents()
Next ctl
‘ do tabcontrols separate by name – a separate for/next loop per ‘control
For Each tp As TabPage In Me.tabcontrol1.TabPages
For Each ctl As Control In tp.Controls
If UCase(ctl.GetType.ToString) <> "SYSTEM.WINDOWS.FORMS.PANEL" _
And UCase(ctl.GetType.ToString) <> "SYSTEM.WINDOWS.FORMS.GROUPBOX" Then
shoFont = ctl.Font.Size * perY
ctl.Font = New Font(ctl.Font.FontFamily, shoFont, ctl.Font.Style)
End If
'get new location
ctl.Location = New Point(ctl.Location.X * perX, ctl.Location.Y * perY)
If UCase(ctl.GetType.ToString) = "SYSTEM.WINDOWS.FORMS.LISTBOX" Then
ctl.Height = ctl.Size.Height * perY + shoAdd
ctl.Width = ctl.Size.Width * perX
Else
' get new height & width
ctl.Height = ctl.Size.Height * perY
ctl.Width = ctl.Size.Width * perX
End If
Application.DoEvents()
Next
Next
‘ do groupboxs separate also – separate for/next for each control by ‘name
For Each ctl As Control In GroupBox1.Controls
If UCase(ctl.GetType.ToString) <> "SYSTEM.WINDOWS.FORMS.PANEL" _
And UCase(ctl.GetType.ToString) <> "SYSTEM.WINDOWS.FORMS.GROUPBOX" Then
shoFont = ctl.Font.Size * perY
ctl.Font = New Font(ctl.Font.FontFamily, shoFont, ctl.Font.Style)
End If
'get new location
ctl.Location = New Point(ctl.Location.X * perX, ctl.Location.Y * perY)
If UCase(ctl.GetType.ToString) = "SYSTEM.WINDOWS.FORMS.LISTBOX" Then
ctl.Height = ctl.Size.Height * perY + shoAdd
ctl.Width = ctl.Size.Width * perX
Else
' get new height & width
ctl.Height = ctl.Size.Height * perY
ctl.Width = ctl.Size.Width * perX
End If
Application.DoEvents()
Next
‘ do panels separate also – separate for/next for each ‘panel by name
For Each ctl As Control In Panel1.Controls
If UCase(ctl.GetType.ToString) <> "SYSTEM.WINDOWS.FORMS.PANEL" _
And UCase(ctl.GetType.ToString) <> "SYSTEM.WINDOWS.FORMS.GROUPBOX" Then
shoFont = ctl.Font.Size * perY
ctl.Font = New Font(ctl.Font.FontFamily, shoFont, ctl.Font.Style)
End If
'get new location
ctl.Location = New Point(ctl.Location.X * perX, ctl.Location.Y * perY)
If UCase(ctl.GetType.ToString) = "SYSTEM.WINDOWS.FORMS.LISTBOX" Then
ctl.Height = ctl.Size.Height * perY + shoAdd
ctl.Width = ctl.Size.Width * perX
Else
' get new height & width
ctl.Height = ctl.Size.Height * perY
ctl.Width = ctl.Size.Width * perX
End If
Application.DoEvents()
Next
‘ if you are not maximizing your screen afterwards, then include this code
Me.Top = (prvheight / 2) - (Me.Height / 2)
Me.Left = (prvWidth / 2) - (Me.Width / 2)
End sub
-
October 12th, 2011, 08:22 PM
#5
Re: How to make a Resolution Independent Application?
Please read the rules.
Don't resurrect 2 year old posts that have been answered.
Use CODE TAGS when posting.
and, welcome to the forums!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|