tags:

views:

355

answers:

2

I want to capture as a bitmap the system cursor on Windows OSes as accurately as possible. The provided API for this is to my knowledge GetCursorInfo, DrawIconEx.

The simple chain of actions is:

  • Get cursor by using GetCursorInfo
  • Paint the cursor in a memory DC by using DrawIconEx.

Here is how the code looks roughly.

CURSORINFO  CursorInfo;

(VOID)memset(&CursorInfo, 0, sizeof(CursorInfo));
CursorInfo.cbSize = sizeof(CursorInfo);

if (GetCursorInfo(&CursorInfo) &&
    CursorInfo.hCursor)
{
 // ... create here the memory DC, memory bitmap

 boError |= !DrawIconEx(hCursorDC, // device context
  0,    // xLeft
  0,    // yTop
  CursorInfo.hCursor,  // cursor handle
  0,    // width, use system default
  0,    // height, use system default
  0,    // step of animated cursor !!!!!!!!!
  NULL,    // flicker free brush, don't use it now
  DI_MASK | DI_DEFAULTSIZE); // flags

 // ... do whatever we want with the cursor in our memory DC
}

Now, anyone knows how I could get which step of the animated cursor is being drawn (I need the value that can be then passed to the istepIfAniCur parameter of DrawIconEx...)? Currently the above code obviously always renders only the first step of an animated cursor.

I suspect this can not be easily done, but it's worth asking anyway.

A: 

I suspect you are missing a step.

You need to create a bitmap to select into your device context otherwise your bit map is just a single pixel.

See CreateCompatibleBitmap in the MSDN documentation:

HBITMAP CreateCompatibleBitmap(
  HDC hdc,        // handle to DC
  int nWidth,     // width of bitmap, in pixels
  int nHeight     // height of bitmap, in pixels
);

With DrawIconEx the UINT istepIfAniCur parameter allows you to choose which frame to draw if it is an animated cursor.

It says it there in your comments :

0,  // step of animated cursor 
David L Morris
Hi David,I need to know which step of the animated cursor is being drawn so that I can pass that value to DrawIconEx. The comment about creating the DC is replacing all the code about creating the DC, bitmap, selecting the bitmap. That is not the problem.
Dan Cristoloveanu
Hmm. Almost a different question. I would look at some of the system hook functions. But I doubt it will be there.
David L Morris
+2  A: 

Unfortunately, I don't think there's a Windows API that discloses the current frame of the cursor animation. I assume that's what your after: the look of the cursor at the instant you make the snapshot.

Adrian McCarthy