How to Create and Display a Caret on the Window

2 01 2009

description1

 

 

The CreateCaret Win32 API function creates a new shape for the system caret and assigns ownership of the caret to the specified window. The caret shape can be a line, a block, or a bitmap. Also it is using some other API functions such as SetCaretPos, ShowCaret and HideCaret. Then the caret blink time could adjust by the functions GetCaretBlinkTime and SetCaretBlinkTime.

mycodesnippet1

 

 

void CreateSolidBlackCaret()
{
   // window handle
   HWND hwnd,      
   // width of cursor
   int nWidth = 100;     
   
   // height of cursor
   int nHeight = 100;
   // horizontal coordinate of cursor
   int x       = 10;          
   // vertical coordinate of cursor
   int y       = 10;           

   //Create a solid black caret.
   ::CreateCaret(hwnd,(HBITMAP)NULL,nWidth,nHeight);
  
   //Adjust the caret position
   ::SetCaretPos(x,y);
   //Display the caret
   ::ShowCaret(hwnd);
}

void HideMyCaret()
{
   ::HideCaret(hwnd);
}

void AdjustCaretBlinkTime()
{
   UINT n = GetCaretBlinkTime();
   SetCaretBlinkTime(n -/+ 100);
}

mynote1

 

 

The system provides one caret per queue. A window should create a caret only when it has the keyboard focus or is active. The window should destroy the caret before losing the keyboard focus or becoming inactive.