# How to make a system tray program in Windows?

**URL:** <https://www.ruby-forum.com/t/how-to-make-a-system-tray-program-in-windows/48880>\
**Category:** Ruby\
**Created:** [November 26, 2005, 2:28am UTC](https://www.ruby-forum.com/t/how-to-make-a-system-tray-program-in-windows/48880 "2005-11-26T02:28:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sskim.box](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@sskim.box](https://www.ruby-forum.com/u/sskim.box)\
**Post date:** [November 26, 2005, 2:28am UTC](https://www.ruby-forum.com/t/how-to-make-a-system-tray-program-in-windows/48880/1 "2005-11-26T02:28:23Z")

</div>

How can I make my program appear in the system tray of the Windows?

What I want my program do is to keep track of time regularly (using  
timer) and do something behind the scene. (like a scheduler.)  
So, I want to make my program disappear from main desktop and appear in  
the  
system tray.

What can I do?

---

<div class="post-metadata">

**Author:** ![sskim.box](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@sskim.box](https://www.ruby-forum.com/u/sskim.box)\
**Post date:** [November 26, 2005, 3:08am UTC](https://www.ruby-forum.com/t/how-to-make-a-system-tray-program-in-windows/48880/2 "2005-11-26T03:08:40Z")

</div>

Sung Soo K. wrote:

> How can I make my program appear in the system tray of the Windows?
> 
> What I want my program do is to keep track of time regularly (using  
> timer) and do something behind the scene. (like a scheduler.)  
> So, I want to make my program disappear from main desktop and appear in the  
> system tray.
> 
> What can I do?

This puts a green tick in the SysTray, then changes it to a red minus  
(No Entry sign in UK), then removes it. Delays in between (sleep).

I was going to send a traffic light sequence but I’m not certain whether  
it’s using a generally available DLL. I think most will have  
INETCPL.CPL

You can use any DLL that has icon(s), probably .EXE also ?

Have fun,

daz

#-------------------------------------------------------------------------------

require ‘Win32API’

RT\_ICON = 3  
DIFFERENCE = 11  
RT\_GROUP\_ICON = RT\_ICON + DIFFERENCE

NIF\_MESSAGE = 1  
NIF\_ICON = 2  
NIF\_TIP = 4  
NIM\_ADD = 0  
NIM\_MODIFY = 1  
NIM\_DELETE = 2

ExtractIcon = Win32API.new(‘shell32’, ‘ExtractIcon’, ‘LPI’,  
‘L’)  
Shell\_NotifyIcon = Win32API.new(‘shell32’, ‘Shell\_NotifyIconA’, ‘LP’,  
‘I’)

hicoY = ExtractIcon.call(0, ‘C:\WINDOWS\SYSTEM\INETCPL.CPL’, 21) #  
Green tick  
hicoN = ExtractIcon.call(0, ‘C:\WINDOWS\SYSTEM\INETCPL.CPL’, 22) # Red  
minus

tiptxt = ‘test icon (ruby)’  
pnid = [6_4+64, 0, ‘ruby’.hash, NIF\_ICON | NIF\_TIP, 0,  
hicoY].pack(‘LLIIIL’) \<\<  
tiptxt \<\< “\0”_(64 - tiptxt.size)  
ret = Shell\_NotifyIcon.call(NIM\_ADD, pnid)  
p ‘Err: NIM\_ADD’ if ret == 0

```
  sleep(3) # <----<<

```

pnid = [6_4+64, 0, ‘ruby’.hash, NIF\_ICON | NIF\_TIP, 0,  
hicoN].pack(‘LLIIIL’) \<\<  
tiptxt \<\< “\0”_(64 - tiptxt.size)  
ret = Shell\_NotifyIcon.call(NIM\_MODIFY, pnid)  
p ‘Err: NIM\_MODIFY’ if ret == 0

```
  sleep(6) # <----<<

```

pnid = [6\*4+64, 0, ‘ruby’.hash, 0, 0, 0].pack(‘LLIIIL’) \<\< “\0”  
ret = Shell\_NotifyIcon.call(NIM\_DELETE, pnid)  
p ‘Err: NIM\_DELETE’ if ret == 0

#-------------------------------------------------------------------------------

---

<div class="post-metadata">

**Author:** ![sskim.box](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@sskim.box](https://www.ruby-forum.com/u/sskim.box)\
**Post date:** [November 26, 2005, 7:30am UTC](https://www.ruby-forum.com/t/how-to-make-a-system-tray-program-in-windows/48880/3 "2005-11-26T07:30:29Z")

</div>

On 11/25/05, Sung Soo K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How can I make my program appear in the system tray of the Windows?
> 
> What I want my program do is to keep track of time regularly (using  
> timer) and do something behind the scene. (like a scheduler.)  
> So, I want to make my program disappear from main desktop and appear in the  
> system tray.
> 
> What can I do?

VisualuRuby makes this pretty easy:

[http://www.osk.3web.ne.jp/~nyasu/vruby/vrproject-e.html](http://www.osk.3web.ne.jp/~nyasu/vruby/vrproject-e.html)

Here is the example script for the tray icon stuff:

[http://www.osk.3web.ne.jp/~nyasu/vruby/sample/traytest.rb](http://www.osk.3web.ne.jp/~nyasu/vruby/sample/traytest.rb)

Ryan

---

<div class="post-metadata">

**Author:** ![sskim.box](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@sskim.box](https://www.ruby-forum.com/u/sskim.box)\
**Post date:** [November 26, 2005, 1:12pm UTC](https://www.ruby-forum.com/t/how-to-make-a-system-tray-program-in-windows/48880/4 "2005-11-26T13:12:56Z")

</div>

Hi,

Very neat. I found the icons in C:\WINDOWS\SYSTEM32\INETCPL.CPL’  
though.

Do you have pointers to more info on this API? I am not really a  
windows programmer.

Thanks

Tom

---

<div class="post-metadata">

**Author:** ![sskim.box](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@sskim.box](https://www.ruby-forum.com/u/sskim.box)\
**Post date:** [November 26, 2005, 2:29pm UTC](https://www.ruby-forum.com/t/how-to-make-a-system-tray-program-in-windows/48880/5 "2005-11-26T14:29:10Z")

</div>

Tom A. wrote:

> Hi,
> 
> Very neat. I found the icons in C:\WINDOWS\SYSTEM32\INETCPL.CPL’ though.

Ah, thanks. That may apply to the majority, then.

> Do you have pointers to more info on this API? I am not really a  
> windows programmer.

On MSDN,

> **[qurl.net - This website is for sale! - qurl Resources and...](http://qurl.net/o2)**
>
> This website is for sale! qurl.net is your first and best source for all of the information you’re looking for. From general topics to more of what you would expect to find here, qurl.net has it all. We hope you find what you are searching for!

> **[qurl.net - This website is for sale! - qurl Resources and...](http://qurl.net/o3)**
>
> This website is for sale! qurl.net is your first and best source for all of the information you’re looking for. From general topics to more of what you would expect to find here, qurl.net has it all. We hope you find what you are searching for!

I support Ryan’s point about VRuby/SWin and I should have mentioned it.

The traytest.rb example he linked to should be more appropriate to the  
original poster than I thought.

My example would need a Windows message loop to do anything useful like  
minimizing a GUI or waking it up again by clicking on the tray icon.  
VRuby implements that and, of course, most Windows users will have it  
installed already if they used the RubyForge one-click installer.

Cheers,

daz
