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 LongDeclare Function ShowCaret Lib "user32" (ByVal hWnd As Long) As LongDeclare Function SetCaretBlinkTime Lib "user32" (ByVal wMSeconds As Long) As _    LongDeclare Function GetCaretBlinkTime Lib "user32" () As LongSub 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 WithEnd 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, 14End 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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: