Suppose you carefully craft a form sized to fit your screen. You position every control exactly to achieve the perfect visual effect. Then you distribute the program and your customers run it on a screen with 640 x 480 pixels. You designed it with 800 x 600 pixels so the form doesn't fit.
Modern computers can work at many different resolutions depending on how many colors they display. My computer can run at these resolutions:
640 x 480 |
720 x 480 |
720 x 576 |
800 x 600 |
1024 x 768 |
1152 x 854 |
1280 x 1024 |
1600 x 1200 |
So what can you do to ensure that a form will look good at all resolutions? Here are some approaches you can take:
Design your form for the smallest screen the users will have, probably 640 x 480 pixels. When the screen is larger, you can center it taking the task bar into account like this:
Option Explicit
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function SystemParametersInfo Lib "user32" _
Alias "SystemParametersInfoA" (ByVal uAction As Long, _
ByVal uParam As Long, ByRef lpvParam As RECT, _
ByVal fuWinIni As Long) As Long
Private Const SPI_GETWORKAREA = 48
' Center the form taking the task bar
' into account.
Private Sub CenterForm(ByVal frm As Form)
Dim wa_info As RECT
Dim wa_wid As Single
Dim wa_hgt As Single
If SystemParametersInfo(SPI_GETWORKAREA, _
0, wa_info, 0) <> 0 _
Then
' We got the work area bounds.
' Center the form in the work area.
wa_wid = ScaleX(wa_info.Right, vbPixels, vbTwips)
wa_hgt = ScaleY(wa_info.Bottom, vbPixels, vbTwips)
Else
' We did not get the work area bounds.
' Center the form on the whole screen.
wa_wid = Screen.Width
wa_hgt = Screen.Height
End If
' Center the form.
frm.Move (wa_wid - Width) / 2, _
(wa_hgt - Height) / 2
End Sub
Private Sub Form_Load()
CenterForm Me
End Sub
|
Download an example
This technique is simple. You only need to design the form once. It requires no special changes or rearrangement of controls at run time.
The disadvantage of this technique is that it does not take full advantage of the screen if it has a greater resolution than 640 x 480. If the screen is 800 x 600, you could display a lot more data.
This method also leaves some of the screen uncovered. That generally does not change the program's usability, but it can be distracting. Fortunately it's easy to cover the entire screen. Simply maximize the form by setting the WindowState property to vbMaximized. Then center the form's controls in the form. This is easiest if you place the controls inside a PictureBox. Then you only need to center the PictureBox.
Option Explicit
' At design time we set:
' ControlBox = False
' Caption = ""
' WindowState = Maximized
' BorderStyle = None
Private Sub Form_Resize()
picCenter.BorderStyle = vbBSNone
picCenter.Move _
(ScaleWidth - picCenter.Width) / 2, _
(ScaleHeight - picCenter.Height) / 2
End Sub
|
Download an example
If you like, you can add a picture to the form so the background is not completely blank.
Back to top
When you design for different resolutions, remember that color resolution and pixel resolution are related. On my computer, I can display 24-bit color at up to 1280 x 1024 pixels. If I go to 1660 x 1200 pixels, however, the system only has enough graphic memory to display 8-bit color (256 colors).
If you expect users to set their screens to use very high spatial resolution, you should also expect them to be using low color resolution. If your application displays photographic images or uses lots of colors, some of the colors may be lost. Your program may become unusable at these higher spatial resolutions. In that case, you may want to simply tell the user not to use higher resolutions and you won't have to worry about them.
|