views:

102

answers:

3

I am using a NotifyIcon from Win Forms to make a systray icon for my WPF C# application.

I have a bug where if the user right clicks the icon for the context menu, they can press Alt-F4 and the icon will disappear from the tray, but the Main WPF application is still running. This is especially a problem when they have "minimized to systray" and the only control of the application is now gone.

Anyone know how to handle this specificially on the systray? I've looked at the NotifyIcon documentation and there isn't anything relating to keypress events.

UPDATE: here's a sample application to show how I'm using the systray and the actual bug. http://cid-e75a75f1a1fbfbb5.office.live.com/self.aspx/.Public/WpfApplication1.zip?sa=221089565

A: 
Femaref
sorry, this doesn't work. The main window gets no events when I press keys after opening the context menu of my systrayicon.
Jippers
A: 

Well, I gotta say that was a tricky one, but after some quick testing, I think this will do.

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Activated += new EventHandler(Form1_Activated);
    }

    void Form1_Activated(object sender, EventArgs e)
    {
        string iconPath = "some file system path";
        notifyIcon1.Icon = new Icon(iconPath);
    }

It won't make it so thay you can't make the icon dissappear, but at least when your application get focus, the icon will retrun.

You might also try storing the icon so that you do not need to keep constructing it.

Seattle Leonard
this might work as a work around, except in the instance of my bug where if the user does the "Minimize to systray" and then closes the systrayicon. There's no way to bring the mainwindow back for the restore icon to work.
Jippers
you're right. However, you could minimize this a bit further by adding the restore icon logic in the "minimize to Sys Tray" event.Further, you could add a timer when the application is minimized and ,every 30 sec or so, just go ahead and reset the icon. This will consume more resources, but it will get the job done.
Seattle Leonard
Other than that, about the only thing you can do is to put into your documentation that if someone actually does run into this edge case, that they must launch the task manager and click "switch to".
Seattle Leonard
There may be some whay to tap into the Windows API to get the results you want.
Seattle Leonard
Switch To doesn't work because when minimized to systray it's not showing up in the applications list. I'm thinking windows API might be my only solution but for now it's a me sandwich with rock and hardplace bread.
Jippers
A: 

So this is a bug and I've reported it to Microsoft Connect. Check here for updates I suppose: https://connect.microsoft.com/VisualStudio/feedback/details/568590/unable-to-disable-alt-f4-on-winforms-notifyicon-when-context-menu-is-open?wa=wsignin1.0

Jippers