Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

CreateCaret (user32)
 
.
Summary

C# Signature:

[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth,
   int nHeight);

VB.NET:

    <DllImport("user32.dll", SetLastError:=True)> _
    Public Shared Function CreateCaret(ByVal hWnd As IntPtr, ByVal hBitmap As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As Boolean
    <DllImport("user32.dll")> _
    Function CreateCaret(ByVal hWnd As IntPtr, _
            ByVal hBitmap As IntPtr, _
            ByVal nWidth As Integer, _
            ByVal nHeight As Integer) As Boolean
    End Function

User-Defined Types:

None.

Notes:

Windows allows only one caret per message queue. To add a caret to a control, handle the WM_SETFOCUS message, or the GotFocus event, or override OnGotFocus if you're writing a custom control, and call CreateCaret from the message or event handler. You should also handle WM_KILLFOCUS, LostFocus or OnLostFocus and call DestroyCaret. You will also need to call ShowCaret to make the caret visible, and SetCaretPos to set its position.

User-Defined Types:

None.

Tips & Tricks:

If you're using a Bitmap image to render the caret, the colour will be changed based on the background of the Control. To ensure that you have the expected colour, first apply an XOR mask:

    int r = CursorColour.R ^ ParentControl.BackColor.R;
    int g = CursorColour.G ^ ParentControl.BackColor.G;
    int b = CursorColour.B ^ ParentControl.BackColor.B;

Where CursorColour is the colour you would like the caret to have and ParentControl is the control that the caret is bound to.

Notes:

Windows allows only one caret per message queue. To add a caret to a control, handle the WM_SETFOCUS message, or the GotFocus event, or override OnGotFocus if you're writing a custom control, and call CreateCaret from the message or event handler. You should also handle WM_KILLFOCUS, LostFocus or OnLostFocus and call DestroyCaret. You will also need to call ShowCaret to make the caret visible, and SetCaretPos to set its position.

Sample Code:

Tips & Tricks:

If you're using a Bitmap image to render the caret, the colour will be changed based on the background of the Control. To ensure that you have the expected colour, first apply an XOR mask:

    int r = CursorColour.R ^ ParentControl.BackColor.R;
    int g = CursorColour.G ^ ParentControl.BackColor.G;
    int b = CursorColour.B ^ ParentControl.BackColor.B;

Where CursorColour is the colour you would like the caret to have and ParentControl is the control that the caret is bound to.

//C# Example. This will create and show the caret in textBox1 when button1 is clicked.

Sample Code:

    private void button1_Click(object sender, System.EventArgs e) {
        CreateCaret(textBox1.Handle, IntPtr.Zero, 0, textBox1.Height);
        ShowCaret(textBox1.Handle);
    }

''Visual Basic

    <DllImport("user32.dll", SetLastError:=True)>
    Public Shared Function CreateCaret(ByVal hWnd As IntPtr, ByVal hBitmap As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As Boolean
    End Function

//C# Example. This will create and show the caret in textBox1 when button1 is clicked.

    Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
    HideCaret(TextBox1.Handle)
    Dim bmPtr As IntPtr = GetHBitmap()
    CreateCaret(TextBox1.Handle, bmPtr, 2, 12)
    SetCaretBlinkTime(300)
    ShowCaret(TextBox1.Handle)
    End Sub
    private void button1_Click(object sender, System.EventArgs e) {
        CreateCaret(textBox1.Handle, IntPtr.Zero, 0, textBox1.Height);
        ShowCaret(textBox1.Handle);
    }

Alternative Managed API:

Do you know one? Please contribute it!

    The hBitmap parameter is a handle to the bitmap you want to use(or gray/solid).

    You can use the following to specify if you want gray/solid:

    Public Class hBMParameters
    Public Shared Property Gray As IntPtr = CType(1, IntPtr)
    Public Shared Property Solid As IntPtr = Nothing
    End Class

Documentation
CreateCaret on MSDN

    Bitmaps have a method attached to return the handle

    Example:  
    Function GetHBitmap() As IntPtr
    Dim embeddedBitmap As Byte() = {137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 10, 0, 0, 0, 10, 8, 2, 0, 0, 0, 2, 80, 88, 234, 0, 0, 0, 1, 115, 82, 71, 66, 0, 174, 206, 28, 233, 0, 0, 0, 4, 103, 65, 77, 65, 0, 0, 177, 143, 11, 252, 97, 5, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 14, 195, 0, 0, 14, 195, 1, 199, 111, 168, 100, 0, 0, 0, 48, 73, 68, 65, 84, 40, 83, 99, 248, 143, 23, 16, 39, 205, 192, 128, 162, 14, 206, 165, 138, 225, 184, 0, 204, 14, 252, 118, 3, 249, 8, 33, 100, 54, 132, 2, 2, 136, 40, 4, 64, 133, 144, 165, 177, 128, 255, 255, 1, 57, 41, 7, 8, 21, 108, 65, 188, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130}
    Dim bm As Bitmap
    Using s As New System.IO.MemoryStream(embeddedBitmap)
        bm = CType(Bitmap.FromStream(s), Bitmap)
    End Using
    Return bm.GetHbitmap()
    End Function

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
CreateCaret on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions