How to Change the Size of the Text Cursor in a Text Box

ID: Q94318


The information in this article applies to:
  • Microsoft Visual Basic programming system for Windows, versions 1.0, 2.0, 3.0


SUMMARY

Although there is no property that will allow you to change the appearance of the text cursor (text caret) in a Visual Basic text box, you can use the Windows API call CreateCaret() function to do so.


MORE INFORMATION

API Calls

In the example below, API calls change the size of the text cursor. The CreateCaret() function creates a new shape for the system caret and assigns ownership of the caret to the given window. The caret shape can be a line, block, or bitmap. Here's the syntax:

   Void CreateCaret(hwnd, hbmp,nwidth,nheight) 

   HWND hwnd      - handle of owner window
   HBITMAP hbmp   - handle of bitmap for caret shape
   int nwidth     - caret width
   int nheight    - caret height 
The ShowCaret() function shows the caret on the screen at the caret's current position. Once shown, the caret begins flashing automatically.

   Void ShowCaret(hwnd) 

   HWND hwnd     - handle of window with caret 
The GetFocus() function retrieves the handle of the window that currently has the input focus.

   HWND GetFocus(void) 

Example Code

To see these API calls in action do the following:

  1. Start Visual Basic or start a new project (ALT, F, N).


  2. Add two text boxes to Form1.


  3. Add the following declarations to the General Declarations section of Form1. Note that you must enter each declaration on a single line even though, for readability, the first declaration is shown on two lines:
    
       Declare Sub CreateCaret Lib "user" (ByVal w%, ByVal x%,
             ByVal y%, ByVal z%)
       Declare Function showcaret% Lib "user" (ByVal x%)
       Declare Function getfocus% Lib "user" () 


  4. Add the following code to the Command1_Click event:
    
       Sub Text1_GotFocus ()
          h% = getfocus%()                ' get the handle to the text box
          Call createcaret(h%, 0, 3, 24)  ' create new caret size
          x% = showcaret%(h%)             ' show the new caret
       End Sub 


  5. Run the program.


You will see that while the focus is on Text1 the size of the text caret in the text box appears larger than normal.

Additional query words: 2.00 3.00

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: September 1, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.