Device Context Attributes for Text

Several device context attributes affect text. In the default device context, the text color is black, but you can change that:

SetTextColor (hdc, rgbColor) ;

As with pen colors and hatch brush colors, Windows converts the value of rgbColor to a pure color. You can obtain the current text color by calling GetTextColor.

The spaces between the character strokes are colored in, based on the setting of the background mode and the background color. You can change the background mode using:

SetBkMode (hdc, nMode) ;

where nMode is either OPAQUE or TRANSPARENT. The default background mode is OPAQUE, which means that Windows uses the background color to fill in the area between the character strokes. You can change the background color by using:

SetBkColor (hdc, rgbColor) ;

The value of rgbColor is converted to that of a pure color. The default background color is white. If the background mode is set to TRANSPARENT, Windows ignores the background color and doesn't color the area between the character strokes. Windows also uses the background mode and background color to color the spaces between dotted and dashed lines and the area between the hatches of hatched brushes, as you saw in Chapter 12.

Many Windows programs specify WHITE_BRUSH as the brush that Windows uses to erase the background of a window. The brush is specified in the window class structure. However, you may want to make the background of your program's window consistent with the ”system colors“ that a user can set in the CONTROL program. In that case, you would specify the background color this way:

wndclass.hbrBackground = COLOR_WINDOW + 1 ;

When you want to write text to the client area, you can set the text color and background color using the current system colors:

SetTextColor (hdc, GetSysColor (COLOR_WINDOWTEXT)) ;

SetBkColor (hdc, GetSysColor (COLOR_WINDOW)) ;

If you do this, then you'll want your program to be alerted if the system colors change:

case WM_SYSCOLORCHANGE:

InvalidateRect (hwnd, NULL, TRUE) ;

break ;

Another device context attribute that affects text is the intercharacter spacing. By default it's set to 0, which means that Windows doesn't add any space between characters. You can insert space by using the function:

SetTextCharacterExtra (hdc, nExtra) ;

The nExtra parameter is in logical units. Windows converts it to the nearest pixel, which can be 0. If you use a negative value for nExtra (perhaps in an attempt to squeeze characters closer together), Windows takes the absolute value of the number: You can't make the value less than 0. You can obtain the current intercharacter spacing by calling GetTextCharacterExtra. Windows converts the pixel spacing to logical units before returning the value.