Visual Basic Concepts

Managing Multiple Color Palettes

See Also

When you work with color palettes, keep in mind that many displays can display only 256 colors simultaneously on the screen.

This limitation becomes important when you use more than one color palette in your application. For example, on a single form, you might display a 256-color bitmap in an image control while displaying a second image in a picture box. If the logical palettes of these two images don’t contain exactly the same 256 colors, Windows must decide which logical palette places its colors in the hardware palette first. Remember: The hardware palette determines what actually appears on the screen.

A similar situation occurs when your Visual Basic application has two or more forms with differing logical palettes. As each form receives focus, its logical palette controls the hardware palette. This can often result in a less than optimal display on 256-color systems. As a Visual Basic programmer, you can control the hardware palette by using the PaletteMode property.

The PaletteMode Property

When designing applications that may run on 256-color systems, you can control the way that Windows chooses the display colors by setting the PaletteMode property of a form, user control, or user document. (User controls and user documents are only available in the Professional and Enterprise editions.) All controls contained on the form, user control, or user document will be displayed based on the PaletteMode. The following table shows the available PaletteMode settings:

Mode Constant Applies to
Halftone vbPaletteModeHalftone Forms, User Controls, User Documents
UseZOrder vbPaletteModeUseZOrder Forms, User Controls, User Documents
Custom vbPaletteModeCustom Forms, User Controls, User Documents
Container vbPaletteModeContainer User Controls
None vbPaletteModeNone User Controls
Object vbPaletteModeObject ActiveX designers that contain a palette

The PaletteMode property only applies to 256-color displays. On high-color or true-color displays, color selection is handled by the video driver using a palette of 32,000 or 16 million colors respectively. Even if you’re programming on a system with a high-color or true-color display, you still may want to set the PaletteMode, because many of your users may be using 256-color displays.

The PaletteMode property can be set at design time through the Properties window, or changed at run time via code. The Palettes sample application demonstrates the effects of displaying images with different palettes using several different PaletteMode settings.

Note   For previous versions of Visual Basic, PaletteMode corresponded to UseZOrder.

Halftone PaletteMode

The default mode for forms and user documents is Halftone. In this mode, any controls, images contained on the form, or graphics methods draw using the system halftone palette.

Halftone mode is a good choice in most cases because it provides a compromise between the images in your form, and colors used in other forms or images. It may, however, result in a degradation of quality for some images. For example, an image with a palette containing 256 shades of gray may lose detail or display unexpected traces of other colors.

UseZOrder PaletteMode

Z-order is a relative ordering that determines how controls overlap each other on a form. When the PaletteMode of the form with the focus is set to UseZOrder, the palette of the topmost control always has precedence. This means that each time a new control becomes topmost (for instance, when you load a new image into a picture box), the hardware palette will be remapped. This will often cause a side effect known as palette flash: The display appears to flash as the new colors are displayed, both in the current form and in any other visible forms or applications.

Although the UseZOrder setting provides the most accurate color rendition, it comes at the expense of speed. Additionally, this method can cause the background color of the form or of controls that have no image to appear dithered. Setting the PaletteMode to UseZOrder is the best choice when accurate display of the topmost image outweighs the annoyance of palette flash, or when you need to maintain backward compatibility with earlier versions of Visual Basic.

Custom PaletteMode

If you need more precise control over the actual display of colors, you can use a 256-color image to define a custom palette. To do this, assign a 256-color image (.bmp, .cur, .ico, .dib, or .gif) to the Palette property of the form and set the PaletteMode property to Custom. The bitmap doesn’t have to be very large; even a single pixel can define up to 256 colors for the form or picture box. This is because the logical palette of a bitmap can list up to 256 colors, regardless of whether all those colors appear in the bitmap.

As with the default method, colors that you define using the RGB function must also exist in the bitmap. If the color doesn’t match, it will be mapped to the closest match in the logical palette of the bitmap assigned to the Palette property.

To set the Custom PaletteMode at run time, add the following code to the Form_Load event (assuming that the image containing your chosen palette has been assigned to a Image control named Image1):

' Assign the palette from Image1 to the form.
Form1.Palette = Image1.Picture
' Use the Custom mode.
Form1.PaletteMode = vbPaletteModeCustom

Alternatively, you can use the Picture object to achieve the same effect without the extra Image control:

Dim objPic As Picture 
Set objPic = LoadPicture(App.Path & "\Pastel.bmp")
' Assign picture object's palette to the form.
Form1.Palette = objPic
' Use the Custom mode.
Form1.PaletteMode = vbPaletteModeCustom

The Custom PaletteMode is your best choice when you want to maintain a uniform palette throughout your application.

Note   Using the Custom PaletteMode can also improve the performance of your application in cases where you aren’t using any 256-color graphics. If you set the PaletteMode of a form to Custom and leave the Palette property blank, your form will load faster because no palette matching will occur.

For More Information   To learn more about the Picture object, see "Using the Picture Object" later in this chapter.

Other Palette Modes

Two additional PaletteMode settings are available when creating user controls: Container and None. The Container mode maps the palette of the user control and any contained controls to the ambient palette of the container (form or user document) at run time. If the container doesn’t supply an ambient palette, the Halftone mode will be invoked. Because you may not know in advance where your user control may be deployed, this mode can prevent your control from conflicting with other palette handling methods.

The None mode does just what you might expect: It eliminates palette handling altogether. When creating a user control that doesn’t display images or graphics, setting PaletteMode to None improves performance by eliminating the added overhead of handling palette messages.

Using Relative Palette Colors

When designing for 256 color displays, some colors may appear dithered. This can make text and other graphic elements difficult to read. By specifying a relative palette color, Visual Basic will display the closest undithered approximation of a specified color on 256 color displays while still displaying the exact color at higher color depths.

To force Visual Basic to use the closest solid, rather than dithered, color for a given property, put a 2 in the high order byte of the color property. For example, to force a form's background to be a solid light orange you could use the following code:

Private Function PaletteRGB(RGB As Long) As Long
 PaletteRGB = &H02000000 Or RGB
End Function

If you set this at design time:

Form1.BackColor = &H00C0E0FF&    'dithered light orange

And add the following to the Form_Click event:

Private Sub Form_Click()
   Form1.BackColor = PaletteRGB(Form1.BackColor)
End Sub

At run-time when the form is clicked the backcolor will change to a solid, rather than dithered, shade. It is now using the closest color out of the halftone palette. This effect may not be visible on systems running at a color depth greater than 256 colors.