Win32 WM_SETCURSOR, WM_MOUSEMOVE is always paired?

I am working on a Win32 control. There may be hundreds of โ€œitemsโ€ on this control. These are not windows, but internal objects (for example: rectangles). Depending on the position of the mouse, I want to change the mouse cursor. This is normal, I can use WM_SETCURSOR.

At the same time, based on the movement of the mouse, I want to display a status bar that displays information about the object under the mouse. For this I can use WM_MOUSEMOVE.

Because there may be hundreds of objects traveling through them to find them under the mouse, but this is not effective, especially twice (one for a given cursor, one for moving the mouse).

To do this briefly, did you know that WM_SETCURSOR and WM_MOUSEMOVE are ALWAYS paired? In this case, I can calculate what I want during WM_SETCURSOR. Another option is to set the mouse cursor during WM_MOUSEMOVE, but as far as I know this is not a good solution (it will flicker).

thanks

+4
source share
4 answers

While they can always occur as matching pairs, you probably cannot rely on this behavior.

You can set the cursor during WM_MOUSEMOVE (using SetCursor) and it will not flicker until (IIRC) you return TRUE from WM_SETCURSOR without doing anything (i.e. you are eating a message) and your window doesnโ€™t assign him a class cursor.

+2
source

Most importantly, window message handlers do not have to worry about holding or computing anything. You should simply signal to your application logic that the mouse is potentially located outside the new area and will force it to find the object (s). Once you find a hot zone (or more than one), cache your borders and check the following mouse movements on them. Once the mouse exits one of them, you can restore the list of hot objects.

You do not need to hunt for a hot zone throughout the control every time you move the mouse.

In the case where there can be many objects sharing the same area, the question arises of the z-order. Think about it when you create these objects and handle their movement.

You should also think about an effective data structure containing the coordinates of the object, so you do not need to check every object every time you look for a hot one.

Just my two euros.;)

+2
source

You can also try GetMessagePos() (gives the coordinates of the cursor screen), then MapWindowPoints() and see if it is in a hot rectangle or something like that.

+2
source

Is there a way to cache the last item found and speed up the search if the cursor is in one place? That would be the most reliable solution.

+1
source

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


All Articles