Company logo

FOXSTUFF

Advice, tips, techniques and downloads for Visual Foxpro developers.

Home | Contact

Add a dash of colour to your menus

How to use colour pairs in menu pads and bars

When you use Visual FoxPro's menu designer, you can't choose the colours of the generated menu. You're stuck with whatever colour scheme the user chose through their Control Panel settings. For the most part, that's how it should be.

But there might be times when you want to override the user's settings and choose specific colours for the various elements of the menu. For example, you might want to use colours to group the menu items in some way, or you might want to emphasise certain commands by displaying them in a different shade. In this article, we'll explain how to do that.

Although you can't control the colours of menu items within the menu designer, you can do so if you write the menu code by hand, or hand-modify the code that the designer generates. (Remember, if you hand-modify generated code, your changes will be lost if you use the designer to generate the code again.)

The COLOR clause

The trick is to use the COLOR clause in the DEFINE PAD, DEFINE POPUP or DEFINE BAR commands. (DEFINE PAD creates the top-level menu title, DEFINE POPUP creates the menu itself, and DEFINE BAR creates an individual menu item.) According to the VFP Help, the COLOR clause includes something called a ColorPairList. Unfortunately, the Help fails to tell you what a ColorPairList looks like or how to code it.

In fact, it's quite easy. A ColorPairList is a comma-separated list of up to ten values. Each value corresponds to a different element of the menu, as indicated in Table 1. Of these ten items, the ones that you are most likely to want to customise are the first, second and sixth. These determine the colours of disabled items, normal (enabled) items and selected items respectively.

Item in list Meaning
1 Disabled item
2 Enabled item
3 Border
4 Title
5 Message
6 Selected item
7 Hotkey indicator
8 Shadow
9 Enabled control
10 Disabled control

Table 1: The ten items in a ColorPairList

Within the list, you can choose between two methods of coding the various colours. The first method relies on combinations of letters, separated by a forward slash. The letters represent the foreground and background colours respectively. For example, W/G is white on green, while N/RB is black on magenta. Table 2 lists the available colours. The plus sign (as in GR+) indicates a "bright" version of the colour; this is available for the foreground colour only. To produce a bright version of a background colour, add an asterisk.

Colour  Code
Black N
Blue B
Brown GR
Cyan BG
Green G
Magenta RB
Red R
White W
Yellow GR+

Table 2: Alphabetic colour codes

Here then is a command to create a menu in which disabled items appear as green on white, normal items as blue on white, and the selected item as "bright" white on brown (if the plus sign was omitted from the last element, the "white" would show up as a medium grey).

DEFINE POPUP Shortcut COLOR G/W*, B/W*,,,,W+/GR

Note the use of commas as place-holders for missing items.

A wider choice of colours

Coding these colour pairs is straightforward, but the system has an obvious drawback: you are limited to a few basic colours.

For a wider choice of colours, you use the second form of the colour pair. Here, each colour is represented by what looks at first glance like a standard RGB() function. But this is deceptive. It isn't really a function it all - it just uses similar syntax. Unlike a real RGB() function, it takes six arguments, these being the three-part colour codes for the foreground and background colours respectively.

For example, the colour pair for light grey text on a white background would be RGB(192,192,192,255,255,255), while dark brown on a yellow background would be RGB(128,64,64,128,128,0).

The following command produces the same colours as the one shown above, but using the alternative syntax:

DEFINE POPUP Shortcut COLOR ;
  RGB(0,128,0,255,255,255), ;
  RGB(0,0,255,255,255,255),,,, ;
  RGB(255,255,255,128,128,0)

Should you want to, you can mix the two forms in the same command:

DEFINE POPUP Shortcut COLOR ;
  G/W* ;
  RGB(0,0,255,255,255,255),,,, ;
  RGB(255,255,255,128,128,0)

And that's all there is to it. Using the COLOR clause and the ColorPairList, you can apply any colours you like to any part of a menu. But remember, you should only do this if you have a good reason. For most of the time, the default colours provided by the menu designer are the best ones to use.

Mike Lewis Consultants Ltd. February 2007.

More Visual FoxPro articles | Crystal Reports articles | Recommended books | Visual FoxPro consultancy | Contact us

FoxStuff is maintained by Mike Lewis Consultants Ltd. as a service to the VFP community. Feel free to download and use any code or components, and to pass around copies of the articles (but please do not remove our copyright notices or disclaimers).

The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly in your applications.

© Copyright Mike Lewis Consultants Ltd.