Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleHide the caret in a TextBox
DescriptionThis example shows how to hide the caret in a TextBox in Visual Basic 6. Because Visual Basic frequently restores the caret, subclass and rehide the caret when the control receives the WM_PAINT or H100E events.
KeywordsTextBox, caret, hide caret, cursor
CategoriesControls, Tips and Tricks, API
 
Use the HideCaret API function. Unfortunately, Visual Basic restores the caret frequently. Subclass and hide the caret whenever the TextBox gets message WM_PAINT or &H100E (whatever that is).

Warning: Subclassing is dangerous. The debugger does not work well when a new WindowProc is installed. If you halt the program instead of unloading its forms, it will crash and so will the Visual Basic IDE. Save your work often! Don't say you weren't warned.

 
Private Sub Form_Load()
    OldWindowProc = SetWindowLong( _
        Text1.hwnd, GWL_WNDPROC, _
        AddressOf NewWindowProc)
End Sub
 
The new WindowProc watches for the WM_NCDESTROY message and restores the original WindowProc when the form is closing. When it sees the WM_PAINT or &H100E message, it calls HideCaret.
 
Public Function NewWindowProc(ByVal hwnd As Long, ByVal msg _
    As Long, ByVal wParam As Long, ByVal lParam As Long) As _
    Long
Const WM_NCDESTROY = &H82
Const WM_RESERVED = &H100E
Const WM_PAINT = &HF

    ' If we're being destroyed,
    ' restore the original WindowProc.
    If msg = WM_NCDESTROY Then
        SetWindowLong _
            hwnd, GWL_WNDPROC, _
            OldWindowProc
    End If

    If msg = WM_PAINT Or msg = WM_RESERVED _
        Then HideCaret Form1.Text1.hwnd

    NewWindowProc = CallWindowProc( _
        OldWindowProc, hwnd, msg, wParam, _
        lParam)
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated