开发者

Create a cursor from .cur format bytes?

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 betwee开发者_运维技巧n 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?


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 uninitialized IPicture object, which you then initialize from your array of bytes in memory via the IPersistStream::Load method. Once you've done that, just use the CopyImage function to create a cursor from the loaded image.


Arguments to CreateCursor() are here: http://msdn.microsoft.com/en-us/library/ms648385(VS.85).aspx

HCURSOR WINAPI CreateCursor(
  __in_opt  HINSTANCE hInst,
  __in      int xHotSpot,
  __in      int yHotSpot,
  __in      int nWidth,
  __in      int nHeight,
  __in      const VOID *pvANDPlane,
  __in      const VOID *pvXORPlane
);

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.


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

HICON CreateIconIndirect(
    PICONINFO piconinfo
);

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

typedef struct _ICONINFO {
    BOOL    fIcon;
    DWORD   xHotspot;
    DWORD   yHotspot;
    HBITMAP hbmMask;
    HBITMAP hbmColor;
} ICONINFO;

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜