FrmBase.cs
3.3 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using Common;
using System;
using System.Windows.Forms;
namespace UIControl.Forms
{
public partial class FrmBase : Form
{
public FrmBase(string name = "App")
{
InitializeComponent();
InitMiniIcon(name);
}
public FrmBase()
{
InitializeComponent();
}
#region 托盘
private bool exit = false;
private NotifyIcon notify;
private ContextMenuStrip notifyMenu;
private void InitMiniIcon(string name)
{
//托盘菜单
notifyMenu = new ContextMenuStrip();
ToolStripMenuItem itemShow = new ToolStripMenuItem("显示(&S)");
itemShow.Click += ItemShow_Click;
ToolStripMenuItem itemExit = new ToolStripMenuItem("退出(&X)");
itemExit.Click += ItemExit_Click;
notifyMenu.Items.Add(itemShow);
notifyMenu.Items.Add(itemExit);
//托盘控件
notify = new NotifyIcon { Icon = Icon, Visible = true, ContextMenuStrip = notifyMenu, Text = name };
notify.MouseDoubleClick += Notify_MouseDoubleClick; ;
Opacity = 100;
Visible = false;
notify.Visible = true;
ShowInTaskbar = false;
Cursor = Cursors.Default;
}
private void Notify_MouseDoubleClick(object sender, MouseEventArgs e)
{
//Show();
show();
if (WindowState == FormWindowState.Minimized)
WindowState = FormWindowState.Normal;
}
private void ItemExit_Click(object sender, EventArgs e)
{
//notify.Dispose();
//exit = true;
//Close();
ExitApp();
}
private void ItemShow_Click(object sender, EventArgs e)
{
show();
}
private void show()
{
Opacity = 100;
Visible = true;
notify.Visible = false;
ShowInTaskbar = true;
}
#endregion
/// <summary>
/// 关闭窗体
/// </summary>
protected void FrmClose()
{
Close();
}
/// <summary>
/// 关闭所有线程资源
/// </summary>
protected virtual void CloseAllThreads()
{
}
/// <summary>
/// 隐藏窗体
/// </summary>
protected void HideForm()
{
Opacity = 0;
Visible = false;
notify.Visible = true;
ShowInTaskbar = false;
GC.Collect();
}
/// <summary>
/// 退出软件
/// </summary>
private void ExitApp()
{
LogUtil.Info("点击:退出系统");
DialogResult result = MessageBox.Show("是否确定退出客户端?", "提示", MessageBoxButtons.YesNo);
if (result.Equals(DialogResult.Yes))
{
try
{
CloseAllThreads();
System.Diagnostics.Process.GetCurrentProcess().Kill();
Close();
Environment.Exit(Environment.ExitCode);
}
catch (Exception ex)
{
LogUtil.Error("退出软件出错:", ex);
}
}
}
}
}