-
Notifications
You must be signed in to change notification settings - Fork 11
/
Interactive.cs
192 lines (164 loc) · 6.89 KB
/
Interactive.cs
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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using NewLife.Log;
namespace XAgent
{
/// <summary>用户界面交互</summary>
public static class Interactive
{
private static Form _MainForm;
/// <summary>主窗口</summary>
public static Form MainForm { get { return _MainForm ?? (_MainForm = new FrmMain()); } set { _MainForm = value; } }
static Boolean hasShown = false;
/// <summary>隐藏</summary>
public static void Hide()
{
if (!hasShown) return;
try
{
MainForm.Invoke(new Action<Form>(f => f.Hide()));
}
catch { }
}
/// <summary>显示窗体</summary>
public static void ShowForm()
{
if (hasShown) return;
hasShown = true;
Task.Factory.StartNew(Show, TaskCreationOptions.LongRunning);
//Thread thread = new Thread(Show);
//thread.Name = "XAgentUI";
//thread.IsBackground = true;
//thread.Start();
}
static void Show()
{
GetDesktopWindow();
IntPtr hwinstaSave = GetProcessWindowStation();
IntPtr dwThreadId = GetCurrentThreadId();
IntPtr hdeskSave = GetThreadDesktop(dwThreadId);
IntPtr hwinstaUser = OpenWindowStation("WinSta0", false, 33554432);
if (hwinstaUser == IntPtr.Zero)
{
RpcRevertToSelf();
return;
}
SetProcessWindowStation(hwinstaUser);
IntPtr hdeskUser = OpenDesktop("Default", 0, false, 33554432);
RpcRevertToSelf();
if (hdeskUser == IntPtr.Zero)
{
SetProcessWindowStation(hwinstaSave);
CloseWindowStation(hwinstaUser);
return;
}
SetThreadDesktop(hdeskUser);
IntPtr dwGuiThreadId = dwThreadId;
//Form frm = MainForm;
//frm.Visible = false;
//Application.Run(frm);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Init();
Application.Run();
dwGuiThreadId = IntPtr.Zero;
SetThreadDesktop(hdeskSave);
SetProcessWindowStation(hwinstaSave);
CloseDesktop(hdeskUser);
CloseWindowStation(hwinstaUser);
}
#region 托盘图标初始化
static void Init()
{
var Components = new Container();
var ni = new NotifyIcon(Components);
var menu = new ContextMenuStrip();
menu.Size = new Size(153, 98);
menu.Items.Add("主界面", null, delegate { MainForm.Show(); MainForm.BringToFront(); });
menu.Items.Add("打开目录", null, delegate { Process.Start("explorer.exe", "\"" + AppDomain.CurrentDomain.BaseDirectory + "\""); });
var tsmi = new ToolStripSeparator();
tsmi.Size = new Size(menu.Size.Width - 4, 6);
menu.Items.Add(tsmi);
menu.Items.Add("退出", null, delegate
{
var service = AgentServiceBase.Instance;
if (MessageBox.Show("退出将会同时关闭" + service.DisplayName + "服务,确定退出?", "退出", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//AgentServiceBase.ControlService(false);
service.ControlService(false);
Application.Exit();
}
});
//ni.Icon = null;
ni.ContextMenuStrip = menu;
ni.Visible = true;
var resources = new ComponentResourceManager(typeof(FrmMain));
//ni.Icon = ((Icon)(resources.GetObject("notifyIcon1.Icon")));
ni.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(Interactive), "leaf.ico"));
ni.Text = "新生命服务代理";
ni.Visible = true;
ni.MouseDoubleClick += ni_MouseDoubleClick;
}
static void ni_MouseDoubleClick(object sender, MouseEventArgs e)
{
try
{
MainForm.Show();
MainForm.BringToFront();
}
catch { MainForm = null; }
}
#endregion
#region 异常捕获
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
XTrace.WriteLine("" + e.ExceptionObject);
if (e.IsTerminating)
{
XTrace.WriteLine("异常退出!");
XTrace.WriteMiniDump(null);
MessageBox.Show("" + e.ExceptionObject, "异常退出", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
XTrace.WriteLine(e.Exception.ToString());
}
#endregion
#region PInvoke
[DllImport("user32.dll")]
static extern int GetDesktopWindow();
[DllImport("user32.dll")]
static extern IntPtr GetProcessWindowStation();
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThreadId();
[DllImport("user32.dll")]
static extern IntPtr GetThreadDesktop(IntPtr dwThread);
[DllImport("user32.dll")]
static extern IntPtr OpenWindowStation(string a, bool b, int c);
[DllImport("user32.dll")]
static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags, bool fInherit, uint dwDesiredAccess);
[DllImport("user32.dll")]
static extern IntPtr CloseDesktop(IntPtr p);
[DllImport("rpcrt4.dll", SetLastError = true)]
static extern IntPtr RpcImpersonateClient(int i);
[DllImport("rpcrt4.dll", SetLastError = true)]
static extern IntPtr RpcRevertToSelf();
[DllImport("user32.dll")]
static extern IntPtr SetThreadDesktop(IntPtr a);
[DllImport("user32.dll")]
static extern IntPtr SetProcessWindowStation(IntPtr a);
[DllImport("user32.dll")]
static extern IntPtr CloseWindowStation(IntPtr a);
#endregion
}
}