LupoX has asked for the wisdom of the Perl Monks concerning the following question:

Hello Ladies and Gentlemen,

My first steps in Win32:GUI programming have ended here:
my small script shoult display a task bar icon. When I click a mousebutton over the icon, my POP3 account should be checked. The icon should change when new mails are on the server.
Now my problem is that after destroying one icon, i create a new with "AddNotifyIcon...".
The "-id" parameter has to be different from the first icon`s id .
So when I click on the new button (when I have mails) the old button should come up. Now using "AddNotifyIcon..." the id has again to be a new one.
I could solve the problem in generating new ids in a loop but this hurts in my eyes when reading the code.

So how do I make it more elegant?

here is the code i wrote so far:
#!/usr/bin/perl -w #use strict; use Win32::GUI; use Mail::POP3Client; $main_window = new Win32::GUI::Window( -minsize => [0, 0], -maxsize => [0, 0], -minwidth => 0, -minheight => 0, -maxwidth => 0, -maxheight => 0 ); $tray_icon_file_mail_no = new Win32::GUI::Icon("roll.ico") || die "una +ble to open icon file: $!\n"; $tray_icon_file_mail_yes = new Win32::GUI::Icon("popme.ico") || die "u +nable to open icon file: $!\n"; main(); ######################## sub Check_Mail { my $pop = new Mail::POP3Client( USER => "user", PASSWORD => "sn1ffm3", HOST => "my.pop.host", AUTH_MODE => 'PASS', DEBUG => 0 ); open (TEMP, ">mail.tmp") or die "Cannot open temp file mail.tmp: $!\n" +; my $b = $pop->Count(); for (my $i = 1; $i <= $b; $i++) { foreach ( $pop->Head( $i ) ) { /^From:\s+/i and print $_, "\n" and print TEMP "$_\n"; /^Subject:\s+/i and print $_, "\n" and print TEMP "$_\n"."***\n"; } } close TEMP; $pop->Close(); } ####################### sub Yes_Mail { ##Win32::GUI::NotifyIcon::Delete($main_window, $main_window->TrayIconM +ailNo->{-id}); ##Win32::GUI::DestroyIcon($tray_icon_file_mail_no); $main_window->{'TrayIconMailNo'}->DESTROY(); $main_window->AddNotifyIcon ( -icon => $tray_icon_file_mail_yes, -id => "1", -name => "TrayIconMailYes", -tip => "LupoX POP3 Notifier(TM) - YOU HAVE MAIL" ); sub TrayIconMailYes_Click() { #Win32::GUI::NotifyIcon::Delete($main_window, $main_window->TrayIconMa +ilYes->{-id}); #Win32::GUI::DestroyIcon($tray_icon_file_mail_yes); $main_window->{'TrayIconMailYes'}->DESTROY(); main(); print "left_clicked - and mail checked.\n"; } sub TrayIconMailYes_RightClick() { $main_window->{'TrayIconMailYes'}->DESTROY(); print "right_clicked - mail yes.\n"; return(0); } Win32::GUI::Dialog(); } sub main { $main_window->AddNotifyIcon ( -icon => $tray_icon_file_mail_no, -id => "0", -name => "TrayIconMailNo", -tip => "LupoX POP3 Notifier(TM)" ); sub TrayIconMailNo_Click() { Check_Mail(); print "right_clicked - and mail checked.\n"; open (TEMP2, "<mail.tmp") or die "Cannot open mail.tmp: $!\n"; my @mailinfo = <TEMP2>; close TEMP2; my $nr = @mailinfo; if ($nr != 0) { Yes_Mail(); } } sub TrayIconMailNo_RightClick() { Check_Mail(); print "right_clicked - and mail checked.\n"; open (TEMP2, "<mail.tmp") or die "Cannot open mail.tmp: $!\n"; my @mailinfo = <TEMP2>; close TEMP2; my $nr = @mailinfo; if ($nr != 0) { Yes_Mail(); } } sub TrayIconMailNo_MouseEvent($) { my $msg = shift; #print "mouse_event: $msg\n"; # 512 mouse over icon if ($msg == 512) { print "mouse over icon\n"; } elsif ($msg == 513) { print "left button clicked\n"; } elsif ($msg == 514) { print "left button released\n"; } elsif ($msg == 515) { print "left button doubleclicked\n"; } elsif ($msg == 516) { print "right button clicked\n"; } elsif ($msg == 517) { print "right button released\n"; } elsif ($msg == 518) { print "right button doubleclicked\n"; } elsif ($msg == 519) { print "middle button clicked\n"; } elsif ($msg == 520) { print "middle button released\n"; } elsif ($msg == 521) { print "middle button doubleclicked\n"; } else { print "unknown event: $msg\n"; } # 513 left click # 514 left released # 515 left double click # 516 right click # 517 right released # 518 right double click # 519 middle click # 520 middle released # 521 middle double click } Win32::GUI::Dialog(); }

Thank you for any critics.
Sorry to all real perl masters if my question is boring for you....

Have a good time...
Georg
perlmonks@viot.de

Replies are listed 'Best First'.
Re: Win32::GUI Task Bar Icon Bug or bad programming?
by chromatic (Archbishop) on Oct 18, 2001 at 04:49 UTC
    You only need a new id when creating a new icon? You don't need to keep track of it? Seems like a good case to enclose a lexical:
    my $id = 1; sub Yes_Mail { $main_window->TrayIconMailNo->{-id}); $main_window->{'TrayIconMailNo'}->DESTROY(); $main_window->AddNotifyIcon ( -icon => $tray_icon_file_mail_yes, -id => $id++, -name => "TrayIconMailYes", -tip => "LupoX POP3 Notifier(TM) - YOU HAVE MAIL" );
    I'm not a Win32 programmer by any means, but that's how I understand your requirements.

    A couple of other things could be stronger. Build a hash of event numbers and handlers instead of a large if/else switch:

    my %events = ( 512 => sub { print "mouse over icon\n" }, 513 => sub { print "left button clicked\n"; }, # et cetera ); if (exists $events{$msg}) { $events{$msg}->(); }

    You can also avoid reading all of the lines in a file to see if there are new messages:

    open (NEW, 'mail.tmp') or die "Cannot open mail.tmp: $!\n"; if (defined(my $line = <NEW>)) { Yes_Mail(); } close NEW;
Re: Win32::GUI Task Bar Icon Bug or bad programming?
by MZSanford (Curate) on Oct 18, 2001 at 13:05 UTC
    I just finished gappling with the change-of-systray-icon problem, and here is a short version of what i did :
    use Win32::GUI; use strict; my $okIcon = new Win32::GUI::Icon('C:\Monolith.ico'); my $badIcon = new Win32::GUI::Icon('C:\Skull & Crossbones.ico'); my $main = Win32::GUI::Window->new(-name => "Main",-size => [250,350]) +; $main->AddButton(-pos => [10,10],-name => "Bar",-text => "Bad Icon"); $main->AddButton(-pos => [10,35],-name => "Bar2",-text => "Good Icon") +; $main->AddNotifyIcon( -name => "Tray", -tip => "Tray", -icon => $okIcon, -id => 222, ); $main->Show(); Win32::GUI::Dialog(); sub Bar_Click { Win32::GUI::NotifyIcon::Modify( $main, -id => 222, -icon => $badIcon, -tip => "Bad", ); } sub Bar2_Click { Win32::GUI::NotifyIcon::Modify( $main, -id => 222, -icon => $okIcon, -tip => "OK", ); } sub Main_Terminate { $main->AddNotifyIcon( -name => "Tray", -tip => '', -icon => '', -id => 222, ); -1; } sub Main_Minimize { $main->Disable(); $main->Hide(); 1; } sub Tray_Click { $main->Enable(); $main->Show(); 1; }

    i had a memory leak once, and it ruined my favorite shirt.