# Loading icons on windows with icon index

**URL:** <https://www.ruby-forum.com/t/loading-icons-on-windows-with-icon-index/187654>\
**Category:** wxRuby\
**Created:** [April 27, 2010, 1:38pm UTC](https://www.ruby-forum.com/t/loading-icons-on-windows-with-icon-index/187654 "2010-04-27T13:38:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 27, 2010, 1:38pm UTC](https://www.ruby-forum.com/t/loading-icons-on-windows-with-icon-index/187654/1 "2010-04-27T13:38:07Z")

</div>

How do you load icons on windows in wxRuby? I know in wxWidgets you use  
wxIconLocation, but as far as I can see, it is not included in wxRuby.  
Any ideas?

Thanks.

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 28, 2010, 5:44am UTC](https://www.ruby-forum.com/t/loading-icons-on-windows-with-icon-index/187654/2 "2010-04-28T05:44:04Z")

</div>

Do you mean to load icon to top-left of a window?  
every window should have set\_icon method to do that

Regards  
Hendra

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 28, 2010, 11:37am UTC](https://www.ruby-forum.com/t/loading-icons-on-windows-with-icon-index/187654/3 "2010-04-28T11:37:38Z")

</div>

On 27/04/2010 12:38, John B. wrote:

> How do you load icons on windows in wxRuby? I know in wxWidgets you use  
> wxIconLocation, but as far as I can see, it is not included in wxRuby.  
> Any ideas?

You can load an external icon from a file simple with  
Wx::Icon.new(filepath,…)

If you have a Windows icon file containing multiple icons, you should be  
able to use Wx::IconBundle.new(filepath,…), then the method  
get\_icon(i) to access individual icons by index.

hth  
alex

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 28, 2010, 1:46pm UTC](https://www.ruby-forum.com/t/loading-icons-on-windows-with-icon-index/187654/4 "2010-04-28T13:46:17Z")

</div>

Will that load all the icons from the file? It would be better to be  
able to just load one. I will be loading the icons from various system  
dlls so it wouldn’t make sense to load all of them.

Alex F. wrote:

> On 27/04/2010 12:38, John B. wrote:
> 
> > How do you load icons on windows in wxRuby? I know in wxWidgets you use  
> > wxIconLocation, but as far as I can see, it is not included in wxRuby.  
> > Any ideas?
> 
> You can load an external icon from a file simple with  
> Wx::Icon.new(filepath,…)
> 
> If you have a Windows icon file containing multiple icons, you should be  
> able to use Wx::IconBundle.new(filepath,…), then the method  
> get\_icon(i) to access individual icons by index.
> 
> hth  
> alex

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 29, 2010, 12:12am UTC](https://www.ruby-forum.com/t/loading-icons-on-windows-with-icon-index/187654/5 "2010-04-29T00:12:54Z")

</div>

If you wish to load just one Icon, create a method like this:

def load\_windows\_icon(dll\_file,icon\_number)  
ib = Wx::IconBundle.new(dll\_file)  
ib.get\_icon(icon\_number)  
end

This will return a Wx::Icon, and the ib will be put on the garbage heap  
for  
release at the next run of the Garbage Collector. There is no slowing  
down  
between loading a single icon, and loading multiple Icons through  
IconBundle, as wxWidgets will utilize Win32API first, and foremost, for  
most  
GUI Operations. In the case of IconBundles, it will read the Icon  
Header,  
be it .ico, .dll, or .exe, and it will parse the most important  
information  
needed to create the IconBundle class. The actual extraction, and  
creation  
of the Icon is deferred to when you use the get\_icon method on the  
IconBundle class. An Icon Resource could have a hundred icons in it,  
and it  
will not slow down the reading of the data.

hth,

Mario

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 29, 2010, 5:54pm UTC](https://www.ruby-forum.com/t/loading-icons-on-windows-with-icon-index/187654/6 "2010-04-29T17:54:47Z")

</div>

Alex F. wrote:

> If you have a Windows icon file containing multiple icons, you should be  
> able to use Wx::IconBundle.new(filepath,…), then the method  
> get\_icon(i) to access individual icons by index.

I apologize for asking here what I could discover on my own, but I get  
impatient about some things and I won’t have access to a Mac till the  
weekend. 🙂

If I have a Windows icon file with multiple icons, will the Mac use  
those icons too?

Alternatively, if I were to have a Mac icns file, does it work the same  
way you describe when on the Mac (instead of Windows)? And will Windows  
be able to access the individual icons in that file or are they  
inaccessible to it?

From the suggestion I’ve seen more than once to use .png for icons to  
simplify different platform use, I’m guessing these questions will be  
answered in the negative. Is the preferred method to use Wx::SetIcons  
with various sized png’s if you don’t know on what platform your app  
will be used?

Thanks!

Roger S

rsperberg at gmail

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 29, 2010, 7:57pm UTC](https://www.ruby-forum.com/t/loading-icons-on-windows-with-icon-index/187654/7 "2010-04-29T19:57:12Z")

</div>

OK, thanks. That’s good to know.

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 29, 2010, 11:13pm UTC](https://www.ruby-forum.com/t/loading-icons-on-windows-with-icon-index/187654/8 "2010-04-29T23:13:51Z")

</div>

Hello Roger,

On Thu, Apr 29, 2010 at 11:54 AM, Roger S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> I apologize for asking here what I could discover on my own, but I get  
> impatient about some things and I won’t have access to a Mac till the  
> weekend. 🙂

Not a problem, we get quite a few questions on here, that people can’t  
find  
in documentation.

> If I have a Windows icon file with multiple icons, will the Mac use  
> those icons too?

I believe that wxWidgets needs a library in order to load Widows Icon  
files  
on any other system other then Windows itself.

> Alternatively, if I were to have a Mac icns file, does it work the same  
> way you describe when on the Mac (instead of Windows)? And will Windows  
> be able to access the individual icons in that file or are they  
> inaccessible to it?

Mac OS X uses png’s, like Linux does. If you ever look in a .app bundle  
folder, you will often find icons are in png.

> > From the suggestion I’ve seen more than once to use .png for icons to  
> > simplify different platform use, I’m guessing these questions will be  
> > answered in the negative. Is the preferred method to use Wx::SetIcons  
> > with various sized png’s if you don’t know on what platform your app  
> > will be used?

It is best to use PNG’s for icons, simply for the fact that wxWidgets  
can  
load PNG’s correctly on all 3 platforms. And you will find that if you  
load  
with Wx::Bitmap, or Wx::Image, you can convert it to an Icon, through  
Wx::Icon, which will allow the icon to be created in native format, to  
be  
used on the platform in question. And infact, any PNG, GIF, JPEG, etc,  
etc,  
can be converted to a Icon, and used where ever Wx::Icon is required.

Thanks!
