1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FLY.AppHelper
{
/// <summary>
/// 通知区 图标生成辅助工具
/// </summary>
public class WindowNotifyIconHelper
{
System.Windows.WindowState ws;
System.Windows.WindowState wsl;
System.Windows.Forms.NotifyIcon notifyIcon;
System.Windows.Window window;
/// <summary>
///
/// </summary>
/// <param name="window">窗体</param>
/// <param name="text">当鼠标指针停留在通知区域图标上时显示的工具提示文本</param>
public WindowNotifyIconHelper(System.Windows.Window window, string text)
{
notifyIcon = new System.Windows.Forms.NotifyIcon();
//this.notifyIcon.BalloonTipText = "Hello, NotifyIcon!";
notifyIcon.Text = text;
notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
notifyIcon.Visible = true;
notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick;
//退出菜单项
MenuItem exit = new MenuItem("退出");
exit.Click += new EventHandler(exit_Click);
//关联托盘控件
MenuItem[] childen = new MenuItem[] { exit };
notifyIcon.ContextMenu = new ContextMenu(childen);
//this.notifyIcon.ShowBalloonTip(1000);
this.window = window;
wsl = this.window.WindowState;
this.window.StateChanged += new EventHandler(window_StateChanged);
this.window.Closing += new System.ComponentModel.CancelEventHandler(window_Closing);
}
/// <summary>
/// 退出选项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void exit_Click(object sender, EventArgs e)
{
System.Windows.MessageBoxResult r = System.Windows.MessageBox.Show("是否真的要退出程序??", "警告", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Warning, System.Windows.MessageBoxResult.No);
if (r == System.Windows.MessageBoxResult.Yes)
{
//notifyIcon.Visible = false;
notifyIcon.Dispose();
//System.Windows.Application.Current.Shutdown();
System.Environment.Exit(0);
}
}
void window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//this.notifyIcon.Visible = false;
e.Cancel = true;
this.window.WindowState = System.Windows.WindowState.Minimized;
//System.Windows.MessageBoxResult r = System.Windows.MessageBox.Show("是否真的要退出程序??", "警告", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Warning, System.Windows.MessageBoxResult.No);
//if (r == System.Windows.MessageBoxResult.Yes)
//{
//}
//else
//{
// e.Cancel = true;
// notifyIcon.Visible = true;
//}
}
void window_StateChanged(object sender, EventArgs e)
{
ws = window.WindowState;
if (ws == System.Windows.WindowState.Minimized)
{
window.Hide();
}
}
private void OnNotifyIconDoubleClick(object sender, EventArgs e)
{
window.Show();
window.WindowState = wsl;
//notifyIcon.Visible = false;
}
}
}