从 .cur 格式字节创建游标?
我可以找到几种在 win32 中创建光标的方法,但我需要第三种,但我找不到。
我能找到的第一个是使用 LoadXXX() 打开文件或资源并以这种方式加载光标。
我能找到的第二个方法是使用 CreateCursor 获取热点点、一些数据数组,并从位中取一。
我想要介于两者之间的第三种方式:我有一个包含 .cur 文件数据的字节数组。我想用这些数据来制作光标。这可以做到吗?
I can find a couple ways to create a cursor in win32 but I need the third, the one I can't find.
The first I can find is to use LoadXXX() to open a file or resource and load the cursor that way.
The second I can find is to use CreateCursor to take the hot-spot points, some data arrays, and make one out of bits.
I want the third way that's between the two: I have an array of bytes that contains a .cur file data. I want to use that data to make a cursor. Can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如我在评论中更简短地发布的那样,我认为最简单、最明智的方法是将存储在数组中的二进制数据写入磁盘上的临时 .CUR 文件,然后使用
LoadCursorFromFile
函数来读取该 .CUR 文件。然后您可以删除临时文件。当简单的解决方案就可以解决问题时,就没有理由去寻找复杂的解决方案。话虽这么说,如果您确实需要一种方法来执行此操作,您可能会考虑执行类似于 .NET Framework 团队从内存流加载游标对象的操作。这利用了光标和图标之间的相似性。
使用
OleCreatePictureIndirect
函数创建一个新的未初始化IPicture
对象,然后通过IPersistStream::Load
方法。完成此操作后,只需使用CopyImage
函数 从加载的图像创建光标。As I more briefly posted in a comment, I think the easiest and most sensible way is to just write the binary data stored in the array out to a temporary .CUR file on disk, and then use the
LoadCursorFromFile
function to read that .CUR file. You can then remove the temporary file. There's no reason to go hunting for complicated solutions when simpler ones will do.That being said, if you really need a way to do this, you might consider doing something similar to what the .NET Framework team did to load a cursor object from a memory stream. This takes advantage of the similarity between cursors and icons.
Use the
OleCreatePictureIndirect
function to create a new uninitializedIPicture
object, which you then initialize from your array of bytes in memory via theIPersistStream::Load
method. Once you've done that, just use theCopyImage
function to create a cursor from the loaded image.CreateCursor() 的参数位于:http://msdn。 microsoft.com/en-us/library/ms648385(VS.85).aspx
.cur 文件格式在维基百科上有记录:http://en.wikipedia.org/wiki/ICO_(file_format)#Legacy_format
您可以从该图标文件中获取 CreateCursor() 的所有参数标头;我认为指向图像数据的指针可能有一个用于和平面的位图,后面紧跟着一个用于异或平面的位图。
Arguments to CreateCursor() are here: http://msdn.microsoft.com/en-us/library/ms648385(VS.85).aspx
The .cur file format is documented on Wikipedia here: http://en.wikipedia.org/wiki/ICO_(file_format)#Legacy_format
You can get all the arguments to CreateCursor() out of that icon file header; I think the pointer to the image data probably has a bitmap for the and plane directly followed by a bitmap for the xor plane.
在寻找这个问题的答案时,我偶然发现了 CreateIconIndirect,它“从 ICONINFO 结构创建图标或光标”,为光标图像指定位图句柄 (HBITMAP)(因此您可以使用 Win32 API 位图例程来准备光标):
https://learn.microsoft .com/en-us/windows/desktop/api/winuser/nf-winuser-createiconindirect
虽然它的返回类型是 HICON,但文档说它返回一个图标或光标。
ICONINFO 结构有一个布尔 fIcon 成员来确定它是图标(TRUE)还是光标(FALSE):
我还没有在我的程序中使用它并意识到这是一个非常老的问题,但希望它能指出任何人否则就是试图朝着正确的方向做到这一点。
Whilst looking for the answer to this, I stumbled across CreateIconIndirect, which "Creates an icon or cursor from an ICONINFO structure" that specifies a bitmap handle (HBITMAP) for the cursor image (so you can use the Win32 API bitmap routines to prepare the cursor):
https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-createiconindirect
Though its return type is HICON, the documentation says it returns an icon or cursor.
The ICONINFO structure has a boolean fIcon member to determine whether it is an icon (TRUE) or a cursor (FALSE):
I've not yet used this in my program and realise this is a really old question, but hopefully it will point anyone else that is trying to do this in the right direction.
做不到。最好不要浪费时间去尝试。我只是想避免将一堆 .cur 文件转换为明智的库支持的文件。我以为我会使用 QCursor 的 HCURSOR 构造函数,而且我可以,但是当我只能使用 PNG 文件或其他文件时,不值得费心尝试解决更糟糕的 API。
Can't be done. Best not to waste time trying. I was only trying to avoid having to convert a bunch of .cur files to something that sensible libraries support. Thought I'd use the HCURSOR constructor for QCursor, and I could, but it's just not worth the f'n bother trying to work around the worse imaginable API when I can just use PNG files or something.