Many of the controls in Visual Basic have properties that determine the colors used to display the control. Keep in mind that some of these properties also apply to controls that aren't graphical. The following table describes the color properties.
Property | Description |
BackColor | Sets the background color of the form or control used for drawing. If you change the BackColor property after using graphics methods to draw, the graphics are erased by the new background color. |
ForeColor | Sets the color used by graphics methods to create text or graphics in a form or control. Changing ForeColor does not affect text or graphics already created. |
BorderColor | Sets the color of the border of a shape control. |
FillColor | Sets the color that fills circles created with the Circle method and boxes created with the Line method. |
For More Information For detailed descriptions of these color properties, see "BackColor Property," "ForeColor Property," "BorderColor Property," and "FillColor Property" in the Language Reference.
The color properties can use any of several methods to define the color value. The RGB function described in "Working with Color" is one way to define colors. This section discusses two more ways to define colors:
You don’t need to understand how color values are generated if you use the intrinsic constants listed in the Object Browser. In addition, intrinsic constants do not need to be declared. For example, you can use the constant vbRed whenever you want to specify red as a color argument or color property setting:
BackColor = vbRed
Using the RGB function or the intrinsic constants to define color are indirect methods. They are indirect because Visual Basic interprets them into the single approach it uses to represent color. If you understand how colors are represented in Visual Basic, you can assign numbers to color properties and arguments that specify color directly. In most cases, it’s much easier to enter these numbers in hexadecimal.
The valid range for a normal RGB color is 0 to 16,777,215 (&HFFFFFF&). Each color setting (property or argument) is a 4-byte integer. The high byte of a number in this range equals 0. The lower 3 bytes, from least to most significant byte, determine the amount of red, green, and blue, respectively. The red, green, and blue components are each represented by a number between 0 and 255 (&HFF).
Consequently, you can specify a color as a hexadecimal number using this syntax:
&HBBGGRR&
The BB specifies the amount of blue, GG the amount of green, and RR the amount of red. Each of these fragments is a two-digit hexadecimal number from 00 to FF. The median value is 80. Thus, the following number specifies gray, which has the median amount of all three colors:
&H808080&
Setting the most significant bit to 1 changes the meaning of the color value: It no longer represents an RGB color, but an environment-wide color specified through the Windows Control Panel. The values that correspond to these system-wide colors range from &H80000000 to &H80000015.
Note Although you can specify over 16 million different colors, not all systems are capable of displaying them accurately. For more information on how Windows represents colors, see "Working with 256 Colors" later in this chapter.
When setting the colors of controls or forms in your application, you can use colors specified by the operating system instead of specific color values. If you specify system colors, when users of your application change the values of system colors on their computers, your application automatically reflects the user-specified color values.
Each system color has both a defined constant and a direct color setting. The high byte of direct color settings for system colors differs from those of normal RGB colors. For RGB colors, the high byte equals 0 whereas for system colors the high byte equals 8. The rest of the number refers to a particular system color. For example, the hexadecimal number used to represent the color of an active window caption is &H80000002&.
When you select color properties at design time with the Properties window, selecting the System tab lets you choose system settings, which are automatically converted into the hexadecimal value. You can also find the defined constants for system colors in the Object Browser.