BioImager  5.0.0
A .NET microscopy imaging application based on Bio library. Supports various microscopes by using imported libraries & GUI automation. Supports XInput game controllers to move stage, take images, run ImageJ macros on images or Bio C# scripts.
Loading...
Searching...
No Matches
Win32.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Runtime.InteropServices;
5using System.Text;
6using System.Threading.Tasks;
7using System.Diagnostics;
8
9namespace BioImager
10{
11 public class Win32
12 {
13 [DllImport("User32.dll")]
14 public static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
15 public const int WM_KEYDOWN = 0x100;
16 public const int WM_KEYUP = 0x101;
24 public static bool GetKeyState(System.Windows.Forms.Keys vKey)
25 {
26 int si = (int)GetAsyncKeyState(vKey);
27 if (si == 0)
28 return false;
29 else
30 return true;
31 }
32
33 [DllImport("user32.dll")]
34 public static extern IntPtr GetForegroundWindow();
35
36 [DllImport("user32.dll")]
37 public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
38 [DllImport("user32.dll")]
39 public static extern bool SetForegroundWindow(IntPtr hWnd);
40 [DllImport("user32.dll")]
41 public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
42
43 [StructLayout(LayoutKind.Sequential)]
44 public struct Rect
45 {
46 public int X; // x position of upper-left corner
47 public int Y; // y position of upper-left corner
48 public int Right; // x position of lower-right corner
49 public int Bottom; // y position of lower-right corner
50 }
51 public const int MOUSEEVENTF_ABSOLUTE = 0x8000;
52 public const int MOUSEEVENTF_LEFTDOWN = 0x0002;//The left button is down.
53 public const int MOUSEEVENTF_LEFTUP = 0x0004;//The left button is up.
54 public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;//The middle button is down.
55 public const int MOUSEEVENTF_MIDDLEUP=0x0040;//The middle button is up.
56 public const int MOUSEEVENTF_MOVE=0x0001;//Movement occurred.
57 public const int MOUSEEVENTF_RIGHTDOWN=0x0008;//The right button is down.
58 public const int MOUSEEVENTF_RIGHTUP=0x0010;//The right button is up.
59 public const int MOUSEEVENTF_WHEEL = 0x0800;//The wheel has been moved, if the mouse has a wheel.The amount of movement is specified in dwData
60 public const int MOUSEEVENTF_XDOWN=0x0080;//An X button was pressed.
61 public const int MOUSEEVENTF_XUP=0x0100;//An X button was released.
62 public const int MOUSEEVENTF_HWHEEL=0x01000; //The wheel button is tilted.
63
64 [DllImport("user32.dll")]
65 public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
66
68 public static void MouseWheelUp()
69 {
70 mouse_event(MOUSEEVENTF_WHEEL, 0, 0, 120, 0);
71 }
73 public static void MouseWheelDown()
74 {
75 mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -120, 0);
76 }
81 public static void SetFocus(string process)
82 {
83 Process pr = Process.GetProcessesByName(process)[0];
84 SetForegroundWindow(pr.MainWindowHandle);
85 }
86
90 public static string GetActiveWindowTitle()
91 {
92 const int nChars = 256;
93 StringBuilder Buff = new StringBuilder(nChars);
94 IntPtr handle = GetForegroundWindow();
95
96 if (GetWindowText(handle, Buff, nChars) > 0)
97 {
98 return Buff.ToString();
99 }
100 return null;
101 }
102
103 }
104 public class MouseOperations
105 {
106 [Flags]
107 /* Defining an enum. */
108 public enum MouseEventFlags
109 {
110 LeftDown = 0x00000002,
111 LeftUp = 0x00000004,
112 MiddleDown = 0x00000020,
113 MiddleUp = 0x00000040,
114 Move = 0x00000001,
115 Absolute = 0x00008000,
116 RightDown = 0x00000008,
117 RightUp = 0x00000010
118 }
119
120 [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
121 [return: MarshalAs(UnmanagedType.Bool)]
122 private static extern bool SetCursorPos(int x, int y);
123
124 [DllImport("user32.dll")]
125 [return: MarshalAs(UnmanagedType.Bool)]
126 private static extern bool GetCursorPos(out MousePoint lpMousePoint);
127
128 [DllImport("user32.dll")]
129 private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
130
131 public static void LeftClick()
132 {
133 MouseEvent(MouseEventFlags.LeftDown | MouseEventFlags.LeftUp);
134 }
135 public static void RightClick()
136 {
137 MouseEvent(MouseEventFlags.RightDown | MouseEventFlags.RightUp);
138 }
139 public static void LeftDown()
140 {
141 MouseEvent(MouseEventFlags.LeftDown);
142 }
143 public static void RightDown()
144 {
145 MouseEvent(MouseEventFlags.RightDown);
146 }
147 public static void LeftUp()
148 {
150 }
151 public static void RightUp()
152 {
154 }
159 public static void SetCursorPosition(int x, int y)
160 {
161 SetCursorPos(x, y);
162 }
163
168 public static void SetCursorPosition(MousePoint point)
169 {
170 SetCursorPos(point.X, point.Y);
171 }
172
178 {
179 MousePoint currentMousePoint;
180 var gotPoint = GetCursorPos(out currentMousePoint);
181 if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
182 return currentMousePoint;
183 }
184
188 public static void MouseEvent(MouseEventFlags value)
189 {
190 MousePoint position = GetCursorPosition();
191
192 mouse_event
193 ((int)value,
194 position.X,
195 position.Y,
196 0,
197 0)
198 ;
199 }
200
201 [StructLayout(LayoutKind.Sequential)]
202 public struct MousePoint
203 {
204 public int X;
205 public int Y;
206
207 public MousePoint(int x, int y)
208 {
209 X = x;
210 Y = y;
211 }
212 }
213 }
214 static public class FileUtil
215 {
216 [StructLayout(LayoutKind.Sequential)]
217 struct RM_UNIQUE_PROCESS
218 {
219 public int dwProcessId;
220 public System.Runtime.InteropServices.ComTypes.FILETIME ProcessStartTime;
221 }
222
223 const int RmRebootReasonNone = 0;
224 const int CCH_RM_MAX_APP_NAME = 255;
225 const int CCH_RM_MAX_SVC_NAME = 63;
226
227 enum RM_APP_TYPE
228 {
229 RmUnknownApp = 0,
230 RmMainWindow = 1,
231 RmOtherWindow = 2,
232 RmService = 3,
233 RmExplorer = 4,
234 RmConsole = 5,
235 RmCritical = 1000
236 }
237
238 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
239 struct RM_PROCESS_INFO
240 {
241 public RM_UNIQUE_PROCESS Process;
242
243 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_APP_NAME + 1)]
244 public string strAppName;
245
246 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_SVC_NAME + 1)]
247 public string strServiceShortName;
248
249 public RM_APP_TYPE ApplicationType;
250 public uint AppStatus;
251 public uint TSSessionId;
252 [MarshalAs(UnmanagedType.Bool)]
253 public bool bRestartable;
254 }
255
256 [DllImport("rstrtmgr.dll", CharSet = CharSet.Unicode)]
257 static extern int RmRegisterResources(uint pSessionHandle,
258 UInt32 nFiles,
259 string[] rgsFilenames,
260 UInt32 nApplications,
261 [In] RM_UNIQUE_PROCESS[] rgApplications,
262 UInt32 nServices,
263 string[] rgsServiceNames);
264
265 [DllImport("rstrtmgr.dll", CharSet = CharSet.Auto)]
266 static extern int RmStartSession(out uint pSessionHandle, int dwSessionFlags, string strSessionKey);
267
268 [DllImport("rstrtmgr.dll")]
269 static extern int RmEndSession(uint pSessionHandle);
270
271 [DllImport("rstrtmgr.dll")]
272 static extern int RmGetList(uint dwSessionHandle,
273 out uint pnProcInfoNeeded,
274 ref uint pnProcInfo,
275 [In, Out] RM_PROCESS_INFO[] rgAffectedApps,
276 ref uint lpdwRebootReasons);
277 static public List<Process> Locking(string path)
278 {
279 uint handle;
280 string key = Guid.NewGuid().ToString();
281 List<Process> processes = new List<Process>();
282
283 int res = RmStartSession(out handle, 0, key);
284
285 if (res != 0)
286 throw new Exception("Could not begin restart session. Unable to determine file locker.");
287
288 try
289 {
290 const int ERROR_MORE_DATA = 234;
291 uint pnProcInfoNeeded = 0,
292 pnProcInfo = 0,
293 lpdwRebootReasons = RmRebootReasonNone;
294
295 string[] resources = new string[] { path }; // Just checking on one resource.
296
297 res = RmRegisterResources(handle, (uint)resources.Length, resources, 0, null, 0, null);
298
299 if (res != 0)
300 throw new Exception("Could not register resource.");
301
302 //Note: there's a race condition here -- the first call to RmGetList() returns
303 // the total number of process. However, when we call RmGetList() again to get
304 // the actual processes this number may have increased.
305 res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, null, ref lpdwRebootReasons);
306
307 if (res == ERROR_MORE_DATA)
308 {
309 // Create an array to store the process results
310 RM_PROCESS_INFO[] processInfo = new RM_PROCESS_INFO[pnProcInfoNeeded];
311 pnProcInfo = pnProcInfoNeeded;
312
313 // Get the list
314 res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, processInfo, ref lpdwRebootReasons);
315
316 if (res == 0)
317 {
318 processes = new List<Process>((int)pnProcInfo);
319
320 // Enumerate all of the results and add them to the
321 // list to be returned
322 for (int i = 0; i < pnProcInfo; i++)
323 {
324 try
325 {
326 processes.Add(Process.GetProcessById(processInfo[i].Process.dwProcessId));
327 }
328 // catch the error -- in case the process is no longer running
329 catch (ArgumentException) { }
330 }
331 }
332 else
333 throw new Exception("Could not list processes locking resource.");
334 }
335 else if (res != 0)
336 throw new Exception("Could not list processes locking resource. Failed to get size of result.");
337 }
338 finally
339 {
340 RmEndSession(handle);
341 }
342
343 return processes;
344 }
345 }
346}
static List< Process > Locking(string path)
Definition Win32.cs:277
static void RightClick()
Definition Win32.cs:135
static void LeftUp()
Definition Win32.cs:147
static void RightUp()
Definition Win32.cs:151
static void SetCursorPosition(int x, int y)
Definition Win32.cs:159
static void LeftDown()
Definition Win32.cs:139
static void SetCursorPosition(MousePoint point)
Definition Win32.cs:168
static void LeftClick()
Definition Win32.cs:131
static void MouseEvent(MouseEventFlags value)
Definition Win32.cs:188
static void RightDown()
Definition Win32.cs:143
static MousePoint GetCursorPosition()
Definition Win32.cs:177
const int MOUSEEVENTF_HWHEEL
Definition Win32.cs:62
static IntPtr GetForegroundWindow()
static void MouseWheelDown()
MouseWheelDown() simulates a mouse wheel down event.
Definition Win32.cs:73
static bool GetKeyState(System.Windows.Forms.Keys vKey)
Definition Win32.cs:24
static int GetWindowText(IntPtr hWnd, StringBuilder text, int count)
const int MOUSEEVENTF_WHEEL
Definition Win32.cs:59
static void MouseWheelUp()
"MouseWheelUp()" is a function that simulates a mouse wheel up event.
Definition Win32.cs:68
const int MOUSEEVENTF_RIGHTDOWN
Definition Win32.cs:57
const int WM_KEYUP
Definition Win32.cs:16
const int MOUSEEVENTF_MIDDLEDOWN
Definition Win32.cs:54
const int MOUSEEVENTF_MOVE
Definition Win32.cs:56
const int MOUSEEVENTF_XDOWN
Definition Win32.cs:60
static string GetActiveWindowTitle()
Definition Win32.cs:90
const int MOUSEEVENTF_ABSOLUTE
Definition Win32.cs:51
static void SetFocus(string process)
Definition Win32.cs:81
const int WM_KEYDOWN
Definition Win32.cs:15
const int MOUSEEVENTF_RIGHTUP
Definition Win32.cs:58
static void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo)
static short GetAsyncKeyState(System.Windows.Forms.Keys vKey)
const int MOUSEEVENTF_MIDDLEUP
Definition Win32.cs:55
const int MOUSEEVENTF_LEFTUP
Definition Win32.cs:53
const int MOUSEEVENTF_XUP
Definition Win32.cs:61
static bool SetForegroundWindow(IntPtr hWnd)
static bool GetWindowRect(IntPtr hwnd, ref Rect rectangle)
const int MOUSEEVENTF_LEFTDOWN
Definition Win32.cs:52