WinAPI get mouse cursor icon

2020-03-24 07:31发布

问题:

I want to get the cursor icon in Windows. I think language I use isn't very important here, so I will just write pseudo code with WinAPI functions I'm trying to use:

c = CURSORINFO.new(20, 1, 1, POINT.new(1,1));
GetCursorInfo(c); #provides correctly filled structure with hCursor

DrawIcon(GetWindowDC(GetForegroundWindow()), 1, 1, c.hCursor);

So this part works fine, it draws current cursor on active window. But that's not what I want. I want to get an array of pixels, so I should draw it in memory.

I'm trying to do it like this:

hdc = CreateCompatibleDC(GetDC(0)); #returns non-zero int
canvas = CreateCompatibleBitmap(hdc, 256, 256); #returns non-zero int too

c = CURSORINFO.new(20, 1, 1, POINT.new(1,1));
GetCursorInfo(c);

DrawIcon(hdc, 1, 1, c.hCursor); #returns 1
GetPixel(hdc, 1, 1); #returns -1

Why doesn't GetPixel() return COLORREF? What am I missing?

I'm not very experienced with WinAPI, so I'm probably doing some stupid mistake.

回答1:

You have to select the bitmap you create into the device context. If not, the GetPixel function will return CLR_INVALID (0xFFFFFFFF):

A bitmap must be selected within the device context, otherwise, CLR_INVALID is returned on all pixels.

Also, the pseudo-code you've shown is leaking objects badly. Whenever you call GetDC, you must call ReleaseDC when you're finished using it. And whenever you create a GDI object, you must destroy it when you're finished using it.

Finally, you appear to be assuming that the coordinates for the point of origin—that is, the upper left point—are (1, 1). They are actually (0, 0).

Here's the code I would write (error checking omitted for brevity):

// Get your device contexts.
HDC hdcScreen = GetDC(NULL);
HDC hdcMem = CreateCompatibleDC(hdcScreen);

// Create the bitmap to use as a canvas.
HBITMAP hbmCanvas = CreateCompatibleBitmap(hdcScreen, 256, 256);

// Select the bitmap into the device context.
HGDIOBJ hbmOld = SelectObject(hdcMem, hbmCanvas);

// Get information about the global cursor.
CURSORINFO ci;
ci.cbSize = sizeof(ci);
GetCursorInfo(&ci);

// Draw the cursor into the canvas.
DrawIcon(hdcMem, 0, 0, ci.hCursor);

// Get the color of the pixel you're interested in.
COLORREF clr = GetPixel(hdcMem, 0, 0);

// Clean up after yourself.
SelectObject(hdcMem, hbmOld);
DeleteObject(hbmCanvas);
DeleteDC(hdcMem);
ReleaseDC(hdcScreen);

But one final caveat—the DrawIcon function will probably not work as you expect. It is limited to drawing an icon or cursor at the default size. On most systems, that will be 32x32. From the documentation:

DrawIcon draws the icon or cursor using the width and height specified by the system metric values for icons; for more information, see GetSystemMetrics.

Instead, you probably want to use the DrawIconEx function. The following code will draw the cursor at the actual size of the resource:

DrawIconEx(hdcMem, 0, 0, ci.hCursor, 0, 0, 0, NULL, DI_NORMAL);