13

I'm very new to programming C#, though I've scripted C# in unity3D for a few years. I'm currently trying to make a WPF tray icon, all the sources I've found on the net tell me to use

System.Windows.Forms

However .Forms is not available in System.Windows for me, and I have no idea why not. Can anyone help me with this?

Logan
  • 425
  • 1
  • 6
  • 16

2 Answers2

35

You need to add references to the System.Window.Forms and System.Drawing assemblies and then you use it like this. Suppose you try to minimize the Window to tray icon and show it again when user click that icon:

public partial class Window : System.Windows.Window
{

    public Window()
    {
        InitializeComponent();

        System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
        ni.Icon = new System.Drawing.Icon("Main.ico");
        ni.Visible = true;
        ni.DoubleClick += 
            delegate(object sender, EventArgs args)
            {
                this.Show();
                this.WindowState = WindowState.Normal;
            };
    }

    protected override void OnStateChanged(EventArgs e)
    {
        if (WindowState == WindowState.Minimized)
            this.Hide();

        base.OnStateChanged(e);
    }
}
Erre Efe
  • 15,387
  • 10
  • 45
  • 77
  • 1
    Excellent the word reference helped me to find out how and where. And I now have a tray icon, thanks =) – Logan Sep 14 '12 at 17:59
  • 2
    If you've got it in a window with System.Windows references you will run into some ambiguity issues. I solved it with adding a name to the using: using WinForms = System.Windows.Forms; Then called it with WinForms.NotifyIcon notifyIcon = new WinForms.NotifyIcon(); – Andrew Grinder Jul 07 '14 at 20:00
  • Works excellently, thumbs up. – IssamTP Sep 29 '21 at 09:01
3

You need to add a reference to the System.Windows.Forms.dll and then use the NotifyIcon class.

http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx

Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48