Using Win32API to load an icon from .ico file

Discussion in 'Ruby' started by Tom Robinson, Jul 13, 2004.

  1. Tom Robinson

    Tom Robinson Guest

    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?

    --

    remove 'spamfree.' to respond
     
    Tom Robinson, Jul 13, 2004
    #1
    1. Advertising

  2. > 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
     
    Michael Geary, Jul 13, 2004
    #2
    1. Advertising

  3. Tom Robinson

    Tom Robinson Guest

    On Mon, 12 Jul 2004 22:13:51 -0700, "Michael Geary" <>
    wrote:

    >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!

    --

    remove 'spamfree.' to respond
     
    Tom Robinson, Jul 13, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miro Durcik

    app.rc resource's icon to .net Icon

    Miro Durcik, Oct 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,402
    Miro Durcik
    Oct 9, 2004
  2. Patrick Olurotimi Ige

    Having favicon.ico instead of IE.ico on a website

    Patrick Olurotimi Ige, Dec 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,511
    Martin Dechev
    Dec 9, 2004
  3. Mel
    Replies:
    1
    Views:
    399
    Steve Pugh
    Mar 9, 2005
  4. Dice
    Replies:
    0
    Views:
    96
  5. Dice
    Replies:
    0
    Views:
    79
Loading...

Share This Page