Condividi tramite


Visual Basic Concepts

Designing for Different Display Types

Microsoft Windows is device-independent — a windows-based application can be run on many different computers with different display resolutions and color depths. The applications that you write in Visual Basic are likely to be run on different display types as well; you need to be aware of this when designing an application.

Designing Resolution-independent Forms

By default, Microsoft Visual Basic doesn't change your form and control sizes as you change screen resolutions. What this means is that a form that you design at 1024 by 768 resolution may extend past the edges of the screen when run at 640 by 480 resolution. If you want to create forms and controls that have the same proportions no matter what screen resolution you use, you must either design your forms at the lowest resolution, or add code to your program that changes the forms.

The easiest way to avoid sizing problems is to design your forms at 640 by 480 resolution. If you prefer to work at a higher resolution, you still need to be aware of how your form will appear at a lower resolution. One way to do this is to use the Form Layout window to preview the size and location of your form. You can use Resolution Guides to see what portion of the screen will be visible at a lower resolution. To toggle the Resolution Guides, right click in the Form Layout window and select the Resolution Guides menu item from the popup menu.

Visual Basic also places your form at run time based on its location at design time. If you are running at 1024 by 768 resolution at design time and you place a form in the lower right-hand corner of the screen, it may not be visible when run at a lower resolution. To avoid this, set the startup position of your form at design time by selecting the Startup Position menu item from the Form Layout window popup menu.

Alternatively, you can set a form’s position at runtime with code in the Form Load event:

Private Sub Form_Load()
   Me.Move 0, 0
End Sub

This has the same effect as setting both the Left and Top properties of the form to 0, but the Move method accomplishes it in a single step.

Visual Basic uses a device-independent unit of measurement, a twip, for calculating size and position. Two properties of the Screen object, TwipsPerPixelX and TwipsPerPixelY, can be used to determine the size of the display at run time. Using these properties, you can write code to adjust the size and position of your forms and controls:

Private Sub SetControls()
   Dim X As Integer
   Dim Y As Integer

   X = Screen.TwipsPerPixelX
   Y = Screen.TwipsPerPixelY
   Select Case X, Y
      Case 15, 15
         ' Resize and move controls.
         txtName.Height = 200
         txtName.Width = 500
         txtName.Move 200, 200
      ' Add code for other resolutions.
      …
End Sub

You also need to be aware of the position of Visual Basic's own windows at design time. If you position the Project window to the right side of the screen at high resolution, you may find that it is no longer accessible when you open your project at a lower resolution.

Designing for Different Color Depths

In designing an application, you also need to consider the color display capabilities of the computers that may be running your application. Some computers can display 256 or more colors, others are limited to 16. If you design a form using a 256-color palette, dithering (a process used to simulate colors that are not available) may cause some of the elements on the form to disappear when displayed at 16 colors.

To avoid this situation, it's best to limit the colors used in your application to the 16 standard Windows colors. These are represented by the Visual Basic color constants (vbBlack, vbBlue, vbCyan, and so on). If it's necessary to use more than 16 colors in your application, you should still stick with the standard colors for text, buttons, and other interface elements.