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

Last reviewed: June 21, 1995
Article ID: Q94318
The information in this article applies to:

- Microsoft Visual Basic programming system for Windows,

  versions 1.0, 2.0, and 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 reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: PrgCtrlsStd


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: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.