[Gtk-sharp-list] Gtk Tray-menu not disappearing on Windows

Matthias D. matthi.d at googlemail.com
Sat Jun 30 09:59:30 UTC 2012


I just wanted to create a really simple platform independent program with a
tray-menu and used the code in
On windows this will create a tray menu which will just not disappear,
which is most likely a bug.

After some digging around i found the following work-around (in F#):
--------------- GtkUtils.fs

module GtkUtils
 open Systemopen System.Runtime.InteropServices
 module Native =

    [<DllImport ("user32.dll", SetLastError = true)>]
    extern System.UInt32 GetWindowThreadProcessId ( System.IntPtr
hWnd, int* lpdwProcessId)

    extern IntPtr GetForegroundWindow ();

    [<DllImport ("user32.dll")>]
    extern [<MarshalAs (UnmanagedType.Bool)>] bool SetForegroundWindow
(IntPtr hWnd);

    let bringToForeground() =
            let current_proc = System.Diagnostics.Process.GetCurrentProcess()
            let current_proc_id = current_proc.Id
            let mutable set_foreground_window = true
            let mutable window_handle = GetForegroundWindow ()

            if (window_handle <> IntPtr.Zero) then
                set_foreground_window <- false

                let mutable window_handle_proc_id = 0
                GetWindowThreadProcessId (window_handle,
&&window_handle_proc_id) |> ignore

                if (window_handle_proc_id <> current_proc_id) then
                    set_foreground_window <- true;

            if (set_foreground_window) then
                window_handle <- current_proc.MainWindowHandle
                if (window_handle <> IntPtr.Zero) then
                    SetForegroundWindow (window_handle) |> ignore
            | exn ->
                System.Console.WriteLine("Error pulling Tomboy to
foreground: {0}", exn);

---------------- Program.fs

open Gtkopen System

Application.Init()let icon = StatusIcon.NewFromStock(Stock.Info)
icon.Tooltip <- "Notification Icon"
 let menu = new Menu() let quitItem = new ImageMenuItem("Quit")

quitItem.Image <- new Image(Stock.Quit, IconSize.Menu)

    |> Event.add (fun args ->
            menu.Popup(null, null, new MenuPositionFunc(fun menu x y
push_in -> StatusIcon.PositionMenu(menu, ref x, ref y, ref push_in,
icon.Handle)), 0u, Global.CurrentEventTime);

    |> Event.add (fun args ->


I would suggest to either update the linked post or fix the bug
directly. (This bug is quite frustrating on windows ;) )
 This post should be read more as a bug-report.
The workaround is not perfekt as the Menu will not behave exaclty as
native menus.

For example if the Tray-Menu is over the tray "extension" and you
click somewhere else the tray extension will not disappear as with
other tray menus.
However it is good enough and used by other gtk-programs. (It makes
the tray menu at least usable)
(With Tray extension i mean -> windows 7 -> click on the small
triangle in the tray icon)
Have a nice day,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20120630/3f6db891/attachment.html>

More information about the Gtk-sharp-list mailing list