SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
Executable.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Diagnostics;
5 using System.Runtime.InteropServices;
6 using System.Xml.Serialization;
7 
8 namespace Seci.Definitions
9 {
15  public class Executable : IDisposable
16  {
17  //Members
18  private String _name;
19  private String _filePath;
20  private String _owningComponent;
21  private int _positionX;
22  private int _positionY;
23  private int _width;
24  private int _height;
25  private Process _process;
26  private bool _disposed;
27 
28  //Properties
29  public String Name { get { return _name; } set { _name = value; } }
30  public String FilePath { get { return _filePath; } set { _filePath = value; } }
31  public int PositionX { get { return _positionX; } set { _positionX = value; } }
32  public int PositionY { get { return _positionY; } set { _positionY = value; } }
33  [XmlIgnore] public String OwningComponent { get { return _owningComponent; } set { _owningComponent = value; } }
34 
38  public Executable()
39  {
40 
41  }
42 
47  public Executable(String filepath)
48  {
49  _filePath = filepath;
50 
51  _positionX = 200; //default value
52  _positionY = 200; //default value
53 
54  Load();
55  getSize();
56  }
57 
61  public void Load()
62  {
63  if (_process == null)
64  {
65  _process = new Process();
66 
67  _process.StartInfo.FileName = _filePath;
68  _process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
69  _process.Start();
70 
71  //Need to wait three seconds
72  System.Threading.Thread.Sleep(3000);
73  }
74  }
75 
80  public Boolean Hide()
81  {
82  Helpers.NativeMethods.ShowWindow(_process.MainWindowHandle.ToInt32(), 0);
83 
84  //Get the position (and size?)
85  GetPosition();
86 
87  return true;
88  }
89 
95  public Boolean Show()
96  {
97  Helpers.NativeMethods.ShowWindow(_process.MainWindowHandle.ToInt32(), 9);
98  Helpers.NativeMethods.SetForegroundWindow(_process.MainWindowHandle.ToInt32());
99 
100  return true;
101  }
102 
108  private void getSize()
109  {
110  if (Helpers.NativeMethods.IsIconic(_process.MainWindowHandle.ToInt32()) == 0)
111  {
112  //Not minimised, so get size
113  Helpers.Rect rect = new Helpers.Rect();
114 
115  Helpers.NativeMethods.GetWindowRect(_process.MainWindowHandle.ToInt32(), out rect);
116 
117  _width = rect.Width - rect.X;
118  _height = rect.Height - rect.Y;
119  }
120  }
121 
127  public void GetPosition()
128  {
129  if (Helpers.NativeMethods.IsIconic(_process.MainWindowHandle.ToInt32()) == 0)
130  {
131  //Not minimised, so get size
132  Helpers.Rect rect = new Helpers.Rect();
133 
134  Helpers.NativeMethods.GetWindowRect(_process.MainWindowHandle.ToInt32(), out rect);
135 
136  _positionX = rect.X;
137  _positionY = rect.Y;
138  }
139  }
140 
146  public void Move()
147  {
148  //First getsize to check that it hasn't changed
149  getSize();
150  Helpers.NativeMethods.MoveWindow(_process.MainWindowHandle, _positionX, _positionY, _width, _height, true);
151  }
152 
159  public void Move(int xPos, int yPos)
160  {
161  //First getsize to check that it hasn't changed
162  getSize();
163 
164  //Then move it and update positions
165  Helpers.NativeMethods.MoveWindow(_process.MainWindowHandle, xPos, yPos, _width, _height, true);
166  _positionX = xPos;
167  _positionY = yPos;
168  }
169 
170  #region IDisposable Members
171 
179  public void Dispose()
180  {
181  try
182  {
183  Dispose(true);
184  }
185  finally
186  {
187  GC.SuppressFinalize(this);
188  }
189  }
190 
195  {
196  Dispose(false);
197  }
198 
207  protected virtual void Dispose(Boolean disposing)
208  {
209  //Check has not already been disposed.
210  if (!_disposed)
211  {
212  if (_process != null && !_process.HasExited)
213  {
214  _process.Kill();
215  _process.Dispose();
216  }
217 
218  _process = null;
219  }
220  _disposed = true;
221  }
222 
223  #endregion
224  }
225 }
~Executable()
This destructor will run only if the Dispose method does not get called.
Definition: Executable.cs:194
This class is used for wrapping standard executables that are included as part of a configuration...
Definition: Executable.cs:15
Executable()
Standard Constructor - neccessary for XML deserialisation.
Definition: Executable.cs:38
void Load()
Method for creating the executable's process.
Definition: Executable.cs:61
int _positionX
The component to which this exe belongs.
Definition: Executable.cs:21
Boolean Show()
Method for showing the executable and bringing it to the front. Uses the ShowWindow and SetForeground...
Definition: Executable.cs:95
void Move()
Method for moving an executable's window. This is usually done after the executable is loaded...
Definition: Executable.cs:146
void getSize()
Method for getting the size of an executable's window. First checks that the window is not minimised...
Definition: Executable.cs:108
void Move(int xPos, int yPos)
Overloaded method for moving an executable's window. Uses the MoveWindow method from the WinAPI...
Definition: Executable.cs:159
void Dispose()
Public Dispose Method - member of IDisposable. The loaded executable is unmanaged so it needs to be d...
Definition: Executable.cs:179
virtual void Dispose(Boolean disposing)
Protected Dispose Method - member of IDisposable. If the value passed it true then the method has bee...
Definition: Executable.cs:207
Executable(String filepath)
Recommended Constructor for use in code.
Definition: Executable.cs:47
Boolean Hide()
Method for hiding the executable - uses the ShowWindow method from the WinAPI.
Definition: Executable.cs:80
void GetPosition()
Method for getting the position of an executable's window. First checks that the window is not minimi...
Definition: Executable.cs:127