Controlling the Caret Color

Last reviewed: November 2, 1995
Article ID: Q84054
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

When an application creates a custom caret using a bitmap, it is possible to specify white or black for the caret color. In the case of Windows running on a monochrome display, the application can cause the caret to be the color of the display (white, green, amber, and so forth, as appropriate). However, because the caret color is determined by Windows at run time based on the hardware installed, the application cannot guarantee what color will be used under all circumstances. This article provides information about using color in a custom caret.

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:

  1. Create a pattern brush the same as the window background

  2. Select the pattern brush into a memory display context (DC)

  3. Use the PATCOPY option of the PatBlt function to copy the brush pattern into the caret bitmap.

  4. 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.


Additional reference words: 3.00 3.10 3.50 3.51 4.00 95
KBCategory: kbui
KBSubcategory: UsrCrt


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.