Win32 - How to get offsets for a given system cursor bitmap?

Viewed 284

The question can be reworded as "How to get a cursor click point?", if "click point" is the correct name.

After obtained a bitmap from a cursor global resource, by using a combination of LoadCursorW and a combination of GDI32 functions. And finally having a bitmap that can be draw on top of another, I realized that I don't know how to "shift" the cursor depending on where the click point is for a given cursor.

For example: while the most typical ARROW cursor bitmap has its click point at [0, 0] (left, top). The HAND cursor, the one that appears over links, is a bit translated to the left.

For the most typical cursors, because they are simple 2D concepts, you can solve it by iterating on all the bitmap pixels, and take the top most non transparent pixel as the clicking point, or if there are more than a single pixel sharing the same Y coordinate, then return a value with an X that is at the center of all those pixels.

And I'm hoping there aren't any cursor themes with cursors pointing downwards.

The CURSORINFO structure doesn't contain this information. It only gives you the current mouse x and y coordinates. And they are the same coordinates that you get by other means like GetCursorPos.

1 Answers

The Windows API already provides this information in the ICONINFO structure, that can be filled by calling GetIconInfo.

The function accepts handles to icon or cursors.

The xHotspot and yHotspot fields contain the cursor's "hotspot". By aligning these coordinates with the current/desired cursor position you can draw the cursor in the correct place.

Related