in reply to How do you toggle the system tray icon using Win32::Gui?

Ok, this is probably exploiting the very bug that we've been discussing here, but this code seems to work. You need to use the "id" parameter. It seems that if you do AddNotifyIcon() with the same "id" that you used before, it clears the old icon. So you can "effectively" change the icon by having the old one zapped into a black hole (I don't understand where it goes!).

I'm using info1.ico and info2.ico from The zipfile here, which gives me a yellow and blue "i" - nice!
# Test Icons from http://www.royaltyfreeart.com/favicons.html use Win32::GUI; use strict; my $okIcon = new Win32::GUI::Icon("info1.ico"); my $badIcon = new Win32::GUI::Icon("info2.ico"); my $lastState = 1; # start off ok my $iconCounter = 1; # maintain an icon count my $main = Win32::GUI::Window->new(); DoIcon($okIcon, $iconCounter); # set the initial icon while(1){ if (int rand 2) # Yeah, This is how I do system monitoring! { unless ($lastState) { # Everything's ok now - it wasn't last time round changeIcon($okIcon); $lastState = 1; } } else { if ($lastState) { # Something's gone down - everything was ok last check changeIcon($badIcon); $lastState = 0; } } sleep(5); } sub DoIcon { my ($icon, $id) = @_; $main->AddNotifyIcon( -name => "Tray", -tip => $id, -icon => $icon, -id => $id, ); } sub changeIcon { my $icon = shift; DoIcon($icon, $iconCounter); # Clear the old Icon DoIcon($icon, ++$iconCounter); # Set the new one and incriment coun +ter }
Let me know if it works - it might just be a funny bug on my machine. FYI. Win32::GUI version is 0.0.434, I'm running ActivePerl build 629 on Win2k.

Simon Flack ($code or die)
$,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
=~y'_"' ';eval"die";print $_,lc substr$@,0,3;

Replies are listed 'Best First'.
Re: Re: How do you toggle the system tray icon using Win32::Gui?
by jplindstrom (Monsignor) on Oct 02, 2001 at 05:32 UTC