-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppContainer.cs
358 lines (331 loc) · 12.4 KB
/
AppContainer.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace YSRA
{
/// <summary>
/// 可以把其他窗体应用程序嵌入此容器
/// </summary>
[ToolboxBitmap(typeof(AppContainer), "AppControl.bmp")]
public partial class AppContainer : System.Windows.Forms.Panel
{
Action<object, EventArgs> appIdleAction = null;
EventHandler appIdleEvent = null;
public AppContainer(bool showEmbedResult = false)
{
InitializeComponent();
this.ShowEmbedResult = showEmbedResult;
appIdleAction = new Action<object, EventArgs>(Application_Idle);
appIdleEvent = new EventHandler(appIdleAction);
}
public AppContainer(IContainer container, bool showEmbedResult = false)
{
container.Add(this);
InitializeComponent();
this.ShowEmbedResult = showEmbedResult;
appIdleAction = new Action<object, EventArgs>(Application_Idle);
appIdleEvent = new EventHandler(appIdleAction);
}
/// <summary>
/// 将属性<code>AppFilename</code>指向的应用程序打开并嵌入此容器
/// </summary>
public void Start()
{
if (AppProcess != null)
{
Stop();
}
try
{
ProcessStartInfo info = new ProcessStartInfo(this.m_AppFilename);
info.UseShellExecute = true;
//info.UseShellExecute = false;
//info.RedirectStandardOutput = true;
//info.WindowStyle = ProcessWindowStyle.Minimized; ZZQ
//info.WindowStyle = ProcessWindowStyle.Hidden;
info.Arguments = "-gGUIITEMS_96"; //-h1984
info.WorkingDirectory = Path.GetDirectoryName(this.m_AppFilename);
//info.Verb = "runas";
AppProcess = System.Diagnostics.Process.Start(info);
// Wait for process to be created and enter idle condition
AppProcess.WaitForInputIdle();
//todo:下面这两句会引发 NullReferenceException 异常
//AppProcess.Exited += new EventHandler(AppProcess_Exited);
//AppProcess.EnableRaisingEvents = true;
Application.Idle += appIdleEvent;
}
catch (Exception ex)
{
/*
* int i = 0;
int len = 100;
IntPtr hwnd;
do
{
Thread.Sleep(2000);
hwnd = Win32API.FindWindow("Qt5QWindowIcon", null); //"V-REP PRO EDU - "
StringBuilder WindowTitle = new StringBuilder(len);
int rlt = Win32API.GetWindowText(hwnd, WindowTitle, len);
if (hwnd != IntPtr.Zero)
{
if (String.Compare("V-REP PRO EDU - ", 0, WindowTitle.ToString(), 0, 15) !=0)
hwnd = IntPtr.Zero;
}
i++;
}while (hwnd == IntPtr.Zero && i < 30);
if (hwnd == IntPtr.Zero)
{
MessageBox.Show(this, string.Format("{1}{0}{2}{0}{3}"
, Environment.NewLine
, "*" + ex.ToString()
, "*StackTrace:" + ex.StackTrace
, "*Source:" + ex.Source
), "Failed to load app.");
}
else
{
if (EmbedProcess(AppProcess, this))
{
Application.Idle -= appIdleEvent;
}
else if (AppProcess != null)
{
if (!AppProcess.HasExited)
AppProcess.Kill();
AppProcess = null;
}
}*/
}
try
{
int i = 0;
int len = 100;
IntPtr hwnd;
do
{
Thread.Sleep(3000);
hwnd = Win32API.FindWindow("Qt5QWindowIcon", null); //"V-REP PRO EDU - "
StringBuilder WindowTitle = new StringBuilder(len);
int rlt = Win32API.GetWindowText(hwnd, WindowTitle, len);
if (hwnd != IntPtr.Zero)
{
if (String.Compare("V-REP PLAYER - ", 0, WindowTitle.ToString(), 0, 12) != 0)
hwnd = IntPtr.Zero;
//if (String.Compare("V-REP PRO EDU - ", 0, WindowTitle.ToString(), 0, 13) != 0) //15
// hwnd = IntPtr.Zero;
}
i++;
} while (hwnd == IntPtr.Zero && i < 5); //5
if (hwnd == IntPtr.Zero)
{
MessageBox.Show(this, string.Format("{1}{0}{2}{0}"
, Environment.NewLine
, "* Error:"
, "*Source file: " + m_AppFilename
), "Failed to load app.");
}
else
{
if (EmbedProcess(AppProcess, this))
{
Application.Idle -= appIdleEvent;
}
else if (AppProcess != null)
{
if (!AppProcess.HasExited)
AppProcess.Kill();
AppProcess = null;
}
}
}
catch (Exception ex)
{
MessageBox.Show(this, string.Format("{1}{0}{2}{0}{3}"
, Environment.NewLine
, "*" + ex.ToString()
, "*StackTrace:" + ex.StackTrace
, "*Source:" + ex.Source
), "Failed to load app.");
}
}
/// <summary>
/// 确保应用程序嵌入此容器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Application_Idle(object sender, EventArgs e)
{
if (this.AppProcess == null || this.AppProcess.HasExited)
{
this.AppProcess = null;
Application.Idle -= appIdleEvent;
return;
}
if (AppProcess.MainWindowHandle == IntPtr.Zero) return;
//Application.Idle -= appIdleEvent;
if (EmbedProcess(AppProcess, this))
{ Application.Idle -= appIdleEvent; }
//ShowWindow(AppProcess.MainWindowHandle, SW_SHOWNORMAL);
//var parent = GetParent(AppProcess.MainWindowHandle);//不管用,全是0
//if (parent == this.Handle)
//{
// Application.Idle -= appIdleEvent;
//}
}
/// <summary>
/// 应用程序结束运行时要清除这里的标识
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void AppProcess_Exited(object sender, EventArgs e)
{
AppProcess = null;
}
/// <summary>
/// Close <code>AppFilename</code>
/// <para>将属性<code>AppFilename</code>指向的应用程序关闭</para>
/// </summary>
public void Stop()
{
if (AppProcess != null)// && AppProcess.MainWindowHandle != IntPtr.Zero)
{
try
{
if (!AppProcess.HasExited)
AppProcess.Kill();
}
catch (Exception)
{
}
AppProcess = null;
embedResult = 0;
}
}
protected override void OnHandleDestroyed(EventArgs e)
{
Stop();
base.OnHandleDestroyed(e);
}
protected override void OnResize(EventArgs eventargs)
{
if (AppProcess != null)
{
Win32API.MoveWindow(AppProcess.MainWindowHandle, 0, 0, this.Width, this.Height, true);
}
base.OnResize(eventargs);
}
protected override void OnSizeChanged(EventArgs e)
{
this.Invalidate();
base.OnSizeChanged(e);
}
#region 属性
/// <summary>
/// Embedded application's process
/// </summary>
public Process AppProcess { get; set; }
/// <summary>
/// Target app's file name(*.exe)
/// </summary>
private string m_AppFilename = "C:/Program Files (x86)/V-REP3/V-REP_PLAYER/vrep.exe";
/// <summary>
/// Target app's file name(*.exe)
/// </summary>
[Category("Data")]
[Description("Target app's file name(*.exe)")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
//[Editor(typeof(AppFilenameEditor), typeof(UITypeEditor))]
public string AppFilename
{
get
{
return m_AppFilename;
}
set
{
if (value == null || value == m_AppFilename) return;
var self = Application.ExecutablePath;
int exePathLen = value.ToLower().IndexOf(".exe");
String exePath= value.Substring(0,exePathLen+4);
String exeArguments = value.Substring(exePathLen, value.Length - exePathLen - 4);
if (value.ToLower() == self.ToLower())
{
MessageBox.Show("Please don't embed yourself!", "YS01 3D viewer");
return;
}
if (!exePath.ToLower().EndsWith(".exe"))
{
MessageBox.Show("Target is not an *.exe!", "YS01 3D viewer");
}
if (!File.Exists(exePath))
{
MessageBox.Show("V-REP.exe does not exist!Please reinstall it.", "YS01 3D viewer");
return;
}
m_AppFilename = value;
}
}
/// <summary>
/// 标识内嵌程序是否已经启动
/// </summary>
public bool IsStarted { get { return (this.AppProcess != null); } }
#endregion 属性
public void EmbedAgain()
{
EmbedProcess(AppProcess, this);
}
/// <summary>
/// 如果函数成功,返回值为子窗口的原父窗口句柄;如果函数失败,返回值为NULL。若想获得多错误信息,请调用GetLastError函数。
/// </summary>
public int embedResult = 0;
/// <summary>
/// 将指定的程序嵌入指定的控件
/// </summary>
private bool EmbedProcess(Process app, Control control)
{
// Get the main handle
if (app == null || app.MainWindowHandle == IntPtr.Zero || control == null) return false;
embedResult = 0;
try
{
// Put it into this container
embedResult = Win32API.SetParent(app.MainWindowHandle, control.Handle);
}
catch (Exception)
{ }
try
{
// Remove border and whatnot
Win32API.SetWindowLong(new HandleRef(this, app.MainWindowHandle), Win32API.GWL_STYLE, Win32API.WS_VISIBLE);
}
catch (Exception)
{ }
try
{
// Move the window to overlay it on this window
Win32API.MoveWindow(app.MainWindowHandle, 0, 0, control.Width, control.Height, true);
}
catch (Exception)
{ }
if (ShowEmbedResult)
{
var errorString = Win32API.GetLastError();
MessageBox.Show(errorString);
}
return (embedResult != 0);
}
/// <summary>
/// Show a MessageBox to tell whether the embedding is successfully done.
/// </summary>
public bool ShowEmbedResult { get; set; }
}
}