WINAPI得到鼠标指针的图标 [英] WinAPI get mouse cursor icon

查看:1929
本文介绍了WINAPI得到鼠标指针的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows光标图标。
我觉得语言我用的是不是在这里非常重要,所以我只写假code。与WinAPI的功能,我试图使用方法:

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.

我试图做这样的:

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

为什么不与getPixel()返回COLORREF?我缺少什么?

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

我不是很有经验的WinAPI的,所以我可能做一些愚蠢的错误。

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

推荐答案

您必须选择创建到设备上下文的位图。如果没有, GetPixel 功能将返回 CLR_INVALID (0xFFFFFFFF的):

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

一个位图必须在设备范围内进行选择,否则, CLR_INVALID 上的所有像素被返回。

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

另外,你已经证明了伪code受到严重泄漏对象。每当你叫的GetDC ,您的必须的通话 ReleaseDC 当你使用它完成。每当您创建一个GDI对象,你当你使用完毕后必须摧毁它。

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.

最后,你似乎是假设为原点,也就是点的坐标,左上角点为(1,1)。它们实际上是(0,0)。

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

这里的code,我会写(检查不再赘述错误):

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);


但最后一个警告 - 在 DrawIcon 功能会的可能的像您期望无法正常工作。它仅限于在缺省大小绘制图标或光标。在大多数系统中,这将是32×32。从文档:


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 绘制图标或光标使用由系统指定的图标量度值的宽度和高度;有关更多信息,请参见 GetSystemMetrics的

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

相反,你可能想使用 DrawIconEx 功能。下面code将绘制光标在资源的实际大小:

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);

这篇关于WINAPI得到鼠标指针的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆