using System.Windows.Forms;
namespace ServerApp
{
public partial class MainWindow : Window
{
private NotifyIcon _notifyIcon = null;
public MainWindow()
{
InitializeComponent();
InitialTray();
}
#region 最小化系统托盘
private void InitialTray()
{
this.Visibility = Visibility.Hidden;
_notifyIcon = new NotifyIcon();
_notifyIcon.BalloonTipText = "运行中...";
_notifyIcon.Text = "复制助手";
_notifyIcon.Visible = true;
_notifyIcon.Icon = new Icon(@"logo.ico");
_notifyIcon.ShowBalloonTip(2000);
_notifyIcon.MouseDoubleClick += notifyIcon_MouseDoubleClick;
this.StateChanged += MainWindow_StateChanged;
}
#endregion
#region 窗口状态改变
private void MainWindow_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Minimized)
{
this.Visibility = Visibility.Hidden;
}
}
#endregion
#region 托盘图标鼠标单击事件
private void notifyIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (this.Visibility == Visibility.Visible)
{
this.Visibility = Visibility.Hidden;
}
else
{
this.Visibility = Visibility.Visible;
this.Activate();
}
}
}
#endregion
}
}
将图标替换成下面内容,意思就是读取程序图标,来作为托盘图标
this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);