通过 WinAPI 提取光标大小(Windows 10) [英] Extract cursor size via WinAPI (Windows 10)

查看:62
本文介绍了通过 WinAPI 提取光标大小(Windows 10)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提取光标图像以便在我的 Swing 应用程序 (Java) 中使用它.

首先,我发现了

当用户有例如粉色光标然后比标准光标大:

在这种情况之后,我决定做同样的沙箱应用程序,但是通过 C++ 和 WinAPI.我使用了这个

我认为主要的瓶颈是 GetSystemMetrics 函数返回默认光标大小 (32x32),并且在光标大小改变后它不会改变.我在这里找到了一些证明,但我不确定.

所以我的问题是:

如何获取光标的实际大小?

非常感谢!

解决方案

感谢 @Rita Han - MSFT 提供开始搜索的初始点.

您可以通过两种方式获得真实的光标大小:

  1. 第一种方式之前提到过:我们需要从HKEY_CURRENT_USER\Control Panel\Cursors下的注册表值CursorBaseSize中获取值.
  2. 找到第二种方式这里:我们可以获得光标大小乘数形式的注册表值CursorSize HKEY_CURRENT_USER\Software\Microsoft\Accessibility 下,然后自己计算光标大小.它可以像这样完成:newHeight = cursorHeight + (multiplier - 1) * (cursorHeight/2); 其中 cursorHeight 是来自 GetSystemMetrics(SM_CYCURSOR) 的值multiplier 是来自注册表的值.光标实际宽度值将与 newHeight 相同.如果我们使用 C++,所有值都是 unsigned long.`

两种求大小的方法都得到相同的结果.

如果您需要从注册表中在Java中查找光标的实际大小值,您可以使用JNA-platform 中的Advapi32Util 类:

//读取一个 int (& 0xFFFFFFFFL for large unsigned int)int baseSize = Advapi32Util.registryGetIntValue(WinReg.HKEY_CURRENT_USER, "Control Panel\\Cursors", "CursorBaseSize");

I need to extract the cursor image for using it in my swing application (Java).

Firstly, I found similar question, modified code snippet from an answer, and got this sandbox application.

Shortly, it gets cursor image via JNA and WinAPI, then shows this image on JFrame and sets custom cursor for this frame.

In most cases, this code works correctly and suits my tasks. But then I found that when the user changes the cursor's size or color(Windows 10 feature), the program extracts an invalid cursor image that matches the original cursor neither in color nor in size.

When the user has standard cursor:

When the user has e.g. pink cursor then bigger than standard one:

After this situation, I decided to do the same sandbox application, but via C++ and WinAPI. I used answers for this question, but solved only troubles with color.

I think the main bottleneck is that GetSystemMetrics function returns default cursor size (32x32) and it doesn't change after the cursor's size changing. I found some proves for this here, but I'm not sure.

So my question is:

How can I get the real size of cursor?

Thanks a lot in advance!

解决方案

Thanks @Rita Han - MSFT for giving the initial point to start searching.

You can get a real cursor size in two ways:

  1. The first way is mentioned before: we need to get value from registry value CursorBaseSize under HKEY_CURRENT_USER\Control Panel\Cursors.
  2. The second way was found here: we can get cursor size multiplier form registry value CursorSize under HKEY_CURRENT_USER\Software\Microsoft\Accessibility and then calculate the cursor size yourself. It can be done somehow like this: newHeight = cursorHeight + (multiplier - 1) * (cursorHeight / 2); where cursorHeight is value from GetSystemMetrics(SM_CYCURSOR) and multiplier is value from the registry. The cursor real width value will be the same as newHeight. All values are unsigned long if we use C++.`

Both methods of finding the size lead to the same result.

If you need to find to get cursor real size value from the registry in Java, you can use Advapi32Util class from JNA-platform:

        // Read an int (& 0xFFFFFFFFL for large unsigned int)
        int baseSize = Advapi32Util.registryGetIntValue(
                WinReg.HKEY_CURRENT_USER, "Control Panel\\Cursors", "CursorBaseSize");

这篇关于通过 WinAPI 提取光标大小(Windows 10)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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