Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is actually called a <em>caret</em>, rather than a <em>cursor</em>. That's probably where the confusion comes from, and why searching for a solution didn't yield very much of use. <a href="https://stackoverflow.com/questions/13439717/how-to-change-windows-blink-cursor-shape-from-c#comment18373963_13439717">NullPonyPointer's comment</a> reflects this common confusion as well. The <code>SetCursor</code> function is indeed what you would want to change the mouse cursor, but it won't work to change the caret.</p> <p>Fortunately, there is a whole group of Windows functions that work with carets: <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms648399.aspx" rel="nofollow noreferrer"><code>CreateCaret</code></a>, <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms648406.aspx" rel="nofollow noreferrer"><code>ShowCaret</code></a>, <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms648403.aspx" rel="nofollow noreferrer"><code>HideCaret</code></a>, <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms648405.aspx" rel="nofollow noreferrer"><code>SetCaretPos</code></a>, and <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms648400.aspx" rel="nofollow noreferrer"><code>DestroyCaret</code></a>. There are some others for manipulating blink time, but I recommend sticking to the user's current settings (which will be the default).</p> <p>First, a bit of background. I strongly recommend reading the two introductory MSDN articles <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms648397.aspx" rel="nofollow noreferrer">about carets</a> and on <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms648398.aspx" rel="nofollow noreferrer">using carets</a>. But here's a quick summary: The caret is owned by a window; in particular, the window that currently has the focus. This window will likely be something like a text box control. When the window receives the focus, it creates a caret to use, and then when it loses the focus, it destroys its caret. Obviously, if you don't do any of this manually, you will receive the default implementation.</p> <p>Now, the sample code. Because I like candy machine interfaces, I'd wrap it in a function:</p> <pre><code>bool CreateCustomCaret(HWND hWnd, int width, int height, int x, int y) { // Create the caret for the control receiving the focus. if (!CreateCaret(hWnd, /* handle to the window that will own the caret */ NULL, /* create a solid caret using specified size */ width, /* width of caret, in logical units */ height)) /* height of caret, in logical units */ return false; // Set the position of the caret in the window. if (!SetCaretPos(x, y)) return false; // Show the caret. It will begin flashing automatically. if (!ShowCaret(hWnd)) return false; return true; } </code></pre> <p>Then, in response to <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms646283.aspx" rel="nofollow noreferrer"><code>WM_SETFOCUS</code></a>, <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb761685.aspx" rel="nofollow noreferrer"><code>EN_SETFOCUS</code></a>, or a similar notification, I would call the <code>CreateCustomCaret</code> function. And in response to <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms646282.aspx" rel="nofollow noreferrer"><code>WM_KILLFOCUS</code></a>, <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb761682.aspx" rel="nofollow noreferrer"><code>EN_KILLFOCUS</code></a>, or another similar notification, I would call <code>DestroyCaret()</code>.</p> <p>Alternatively, <code>CreateCustomCaret</code> could have created the caret from a bitmap. I might provide the following overload:</p> <pre><code>bool CreateCustomCaret(HWND hWnd, HBITMAP hbmp, int x, int y) { // Create the caret for the control receiving the focus. if (!CreateCaret(hWnd, /* handle to the window that will own the caret */ hBmp, /* create a caret using specified bitmap */ 0, 0)) /* width and height parameters ignored for bitmap */ return false; // Set the position of the caret in the window. if (!SetCaretPos(x, y)) return false; // Show the caret. It will begin flashing automatically. if (!ShowCaret(hWnd)) return false; return true; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload