views:

1534

answers:

4

I'm working on a Win32 control. There could be hundreds of "items" on this control. Those are not windows, but internal objects (eg: rectangles). Depending on the mouse position I want to change the mouse cursor. That is fine, I can use WM_SETCURSOR.

At the same time based on mouse move I want to display a status bar which shows details about the object currently under the mouse. For that I can use WM_MOUSEMOVE.

Because there could be hundreds of items, traveling all of them to find one under the mouse, well it's not efficient, especially two times (one for set cursor, one for mouse move).

To make it short, do you know if WM_SETCURSOR and WM_MOUSEMOVE are ALWAYS in pair? In that case I can calculate what I want during WM_SETCURSOR. The other option would be to set the mouse cursor during WM_MOUSEMOVE, but as far as I know that it's not a good solution (will flicker).

Thanks

+1  A: 

While they might currently always come as a matched pair, you probably can't rely on this behaviour.

You can set the cursor during WM_MOUSEMOVE (using SetCursor), and it won't flicker, as long as (IIRC), you return TRUE from WM_SETCURSOR without doing anything (i.e. you eat the message), and your window doesn't have a class cursor assigned to it.

Roger Lipscombe
A: 

Is there any way to cache the last item that was found, and shortcut the lookup if the cursor is in the same place? That would be the most robust solution.

Mark Ransom
A: 

Thanks Mark. That is what I considered after I posted the question, and it seems the best option as I can use set cursor and mouse move correctly, but still only with one calculation.

+2  A: 

Most important of all is that your window message handlers shouldn't worry about holding or calculating anything. You should simply signal your application's logic that the mouse is potentially over new area and make it find the object(s). Once you find the hot area (or more than one), cache its (their) boundaries and check the following mouse moves against those. Once the mouse moves out from one of them, you can rebuild your hot-object-list.

You don't have to be hunting for the hot area all over the control on every mouse move.

In case when there can be many objects sharing the same area, there's the question of z-order. Think about it when you're creating those objects and handle their movement.

Also you should think about an efficient data structure holding the object coordinates so you don't have to check every single object every time you're looking for the hot one.

Just my two euros. ;)

macbirdie