我如何知道 HCURSOR 对象的大小

发布于 2024-08-10 07:09:18 字数 174 浏览 6 评论 0原文

我想获取 .cur 文件的高度和宽度,而不查看其格式。

我尝试使用 LoadCursorFromFile() 来获取 HCURSOR,我想有一个 API 函数可以获取 HCURSOR 信息,但我发现 GetCursorInfo() 根本不是我想要的。

有没有办法获取 HCURSOR 对象的高度和宽度?

I want to get the height and width of a .cur file without look into its format.

I try to use LoadCursorFromFile() to get a HCURSOR, I suppose there is a API function to obtain the HCURSOR infos, but I find that GetCursorInfo() is not I want at all.

Is there any way to get the height and width of a HCURSOR object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过期以后 2024-08-17 07:09:18

通用 C++ 代码,适用于任何光标:

SIZE GetSize(HCURSOR ico)
{
    SIZE res = {0};
    if (ico)
    {
        ICONINFO info = {0};
        if ( ::GetIconInfo(ico, &info)!=0 )
        {
            bool bBWCursor = (info.hbmColor==NULL);
            BITMAP bmpinfo = {0};
            if (::GetObject( info.hbmMask, sizeof(BITMAP), &bmpinfo)!=0)
            {
                res.cx = bmpinfo.bmWidth;
                res.cy = abs(bmpinfo.bmHeight) / (bBWCursor ? 2 : 1);
            }

            ::DeleteObject(info.hbmColor);
            ::DeleteObject(info.hbmMask);
        }
    }
    return res;
}

Universal C++ code, for any cursor:

SIZE GetSize(HCURSOR ico)
{
    SIZE res = {0};
    if (ico)
    {
        ICONINFO info = {0};
        if ( ::GetIconInfo(ico, &info)!=0 )
        {
            bool bBWCursor = (info.hbmColor==NULL);
            BITMAP bmpinfo = {0};
            if (::GetObject( info.hbmMask, sizeof(BITMAP), &bmpinfo)!=0)
            {
                res.cx = bmpinfo.bmWidth;
                res.cy = abs(bmpinfo.bmHeight) / (bBWCursor ? 2 : 1);
            }

            ::DeleteObject(info.hbmColor);
            ::DeleteObject(info.hbmMask);
        }
    }
    return res;
}
离去的眼神 2024-08-17 07:09:18

此答案已过时

请参阅 23W 的答案。这对我来说看起来很正确。

Raymond Chen 有一篇博客文章,解释了 SM_CXCURSORSM_CYCURSOR 系统指标是由显示适配器实现的硬件光标的大小。由于光标不再是现代显示硬件的功能,因此这些值已过时,但操作系统仍报告向后兼容性的值。

感谢@SO_fix_the_vote_sorting_bug 的评论让我注意到这一点。


历史背景的原始答案

Windows 中的图标和光标之间的 API 存在一些重叠。您可以调用 GetIconInfoEx 使用 HCURSOR 以及 HICON。您返回的结构将包含有关热点的信息。

我没有找到获得实际大小的方法。从技术上讲,所有光标图标都是固定大小,您可以通过询问系统获得固定大小(使用 GetSystemMetrics) 用于 SM_CXCURSORSM_CYCURSOR。看起来较小的实际上就是那个尺寸,它们只是有很多透明像素。如果您必须知道表观尺寸,则必须提取掩模并扫描位以找出边界矩形。

THIS ANSWER IS OUT OF DATE

See 23W's answer instead. That looks right to me.

Raymond Chen has a blog post that explains the SM_CXCURSOR and SM_CYCURSOR system metrics were the size of the hardware cursor implemented by the display adapter. Since cursors are no longer a feature of modern display hardware, these values are obsolete, but the OS still reports values for backward compatibility.

Thanks to @SO_fix_the_vote_sorting_bug's comment for bringing this to my attention.


ORIGINAL ANSWER FOR HISTORICAL CONTEXT

There is some overlap in the APIs between icons and cursors in Windows. You can call GetIconInfoEx with an HCURSOR as well as with an HICON. The structure you get back will have information about the hotspot.

I don't see a way to get the actual size. Technically, all cursor icons are a fixed size that you can get by asking the system (with GetSystemMetrics) for SM_CXCURSOR and SM_CYCURSOR. The ones that appear smaller are actually that size, they just have a lots of transparent pixels. If you must know the apparent size, you'll have to extract the mask and scan the bits to figure out the bounding rectangle.

泼猴你往哪里跑 2024-08-17 07:09:18

来自MSDN


nWidth 和 nHeight 参数必须指定当前显示驱动程序支持的宽度和高度,因为系统无法创建其他尺寸的光标。要确定显示驱动程序支持的宽度和高度,请使用 GetSystemMetrics 函数,并指定 SM_CXCURSOR 或 SM_CYCURSOR 值。

From MSDN:


The nWidth and nHeight parameters must specify a width and height that are supported by the current display driver, because the system cannot create cursors of other sizes. To determine the width and height supported by the display driver, use the GetSystemMetrics function, specifying the SM_CXCURSOR or SM_CYCURSOR value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文