Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

BackColor, ForeColor

These properties control the color of objects. Big surprise. BackColor is the background (or “paper”) color, while ForeColor is the foreground (or “ink”) color. For some objects, one or the other is irrelevant. For example, CommandButtons don’t have a BackColor, while CommandGroups don’t have a ForeColor.

Usage

oObject.BackColor = nColor
nColor = oObject.BackColor
oObject.ForeColor = nColor
nColor = oObject.ForeColor

nColor is a color number in the range 0 to 16777215. Since we don’t have all 16 million colors at our fingertips, we generally use the predefined colors in FoxPro.H, RGB() (if we know the right red-green-blue trio) or GETCOLOR() to set these properties.

Changes to BackColor and ForeColor take place right away. However, text and graphics that have been drawn on a form (either through traditional Xbase commands like ?, DISPLAY, and so forth, or through the form’s graphic methods like Line and Circle) don’t change color when the form’s ForeColor changes. You have to redraw them to change their color.

If a control’s BackStyle is Transparent, its BackColor is ignored and the form’s BackColor or an underlying object shows instead.

Well, almost. In VFP 5 and later, when a text box or edit box with BackStyle set to Transparent gets focus, the control's BackColor shows anyway.

Be aware that the actual colors you see are affected by factors like video card and resolution. For example, under some circumstances, with certain choices for a form's (or _SCREEN's) BackColor, we see some weird effects with text drawn to the form. Rather than being transparent, there's a box of another color around the text. To see if this is an issue for you, try setting your display to 256 colors, set _SCREEN.BackColor to Magenta (8388863) and then issue DISPLAY MEMORY. We see purple boxes around the DISP MEMO information.

While these properties let you control form and object colors, we strongly advise you not to do so. If you just leave well enough alone, your forms will adopt the user’s chosen Windows colors (the ones picked in the Display Properties applet). Almost all the time, that’s your best choice.

Example

* You could let the user set a form's colors by putting
* a couple of buttons on the form. The Click code for the
* "Background Color" button might be:
ThisForm.BackColor = GETCOLOR()
* The "Foreground Color" button would have similar code.
* Note that we don't really recommend this approach.

See Also

BackStyle, ColorScheme, ColorSource, DisabledBackColor, DisabledForeColor, FillColor, FillStyle, GetColor(), #Include, RGB(), SelectedBackColor, SelectedForeColor