5

I am coding little fun gadget. I want to be able to draw second (or more) mouse pointer icons at different location than the original mouse but to move it according to move of original mouse.

I know how to track movement of the mouse but I dunno how to draw/redraw mouse pointer; can anyone help?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
VisaToHell
  • 508
  • 1
  • 12
  • 29

2 Answers2

7

You can use the following code:

CURSORINFO ci;
ci.cbSize = sizeof(CURSORINFO);
GetCursorInfo(&ci);

Next you can draw a cursor by calling:

DrawIcon(ContextDC, YourXPosition, YourYPosition, ci.hCursor);

If you need additional information about the cursor, like hotspot for example, check the ICONINFO structure:

ICONINFO ii;
GetIconInfo(ci.hCursor, &ii);
Elmue
  • 7,602
  • 3
  • 47
  • 57
Forgottn
  • 563
  • 3
  • 11
  • That is the answer that I was looking for, because DrawCursor() does not exist. MSDN says: DrawIcon() function: Draws an icon or cursor into the specified device context. – Elmue May 11 '15 at 23:54
  • Don't forget to initialize the size of the `CURSORINFO`, so `ci.cbSize = sizeof(ci);` – Ziriax May 17 '19 at 15:19
1

This could be done like:

  1. grab the current mouse cursor from your application, using LoadCursor(). Just specify NULL, and the cursor you want. Or just load a bitmap for the cursor. Now, you have a bitmap.

  2. Next step is to get the Device context of your Desktop: GetWindowDC(NULL). This will give you the opportunity to draw on the desktop anywhere.

  3. There is a huge chance that you will need to apply CreateCompatibleBitmap() to the Image at #1 with the DC obtained at #2.

  4. Now, use some BitBlt() to copy bits OUT from the DC obtained at #2 into a save image (YOU will need to create these) from the position you want to put your cursor.

Now, put the image obtained at #3 onto the DC of the Desktop obtained at #2 at the position you want.

When the user moved the mouse restore the image on the desktop with the saved data at #4. Release all the stuff you don't need (yes, this is mandatory).

And restart from #1.

These two more links might help:

Bitmaps, Device Contexts and BitBlt

Capturing an Image

Good luck!

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • This will never work in a clean manner. Imagine a window that shows dynamic content like a browser that shows an animation. You want to copy back old content when the user moves the mouse, then you get lots of artifacts. Simply forget it. – Elmue May 12 '15 at 00:03
  • I would simply load the cursor bitmap and then create a separate transparent window to display it, then move that window around as needed. Much cleaner and easier to code, and it doesn't have to mess around with the desktop at all – Remy Lebeau Mar 02 '20 at 20:24