RunMode.cs
6.5 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
namespace BLL
{
public static class RunMode
{
[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll ", SetLastError = true)]
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
private const int SW_RESTORE = 9;
private static Form frm;
private static FormWindowState state;
private static bool exit = false;
private static ToolStripItem[] items;
private static NotifyIcon notify;
private static ContextMenuStrip notifyMenu;
private static void AddItem()
{
notifyMenu = new ContextMenuStrip();
items = new ToolStripItem[4];
//System.Reflection.Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();
//string text = assembly.GetName().Version.ToString();
//text = frm.Text + text;
items[0] = new ToolStripMenuItem(frm.Text) { Font = new Font("微软雅黑", 11f, FontStyle.Bold), Image = frm.Icon.ToBitmap() };
items[1] = new ToolStripSeparator();
items[2] = new ToolStripMenuItem("显示") { Font = new Font("微软雅黑", 11f) };
items[3] = new ToolStripMenuItem("退出") { Font = new Font("微软雅黑", 11f) };
notifyMenu.Items.AddRange(items);
items[2].Click += ItemShow_Click;
items[3].Click += ItemExit_Click;
notify = new NotifyIcon { Icon = frm.Icon, Visible = true, ContextMenuStrip = notifyMenu, Text = frm.Text };
notify.MouseDoubleClick += Notify_MouseDoubleClick;
}
private static void ItemShow_Click(object sender, EventArgs e)
{
frm.Show();
if (frm.WindowState == FormWindowState.Minimized)
frm.WindowState = state;
}
private static void ItemExit_Click(object sender, EventArgs e)
{
notify.Dispose();
exit = true;
frm.Close();
}
private static void Notify_MouseDoubleClick(object sender, MouseEventArgs e)
{
frm.Show();
if (frm.WindowState == FormWindowState.Minimized)
frm.WindowState = state;
}
public static void Init(Form frm)
{
RunMode.frm = frm;
state = frm.WindowState;
frm.StartPosition = FormStartPosition.CenterScreen;
AddItem();
}
public static void MenuLanguage(params string[] name)
{
if (items == null || name == null) return;
if (name.Length >= 1) items[2].Text = name[0];
if (name.Length >= 2) items[3].Text = name[1];
}
public static bool Closing(FormClosingEventArgs e)
{
if (!exit)
{
e.Cancel = true;
state = frm.WindowState;
frm.WindowState = FormWindowState.Minimized;
}
return exit;
}
public static bool IsRun()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id == current.Id) continue; //自己
if (process.MainModule.FileName == current.MainModule.FileName)
{
//显示已打开的程序
ShowWindow(process.MainWindowHandle, SW_RESTORE);
SwitchToThisWindow(process.MainWindowHandle, true);
return true;
}
}
return false;
}
public static bool IsAdmin()
{
System.Security.Principal.WindowsIdentity current = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal windowsPrincipal = new System.Security.Principal.WindowsPrincipal(current);
return windowsPrincipal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
}
public static void AdminRun()
{
string path = Process.GetCurrentProcess().MainModule.FileName;
ProcessStartInfo startInfo = new ProcessStartInfo
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = path,
Verb = "runas"
};
Process.Start(startInfo);
}
/// <summary>
/// 复制文件
/// </summary>
/// <param name="srcPath">原路径</param>
/// <param name="dstPath">目标路径</param>
/// <param name="overwrite">是否覆盖</param>
/// <returns></returns>
//public static bool CopyFile(string srcPath, string dstPath, bool overwrite)
//{
// bool rtn = false;
// try
// {
// if (!srcPath.EndsWith("\\")) srcPath += "\\";
// if (!dstPath.EndsWith("\\")) dstPath += "\\";
// if (System.IO.Directory.Exists(srcPath))
// {
// //目录是否存在
// if (!System.IO.Directory.Exists(dstPath))
// System.IO.Directory.CreateDirectory(dstPath);
// //复制文件
// string[] files = System.IO.Directory.GetFiles(srcPath);
// foreach (string file in files)
// {
// System.IO.FileInfo fInfo = new System.IO.FileInfo(file);
// fInfo.CopyTo(dstPath + fInfo.Name, overwrite);
// }
// rtn = true;
// //复制文件夹
// string[] dirs = System.IO.Directory.GetDirectories(srcPath);
// foreach (string dir in dirs)
// {
// System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(dir);
// rtn = CopyFile(dir, dstPath + dInfo.Name, overwrite);
// if (!rtn) break;
// }
// }
// }
// catch (Exception ex)
// {
// rtn = false;
// }
// return rtn;
//}
}
}