SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
CommandLineHost.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Runtime.InteropServices;
10 using System.Diagnostics;
11 
12 namespace SeciControls
13 {
14  public partial class CommandLineHost : UserControl, IDisposable
15  {
16  [DllImport("User32")]
17  private static extern int SetForegroundWindow(IntPtr hWnd);
18 
19  private Process _proc;
20  private IntPtr _windowHandle;
21  private delegate void updateLinkVisibilityDelegate(Boolean visible);
22  private bool _procKilled = false;
23  private static String _filepath;
24  private static String _args;
25  private bool _usePython = false;
26  private int _procID = 0;
27  public String LinkMessage { get { return linkStart.Text; } set { linkStart.Text = value; } }
28  public Boolean UsePython { get { return _usePython; } set { _usePython = value; } }
29  public int ProcID { get { return _procID; } }
30 
31  public event EventHandler OnProcessStartedViaLink;
32 
33  public CommandLineHost()
34  {
36  }
37 
38  public void Initialise(String filepath, String args)
39  {
40  _filepath = filepath;
41  _args = args;
42  }
43 
44  public void StartProcess()
45  {
46  linkStart.Visible = false;
47 
48  try
49  {
50  if (_proc == null || _proc.HasExited)
51  {
53 
54  if (_proc.Start())
55  {
56  _proc.EnableRaisingEvents = true;
57  _proc.Exited += new EventHandler(proc_Exited);
58  }
59 
60  WinApi.LockWindowUpdate(WinApi.GetDesktopWindow());
61  while (!getHandle())
62  {
63  System.Threading.Thread.Sleep(100);
64  }
65 
67  WinApi.SetParent(_windowHandle, this.Handle);
68 
69  WinApi.LockWindowUpdate(IntPtr.Zero);
70 
72  _procKilled = false;
73  _procID = _proc.Id;
74  }
75  }
76  catch (Exception err)
77  {
78  //Could not create process and capture
79  Seci.Helpers.MessageLogger.WriteMessage("CommandLineHost", err.Message);
80  }
81  //linkStart.Visible = false;
82  }
83 
84  private Boolean getHandle()
85  {
86  Process[] procs = Process.GetProcesses();
87  IntPtr hWnd;
88  foreach (Process proc in procs)
89  {
90  try
91  {
92  if (proc.Id == _proc.Id)
93  {
94  if ((hWnd = proc.MainWindowHandle) != IntPtr.Zero)
95  {
96  _windowHandle = hWnd;
97  return true;
98  }
99  }
100  }
101  catch
102  {
103  }
104  }
105  return false;
106  }
107 
108  private void positionSubWindow()
109  {
110  WinApi.Rect temp = new WinApi.Rect();
111  WinApi.GetWindowRect(_windowHandle, out temp);
112 
113  int borderWidth = SystemInformation.Border3DSize.Width;
114  int borderHeight = SystemInformation.Border3DSize.Height;
115  int captionHeight = SystemInformation.CaptionHeight;
116  Size frameborder = SystemInformation.FrameBorderSize;
117 
118  WinApi.MoveWindow(_windowHandle, -frameborder.Width, -frameborder.Height - captionHeight, temp.Width - temp.X, temp.Height - temp.Y, true);
119 
120  if (frameborder.Width == 8)
121  {
122  //Vista theme
123  this.Width = temp.Width - temp.X - frameborder.Width - 3 * borderWidth;
124  this.Height = temp.Height - temp.Y - captionHeight - frameborder.Height - 3 * borderHeight;
125  }
126  else
127  {
128  //non-vista theme
129  this.Width = temp.Width - temp.X - frameborder.Width;
130  this.Height = temp.Height - temp.Y - captionHeight - frameborder.Height;
131  }
132 
133  this.MinimumSize = this.Size;
134  this.MaximumSize = this.Size;
135  }
136 
137  private void disableSystemMenu()
138  {
139  //Disable System menu
140  IntPtr hMenu = WinApi.GetSystemMenu(_windowHandle, false);
141  int numItems = WinApi.GetMenuItemCount(hMenu);
142 
143  for (int i = numItems - 1; i >= 0; --i)
144  {
145  WinApi.RemoveMenu(hMenu, i, WinApi.MF_REMOVE | WinApi.MF_BYPOSITION);
146  }
147  }
148 
149  private void createProcess(String filepath, String args)
150  {
151  _proc = new Process();
152  _proc.StartInfo = new ProcessStartInfo(filepath, args);
153  _proc.StartInfo.Domain = AppDomain.CurrentDomain.FriendlyName;
154  _proc.StartInfo.ErrorDialogParentHandle = this.Handle;
155  _proc.StartInfo.UseShellExecute = true;
156  _proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
157  //_proc.Exited += new EventHandler(proc_Exited);
158  }
159 
160  public void KillProcess()
161  {
162  try
163  {
164  if (_proc != null && !_procKilled)
165  {
166  _procKilled = true;
167  _proc.Kill();
168  _proc = null;
169  }
170  }
171  catch
172  {
173  }
174  }
175 
176  void proc_Exited(object sender, EventArgs e)
177  {
178  if (!_procKilled)
179  {
180  Invoke(new updateLinkVisibilityDelegate(linkVisibility), true);
181  }
182  }
183 
184  private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
185  {
186  StartProcess();
187  if (OnProcessStartedViaLink != null)
188  {
189  OnProcessStartedViaLink(sender, e);
190  }
191  }
192 
193  private void linkVisibility(bool visible)
194  {
195  linkStart.Visible = visible;
196  }
197 
198  #region IDisposable Members
199 
200  void IDisposable.Dispose()
201  {
202  Dispose(true);
203  GC.SuppressFinalize(this);
204  }
205 
206  protected override void Dispose(bool disposing)
207  {
208  if (_proc != null && (!_proc.Responding || !_proc.HasExited))
209  {
210  _proc.Kill();
211  _procKilled = true;
212  _proc.Dispose();
213  _proc = null;
214  }
215 
216  if (disposing && (components != null))
217  {
218  components.Dispose();
219  }
220 
221  base.Dispose(disposing);
222  }
223 
224  #endregion
225 
226  public void SetFocus()
227  {
228  //Set the focus on this window when te genie tab is selected
230  }
231 
232  }
233 
234 }
void proc_Exited(object sender, EventArgs e)
System.ComponentModel.IContainer components
Required designer variable.
void InitializeComponent()
Required method for Designer support - do not modify the contents of this method with the code editor...
static int SetForegroundWindow(IntPtr hWnd)
override void Dispose(bool disposing)
delegate void updateLinkVisibilityDelegate(Boolean visible)
void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
void linkVisibility(bool visible)
void Initialise(String filepath, String args)
void IDisposable. Dispose()
void createProcess(String filepath, String args)
const int MF_BYPOSITION
Definition: WinApi.cs:13