Windows Cursor from Memory

BlitzMax Forums/BlitzMax Programming/Windows Cursor from Memory

Taron(Posted 2016) [#1]
OMG, why must I always run a freaking gauntlet to get things done? Amazing. O_o

I'm writing a little fluid-dynamics based painter called "Verve".

Anyhow, I need to replace the windows cursor, because drawing an image can't display the cursor fast enough to be pleasant.

I already know how to load it from a file, but I cannot use BMax's incbin to embed the file into my exe. I could probably try to figure out how to .rc images, but I really want to create new cursor images on the fly to be more flexible.

All I would need to know is how to load a cursor image from memory or what ever it takes to set the cursor image to be from some place in memory.

Alternatively, if there was some way to let windows draw custom graphics in place of the cursor, that would be the most amazing solution. Ordinary cursors have a maximum size of 256x256, which won't cover brush sizes beyond that, of course.

Any help would be greatly appreciated! Thanks in advance!


BlitzSupport(Posted 2016) [#2]

if there was some way to let windows draw custom graphics in place of the cursor, that would be the most amazing solution



Not sure if it fits with what you're doing, but I managed to get a custom cursor to load in place of the window's own cursor. Bit 'wordy', but seems to work, although the Win32 API call won't work with Incbin, unfortunately.



I have managed to do a crude .ico loader, too, which does work with Incbin:



... but note that it will likely only work with .png-compressed icons (export an image as .ico in GIMP and select PNG compression), and it only uses the first icon image found in the file.

Can't immediately see a way to merge these into a super-duper Incbin-icon-replacer, but these might serve as a couple of pointers, at least.

"Pointers", ha ha...


Taron(Posted 2016) [#3]
Ah, cool, THANKS, James! I will give it a try!!!
(crossing fingers!)

Also, I'm not using MaxGui, by the way, hoping that it won't break all hope right away. As I've said up there, I have managed to replace the cursor, but only with a file (cursor.cur). I even researched the cur/ico format and can handle it. Ideally I want to create such an image in memory and point windows to there, you know.

Anyway, I'll give it a try and I'm super thrilled you responded already!


BlitzSupport(Posted 2016) [#4]
Another possible option, though would only work if all cursors were pre-defined (not created on-the-fly) and a bit fiddly...

Add all of the necessary icons to the built executable using ResHack or similar then load them using the Win32 ExtractIcon function -- the index parameter of ExtractIcon starts at 0 for the first icon (as in this example), up to "however many icons you've added"...



IMPORTANT: You have to build the exe first, add an icon or icons using ResHack, then when you run the executable directly (not from IDE), it will be able to load the built-in icons...

Building again from the IDE would wipe out all of the icons you added, so it would be best to load the icons from file while developing, then add the icons to the final release executable. A simple 'Const' to select two codepaths ought to make this reasonably workable.

There's also Win32's CreateCursor, which should allow my first example to work, with LoadCursorFromFile replaced by the result of CreateCursor... this is probably your best option if you can figure out the data it needs from your images.

Oh, and if not using MaxGUI, you should just have to replace QueryGadget (window, QUERY_HWND) with a handle to the application window.

Final option would be save the Incbin'd icons to a temp location, again using LoadCursorFromFile, though I imagine this isn't what you want!


Taron(Posted 2016) [#5]
Hehehehehe, that last idea I really thought about already (blush), what a heck that would be, hihihihi, funny, but "ey".
No, no, this would be a last resort. First I'll explore everything else. I hope, I can pull myself together for the resource stuff, which always felt like a tiny bit of voodoo magic to me, but that was many years ago.

Anyhow, you totally rock, James, thank you!
If I'll manage to make a pretty piece of code, I'll gladly share it here, too. This stuff can be so painful and I'd love to know I could spare folks some of it. 8P

...but then, it's really not the usual stuff people need. Who knows...


BlitzSupport(Posted 2016) [#6]
No problem... Resource Hacker is easy to use, by the way:

1) Open executable to be edited;
2) Go to Action menu -> "Add Single Binary or Image Resource";
3) Click "Select File" and choose an icon;
4) Click "Add Resource";
5) Save to update the executable.

The first icon in the .exe will be used as the executable icon, then you can add any others that you need for your application, passing their indices to ExtractIcon.