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?

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Don't forget to initialize the size of the CURSORINFO, so ci.cbSize = sizeof(ci);
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!

2 Comments

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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.