Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

c# - Easiest way to have a program minimize itself to the system tray using .NET 4

I'm making a new WPF application and I need to be able to minimize the application and have nice and snug in the system tray, right beside the clock (or in that general area).

This has to work on Windows XP, Vista and 7. I don't have to support older versions of Windows.

What's the simplest way to achieve this if I'm using .NET 4?

question from:https://stackoverflow.com/questions/10230579/easiest-way-to-have-a-program-minimize-itself-to-the-system-tray-using-net-4

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Example in MSDN forum

Here's a quick example to show how to minimize to the notification area. You need to add references to the System.Window.Forms and System.Drawing assemblies.

public partial class Window1 : System.Windows.Window
{

    public Window1()
    {
        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 == System.Windows.WindowState.Minimized)
            this.Hide();

        base.OnStateChanged(e);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...