Hiding a Caret

Whenever your application redraws a screen while processing a message other than WM_PAINT, it must make the caret invisible by using the HideCaret function. When your application is finished drawing, redisplay the caret by using the ShowCaret function. If your application processes the WM_PAINT message, it is not necessary to hide and redisplay the caret, because this function does this automatically.

The following code sample shows how to have your application hide the caret while drawing a character on the screen and while processing the WM_CHAR message.

HWND hwnd, // window handle

HDC hdc; // device context

case WM_CHAR:

switch (wParam)

{

case 0x08:

.

. // Process a backspace.

.

break;

case 0x09:

.

. // Process a tab.

.

break;

case 0x0D:

.

. // Process a carriage return.

.

break;

case 0x1B:

.

. // Process an escape.

.

break;

case 0x0A:

.

. // Process a linefeed.

.

break;

default:

// Hide the caret.

HideCaret(hwnd);

// Draw the character on the screen.

hdc = GetDC(hwnd);

SelectObject(hdc,

GetStockObject(SYSTEM_FIXED_FONT));

TextOut(hdc, x, y, lpszChar, 1);

ReleaseDC(hwnd, hdc);

// Display the caret.

ShowCaret(hwnd);

}

If your application calls the HideCaret function several times without calling ShowCaret, the caret will not be displayed until the application also calls ShowCaret the same number of times.