CONTROL SET COLOR statement

Purpose | Set the color of a control to a specific RGB foreground and background color.
Syntax | CONTROL SET COLOR hDlg, id&, foreclr&, backclr&
Remarks | hDlg identifies the controls parent dialog, and id& is the unique control identifier as assigned to the control with a CONTROL ADD statement. Color values of foreclr& and backclr& must be in the range of &H0 to &H00FFFFFF. RGB can be a useful function to derive a 32-bit color value from discrete Red, Green and Blue values:
foreclr& | The foreground color parameter foreclr& is used to set the color of the text displayed in the control. If foreclr& = -1&, the default foreground text color is used.

backclr&

The backclr& parameter specifies the color of the background behind the text in the control. If backclr& = -1&, the default background text color is used. If backclr& = -2&, the text background is not painted, allowing the background to show "through" the text. The non-visible background style may produce undesirable side effects with some controls. For example, on a FRAME control, the caption text will appear superimposed over an unbroken frame.

In 16-bit or greater color-depth mode, the specified RGB color is used when the background of the control is drawn. However, in 8-bit (256-color) mode, the color system works quite differently. Behind the scenes in Windows, the base system palette usually contains 20 solid colors that are not dithered when drawn on the controls background. These solid-colors are ideal for control background colors with DDT dialogs in 256-color mode.

Conversely, when using a non-solid RGB color value, Windows will dither (approximate) the color to draw the control, using combinations of two or more colors. This usually produces an undesirable pattern effect.

To avoid these problems when in 256-color mode, controls should be colored with one of the 20 standard (solid) system colors, or the default color should be used instead. PowerBASIC includes the following 10 built-in equates for help with the selection of a standard solid color:

%RGB_BLACK %RGB_BLUE %RGB_GREEN %RGB_CYAN %RGB_RED

%RGB_MAGENTA %RGB_YELLOW %RGB_WHITE %RGB_GRAY %RGB_LIGHTGRAY

Many non standard colors are also built into the compiler, see the Built In RGB Color Equates topic for a complete list.

If you prefer to disable color in 256-color mode, the number of colors can be easily tested with the following code:

' Determine number of colors

LOCAL hDC AS DWORD, iColors AS LONG

hDC = GetWindowDC(GetDeskTopWindow())

iColors = 2& ^ (GetDeviceCaps(hDC, %BITSPIXEL) * GetDeviceCaps(hDC, %PLANES))

ReleaseDC GetDeskTopWindow(), hDC

IF iColors > 256 THEN _

CONTROL SET COLOR hDlg, idctl&, -1, RGB(0,255,100)

In 256-color mode on most computers, the values of the standard 20 system colors can be found by requesting the first and last 10 (0 to 9, and 246 to 255 inclusive) entries from the GetSystemPaletteEntries API function, as follows:

' Fill array with solid colors

DIM hDC AS DWORD, Cols AS LONG, x AS LONG

hDC = GetWindowDC(GetDesktopWindow)

Cols = GetDeviceCaps(hDC, %NUMRESERVED)

REDIM lp(1 TO Cols) AS LONG

x& = GetSystemPaletteEntries(hDC, 0, Cols \ 2, BYVAL VARPTR(lp(1)))

x& = GetSystemPaletteEntries(hDC, 256 - x&, Cols - x&, BYVAL VARPTR(lp(x& + 1)))

ReleaseDC GetDesktopWindow, hDC

' Array lp() now contains the solid color table

For more information on working with palettes in 256-color mode, please consult WIN32.HLP or visit http://msdn.microsoft.com.

Restrictions

Windows does not permit the color of standard push button controls, line controls, image controls, image buttons, and most common controls to be altered by the standard CONTROL SET COLOR techniques.

To create a colored push button or colored region on a dialog, the preferred solution is to use an IMGBUTTON/ IMAGEBUTTONX or IMAGE/ IMAGEX control, with a suitably colored bitmap. Some common controls offer specific ways to set their color. For example, the background color of a List View control can be set with the %LVM_SETBKCOLOR message.

When dynamically changing colors of a control from within a callback (i.e., after the DIALOG SHOW statement), a CONTROL SET COLOR statement should be immediately followed by an explicit CONTROL REDRAW statement.

Without this forced control redraw, the control background color change may not become evident to the user until the control is eventually repainted in the normal course of user interaction. For example, a normal repaint may only occur if the control becomes obscured and then uncovered by another window. Ensuring a timely repaint of the control will guarantee the control maintains an up-to-date appearance at all times.

When updating the colors of multiple controls at the same time, a DIALOG REDRAW is usually more efficient than multiple CONTROL REDRAW statements.

See also | Built In RGB Color Equates, Dynamic Dialog Tools, CONTROL GET CLIENT, CONTROL GET SIZE, CONTROL REDRAW, CONTROL SET FONT, DIALOG REDRAW, DIALOG SET COLOR
Example | ' Set the color with discrete RGB values CONTROL SET COLOR hDlg, idBtn&, RGB(255,255,255), RGB(0,0,255) ' Or we could use the built-in equates: CONTROL SET COLOR hDlg, idBtn&, %RGB_WHITE, %RGB_BLUE