Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB4/32,VB5,VB6
Expertise: Intermediate
Apr 20, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Change caret size and blink rate

You can alter the size of the caret (this is the name of the cursor within text boxes, not to be confused with the mouse cursor), for instance to ease visibility on notebook computers, or to signal that the text box is in overwrite mode. All you need is a couple of API functions:

Declare Function CreateCaret Lib "user32" (ByVal hWnd As Long, _
    ByVal hBitmap As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Declare Function ShowCaret Lib "user32" (ByVal hWnd As Long) As Long
Declare Function SetCaretBlinkTime Lib "user32" (ByVal wMSeconds As Long) As _
Declare Function GetCaretBlinkTime Lib "user32" () As Long

Sub ShowCustomCaret(ByVal width As Integer, ByVal height As Integer)
    On Error Resume Next
    With Screen.ActiveForm.ActiveControl
        CreateCaret .hWnd, 0, width, height
        ShowCaret .hWnd
    End With
End Sub
The second argument to CreateCaret is the handle of a bitmap, a feature that is not used in the above routine. The best place to call the ShowCustomCaret procedure is from within the GotFocus event of a text box:

Sub Text1_GotFocus()
    ' show a block cursor
    ShowCustomCaret 6, 14
End Sub
Unfortunately, Visual Basic tends to restore the default caret each time a text box gets the input focus, so we should insert a call to ShowCustomCaret routine in the GotFocus of all the text boxes on the form. This won't suffice, however, if the user switches to another application and then back to this form, because in that case no event is fired and we have no chance to re-enforce our custom cursor.

There are two more API functions that deal with carets: GetCaretBlinkTime returns the blink rate of the caret in milliseconds, while SetCaretBlinkTime can change it. Please note, however, that the caret is a shared resource, so you should always restore the blink rate to the original value in the Unload event of the form.

Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date