Using Win32API to load an icon from .ico file

T

Tom Robinson

Has anyone successfully managed to do this? I'm trying to write an
app using vruby which adds icons to the systray. VRuby comes with an
example of how to do this, which works, except it does not say how to
load the icons you want from .ico files. Instead it just uses icons
out of the user32.dll I think. This is the code it uses to load the
icons:

LoadIcon = Win32API.new("user32","LoadIcon","II","I")
QUESTIONICON= LoadIcon.call(0,32514)

That works fine, and QUESTIONICON can then be used to call
create_trayicon, but I'm trying to do this:

LoadIconA = Win32API.new("user32","LoadIcon",['I','P'],"I")
FOOICON= LoadIconA.call(0,"myicon.ico")

I think if you give a string as a second argument to the LoadIcon call
in the user32.dll it'll try and load from a file, but I'm not 100%
sure. The docs from MS say that LoadIcon is deprecated and you should
use LoadImage but that's even more complicated from what I can tell.
I've found a few examples on the web of people who've used LoadIcon to
load from .ico files (I think), but none of them are in ruby. I'm not
sure if I'm specifying the parameter types correctly using ['I','P']
for an integer and a string either. Anyway, it loads a blank icon at
the moment so it isn't working.

Anyone managed to accomplish this before?
 
M

Michael Geary

LoadIconA = Win32API.new("user32","LoadIcon",['I','P'],"I")
FOOICON= LoadIconA.call(0,"myicon.ico")

I think if you give a string as a second argument to the LoadIcon
call in the user32.dll it'll try and load from a file, but I'm not
100% sure.

The docs from MS say that LoadIcon is deprecated and you should
use LoadImage but that's even more complicated from what I can tell.
I've found a few examples on the web of people who've used LoadIcon to
load from .ico files (I think), but none of them are in ruby. I'm not
sure if I'm specifying the parameter types correctly using ['I','P']
for an integer and a string either. Anyway, it loads a blank icon at
the moment so it isn't working.

LoadIcon is for loading icons from executable or resource files such as
DLLs. The second argument is the resource ID for the icon, not a filename.
The resource ID can be either a string or an integer less than 65536,
depending on how the icon resource was defined when the DLL was built. The
first argument is an HINSTANCE for the DLL, which you obtain by calling
LoadLibrary or LoadLibraryEx.

To load an icon from a .ico file, use either ExtractIcon or LoadImage.
ExtractIcon is simpler, but it only looks for one size of icon, the "system
large" size, typically 32x32 or 48x48. If you want to be able to control
which size icon you get, use LoadImage instead.

I don't have Ruby code handy to do this, but if you Google for the function
names above you'll get the MSDN documentation for them.

-Mike
 
T

Tom Robinson

To load an icon from a .ico file, use either ExtractIcon or LoadImage.
ExtractIcon is simpler, but it only looks for one size of icon, the "system
large" size, typically 32x32 or 48x48. If you want to be able to control
which size icon you get, use LoadImage instead.

I don't have Ruby code handy to do this, but if you Google for the function
names above you'll get the MSDN documentation for them.

Thanks, ExtractIcon was what I needed! Here's the code that works:

ExtractIcon = Win32API.new("shell32","ExtractIcon",['I','P','I'],"I")
FOOICON = ExtractIcon.call(0,"myicon.ico",0)

And then this FOOICON can be used in the create_trayicon function.

Fantastic, I thought it was going to be a lot trickier for some
reason. Cheers!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top