MORE INFORMATION
To create a caret, first create a bitmap with the desired pattern. To
display the caret, Windows exclusive-ORs (XORs) and takes the opposite
of the result (the NOT of the result) of the bitmap with the
background of the client window. Therefore, to create a white caret,
create a bitmap that when XOR'd with the window background will have
an opposite value that will create a white color. It is when the caret
blinks that Windows uses the reverse of the bitmap XOR'd with the
background to draw the caret; this creates the white blink seen on the
screen.
The bitmap for the caret cannot use a color palette. Windows does not
use the color values from the palette in its calculations but the
indices into the palette. While it is possible to use palette indices
successfully, perfect symmetry of the colors in the palette is
required. This is unlikely. For each color in the palette, its exact
opposite color must be in the palette, in the exactly opposite index
position.
However, when the application creates bitmaps itself, it has complete
control over the bits. Therefore, the application can create the
perfect counterpart that corresponds to the window background color.
If the application uses this information to create the caret bitmap,
when Windows creates the caret, it can choose the closest color
available in the system palette.
Therefore, to create a white caret (or a black one, if the screen has
too many light elements), the task is straightforward. Windows always
reserves a few colors in the system palette and makes them available
to all applications. On a color display, these colors include black
and white. On a monochrome display, these colors are whatever the
monochrome color elements are.
Because black and white (or the monochrome screen colors) are always
available, the application simply creates a bitmap that, when XOR'd
with the screen background color, produces black or white.
The technique involves one main principle: background XOR background =
FALSE. Anything XOR'd with itself returns FALSE, which in bitmap terms
maps to the color black.
The process of creating a caret from the background color involves the
four steps discussed below:
- Create a pattern brush the same as the window background
- Select the pattern brush into a memory display context (DC)
- Use the PATCOPY option of the PatBlt function to copy the brush
pattern into the caret bitmap.
- Specify the caret bitmap in a call to the CreateCaret function.
When this caret is XOR'd with the background, black will result. When
the caret blinks, and is therefore displayed, Windows computes the
opposite of the caret and XOR's this value into the background. This
yields NOT(background XOR background) = NOT(FALSE) = TRUE which
corresponds to WHITE. The first background represents the caret
bitmap and the second is the current background color of the window.
Note that half the time the custom bitmap is displayed (when the caret
"blinks") the other half of the time the background is displayed,
(between "blinks").
If the background color is light gray or lighter [RGB values
(128, 128, 128) through (255, 255, 255)], then a black caret is
usually desired. The process of creating a black caret is just as
straightforward. Modify step 1 of the process given above to
substitute the inverse of the background for the background bitmap.
When the caret blinks, it will show black. The equation that
corresponds to this case is NOT(inverse of background XOR background)
= NOT(TRUE) = FALSE which corresponds to BLACK.
To change the caret color to something other than black or white
requires considerably more work, with much less reliable results
because the application must solve the following equation:
NOT(caret XOR background) = desired_color on the
"blink" of the caret.
where the value for the caret color must be determined given the
desired color. A series of raster operations is required to solve this
type of equation. (For more information on raster operations, see
chapter 11 of the "Microsoft Windows [3.0] Software Development Kit
Reference, Volume 2" or pages 573-585 of the "Microsoft Windows [3.1]
Software Development Kit Programmer's Reference, Volume 3: Messages,
Structures, and Macros.")
Even after solving this equation, the color actually displayed is
controlled by Windows and the colors in the current system palette.
With colors other than black or white, an exact match for the desired
color may not be available. In that case, Windows will provide the
closest match possible. Because the palette is a dynamic entity and
can be modified at will, it is impossible to guarantee a particular
result color in all cases. The colors black and white should be safe
most of the time because it is quite unusual for an application to
modify the reserved system colors. Even when an application does
change the system palette, it most likely retains a true black and a
true white.
As long as a black and white remain in the palette (which is usually
the case), this algorithm will provide a white or black caret.