How to change mouse cursor shape using C ++?

How to change the flickering shape of the Windows cursor vertically, which by default (|) matches the horizontal, as in dos (_).

Is there any good feature that takes care of this?

OS: win7

+1
source share
1 answer

This is actually called a cursor, not a cursor. Probably where the confusion came from, and why the search for a solution did not bring much benefit. NullPonyPointer 's comment reflects this general confusion. The SetCursor function SetCursor really what you would like to change with the mouse cursor, but it will not work to change the caret.

Fortunately, there is a whole group of Windows functions that work with carriages: CreateCaret , ShowCaret , HideCaret , SetCaretPos and DestroyCaret . There are other ways to manipulate blinking times, but I recommend sticking with your current user settings (which will be the default).

First, a little background. I highly recommend reading the two introductory MSDN articles on carriages and using CARETS . But here is a brief summary: the carriage belongs to the window; in particular, the window that is currently focused. This window will most likely look like a text box control. When a window gains focus, it creates a carriage for use, and then when it loses focus, it destroys its carriage. Obviously, if you do not do this manually, you will get a standard implementation.

Now an example code. Since I like the interfaces of conveyor machines, I would wrap it in a function:

 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; } 

Then, in response to WM_SETFOCUS , EN_SETFOCUS or a similar notification, I would call the CreateCustomCaret function. And in response to WM_KILLFOCUS , EN_KILLFOCUS or another similar notification, I would call DestroyCaret() .

Alternatively, CreateCustomCaret can create a CreateCustomCaret from a bitmap. I can provide the following overload:

 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; } 
+2
source

Source: https://habr.com/ru/post/1385996/


All Articles