-2

Code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        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); //如果不是,不做处理(准确的说,是根据参数e做调整)
    }


}

I am trying to realize my App MainWindow minimize to traypanel.And I found this code snippet.But it doesn't work in my VS2012.

source:(Easiest way to have a program minimize itself to the system tray using .NET 4)

I guess it's because I didn't have an icon file in the Project.So, I selected an icon file(.ico) and paste it to my project.Also ,it doesn't work.

[Problem Resolved,Thank You All] I should paste icon file to the file folder where .exe file was generated. 囧~~

Community
  • 1
  • 1
FranklinLee
  • 309
  • 2
  • 9

1 Answers1

0

I'm lucky to find the resolving method : paste icon file to the file folder where .exe file was generated,instead of paste it to the project folder in VS.

I don't know why ,but it really works.

FranklinLee
  • 309
  • 2
  • 9