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

Greetings honoured friars,

I have a program that creates an icon in the system tray of Windows 7 and displays a tool-top when the mouse is hovered over it.

The issue comes when the user right-clicks the icon, it should display a menu but instead I get a thin sliver of a window, like a menu but with no text (the word "File" in this case). Something is there as if I click where "File" should be I get a sub-menu, no problem, I just cannot see the word "File".

I have worked through the articles elsewhere on Perlmonks but to no avail, even the "traymon.pl" code has this same problem, at least on my machine anyway.

The following code shows this exact problem, feel free to change the icon path to any .ICO file you have spare, or leave it blank and right-click in the blank space that will appear in your system tray.

# A simple Perl program to show the fly-out menu problem. # Uses and requires use strict; use Win32::GUI(); my $win_main; # Pointer to the main window. my $menu_popup; # Pointer to the menu that should appear. my $icn; # Ptr to an icon. my $debug = 1; # Debug flag # Windows event handlers go here. sub Main_Terminate { $win_main->NI->Remove(); -1; } sub NI_RightClick() { print "NI_RightClick event called.\n" if ($debug); $win_main->TrackPopupMenu($menu_popup, Win32::GUI::GetCursorPo +s()); } # Main program start # First, build everything # Create the context menu to be displayed when the icon is right-click +ed # Translate the menu $menu_popup = Win32::GUI::Menu->new( "&File" => "File", ">E&xit" => { -name => "File_Exit", -onClick => sub{-1} + }, ); # Create the main window - this will not be displayed. $win_main = Win32::GUI::Window->new( -name => 'Main', -menu => $menu_popup, -width => 1, -height => 1, -text => '', ); # Create an icon that will appear in the system tray. Pick any icon yo +u want. $icn = new Win32::GUI::Icon("c:/Windows/System32/PerfCenterCpl.ico"); # Add the icon to the window. $win_main->AddNotifyIcon( -name => 'NI', -icon => $icn, -tip => 'Thi is a notify fly-out/tool-tip thing.', -balloon => 0, ); # Enter the Windows message loop Win32::GUI::Dialog();
I would appreciate your thoughts on this, or even a simple test to see if this code works on your system (in which case it is environmental, but I still have no clue as to what it might be!)

|\/|artin

Replies are listed 'Best First'.
Re: Win32::GUI won't display an NI menu properly
by ww (Archbishop) on Aug 03, 2015 at 14:04 UTC

    Using warnings might help understand at least one problem:

    Use of uninitialized value in subroutine entry at C:/Perl/site/lib/Win32/GUI.pm line 3419 during global destruction.

    Chasing that backwards, '">E&xit"' looks a tad off.

    Even together, these are not the whole ball of wax, but they're a start. You might wish to sprinkle in some additional debug messages ... and re-read the doc (  perldoc -f Win32::GUI ).

    And, yes, I see the same problems with Win7, AS Perl 5.018 but don't have time to chase them all to completion.

      Hmm, thought I had replied to your note but it's not appeared.

      So I added "warnings;" in to the code but it revealed nothing on my system, no complaints or anything, which is of concern since you indicated that you had issues, but I tried again anyway and got the same problem.

      I took the code for creating the menu from win32-gui-demos.pl on CPAN and just pared it down. This menu is designed to be used as a standard Windows menu but as far as I can tell pop-up menus are created in the same way.

      I should have said that I am running ActiveState Perl v5.16.3.

Re: Win32::GUI won't display an NI menu properly
by wardmw (Acolyte) on Aug 03, 2015 at 17:53 UTC
    I have found a solution to my problem although the original issue still exists.

    I originally created my menu to look like "File" and under that "Exit" like most other Windows applications. This creates two entries in $menu_popup, "File" and "File_Exit".

    In my code above I was trying to display the menu starting from and including the "File" option, however this was not working.

    I changed my RightClick code to be:

    sub NI_RightClick() { print "NI_RightClick event called.\n" if ($debug); $win_main->TrackPopupMenu($menu_popup->{File}, Win32::GUI::GetCurs +orPos()); }
    and it now works inasmuch as when I right-click the notification icon it display a menu with "Exit" in it. I also found that I could expand the menu by adding lines similar to the ">E&xit" one, so the code now displays the menu list that would appear under the "File" menu button.

    So, my problem is resolved but the issue still exists. It may be that this is the way it's supposed to work and you cannot pass a Win32::GUI::Menu item to the TrackPopupMenu() function but other posts and examples suggest that this is exactly what you should pass to this function.

    Ah well, it's working for now, thanks for looking, providing me with help on formatting and your suggestions for the solution. |\/|artin

Re: Win32::GUI won't display an NI menu properly
by anonymized user 468275 (Curate) on Aug 03, 2015 at 15:30 UTC
    I've never used Win32::, but having metaCPANned this, I am surprised you are not passing $menu as an event or 'on' parameter to the AddNotifyIcon function instead of the main window, although my surprise could mean nothing of course, under the circumstances.

    One world, one people

      I was passing $menu_popup in to the main window to link it in as it seemed to make sense there however I tried adding it to the AddNotifyIcon instead but I get the same problem, it's the displaying of the menu that is the issue.

      I can track the right-click, the issue seems to be in the TrackPopupMenu call.