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