BioCore  5.0.0
A Windows .NET microscopy library and application for viewing, editing, and annotating images.
Loading...
Searching...
No Matches
Win32.cs
Go to the documentation of this file.
1using System.Diagnostics;
2using System.Runtime.InteropServices;
3using System.Text;
4
5namespace BioCore
6{
7 public class Win32
8 {
9 [DllImport("User32.dll")]
10 public static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
11 public const int WM_KEYDOWN = 0x100;
12 public const int WM_KEYUP = 0x101;
20 public static bool GetKeyState(System.Windows.Forms.Keys vKey)
21 {
22 int si = (int)GetAsyncKeyState(vKey);
23 if (si == 0)
24 return false;
25 else
26 return true;
27 }
28
29 [DllImport("user32.dll")]
30 public static extern IntPtr GetForegroundWindow();
31
32 [DllImport("user32.dll")]
33 public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
34 [DllImport("user32.dll")]
35 public static extern bool SetForegroundWindow(IntPtr hWnd);
36 [DllImport("user32.dll")]
37 public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
38
39 [StructLayout(LayoutKind.Sequential)]
40 /* A struct that is used to get the position of the window. */
41 public struct Rect
42 {
43 public int X; // x position of upper-left corner
44 public int Y; // y position of upper-left corner
45 public int Right; // x position of lower-right corner
46 public int Bottom; // y position of lower-right corner
47 }
48 public const int MOUSEEVENTF_ABSOLUTE = 0x8000;
49 public const int MOUSEEVENTF_LEFTDOWN = 0x0002;//The left button is down.
50 public const int MOUSEEVENTF_LEFTUP = 0x0004;//The left button is up.
51 public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;//The middle button is down.
52 public const int MOUSEEVENTF_MIDDLEUP = 0x0040;//The middle button is up.
53 public const int MOUSEEVENTF_MOVE = 0x0001;//Movement occurred.
54 public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;//The right button is down.
55 public const int MOUSEEVENTF_RIGHTUP = 0x0010;//The right button is up.
56 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
57 public const int MOUSEEVENTF_XDOWN = 0x0080;//An X button was pressed.
58 public const int MOUSEEVENTF_XUP = 0x0100;//An X button was released.
59 public const int MOUSEEVENTF_HWHEEL = 0x01000; //The wheel button is tilted.
60
67 [DllImport("user32.dll")]
68 public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
69
71 public static void MouseWheelUp()
72 {
73 mouse_event(MOUSEEVENTF_WHEEL, 0, 0, 120, 0);
74 }
76 public static void MouseWheelDown()
77 {
78 mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -120, 0);
79 }
84 public static void SetFocus(string process)
85 {
86 Process pr = Process.GetProcessesByName(process)[0];
87 SetForegroundWindow(pr.MainWindowHandle);
88 }
93 public static bool RunningOnWine()
94 {
95 //We see if winlogon is running indicating if we are on windows
96 Process[] pr = Process.GetProcessesByName("winlogon");
97 if (pr.Length != 0)
98 return true;
99 else
100 return false;
101 }
105 public static string GetActiveWindowTitle()
106 {
107 const int nChars = 256;
108 StringBuilder Buff = new StringBuilder(nChars);
109 IntPtr handle = GetForegroundWindow();
110
111 if (GetWindowText(handle, Buff, nChars) > 0)
112 {
113 return Buff.ToString();
114 }
115 return null;
116 }
117
118 }
119 public class MouseOperations
120 {
121 [Flags]
122 /* A enum that is used to define the mouse events. */
123 public enum MouseEventFlags
124 {
125 LeftDown = 0x00000002,
126 LeftUp = 0x00000004,
127 MiddleDown = 0x00000020,
128 MiddleUp = 0x00000040,
129 Move = 0x00000001,
130 Absolute = 0x00008000,
131 RightDown = 0x00000008,
132 RightUp = 0x00000010
133 }
134
140 [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
141 [return: MarshalAs(UnmanagedType.Bool)]
142 private static extern bool SetCursorPos(int x, int y);
143
149 [DllImport("user32.dll")]
150 [return: MarshalAs(UnmanagedType.Bool)]
151 private static extern bool GetCursorPos(out MousePoint lpMousePoint);
152
165 [DllImport("user32.dll")]
166 private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
167
170 public static void LeftClick()
171 {
172 MouseEvent(MouseEventFlags.LeftDown | MouseEventFlags.LeftUp);
173 }
176 public static void RightClick()
177 {
178 MouseEvent(MouseEventFlags.RightDown | MouseEventFlags.RightUp);
179 }
181 public static void LeftDown()
182 {
183 MouseEvent(MouseEventFlags.LeftDown);
184 }
186 public static void RightDown()
187 {
188 MouseEvent(MouseEventFlags.RightDown);
189 }
192 public static void LeftUp()
193 {
195 }
198 public static void RightUp()
199 {
201 }
206 public static void SetCursorPosition(int x, int y)
207 {
208 SetCursorPos(x, y);
209 }
210
215 public static void SetCursorPosition(MousePoint point)
216 {
217 SetCursorPos(point.X, point.Y);
218 }
219
224 {
225 MousePoint currentMousePoint;
226 var gotPoint = GetCursorPos(out currentMousePoint);
227 if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
228 return currentMousePoint;
229 }
230
235 public static void MouseEvent(MouseEventFlags value)
236 {
237 MousePoint position = GetCursorPosition();
238
239 mouse_event
240 ((int)value,
241 position.X,
242 position.Y,
243 0,
244 0)
245 ;
246 }
247
248 [StructLayout(LayoutKind.Sequential)]
249 /* A struct that contains the x and y coordinates of the mouse. */
250 public struct MousePoint
251 {
252 public int X;
253 public int Y;
254
255 public MousePoint(int x, int y)
256 {
257 X = x;
258 Y = y;
259 }
260 }
261 }
262}
static MousePoint GetCursorPosition()
Definition Win32.cs:223
static void LeftUp()
Definition Win32.cs:192
static void MouseEvent(MouseEventFlags value)
Definition Win32.cs:235
static void LeftClick()
Definition Win32.cs:170
static void RightDown()
It sends a message to the operating system that the right mouse button has been pressed.
Definition Win32.cs:186
static void RightClick()
Definition Win32.cs:176
static void LeftDown()
The function takes the mouse event flag and calls the mouse event function.
Definition Win32.cs:181
static void RightUp()
Definition Win32.cs:198
static void SetCursorPosition(MousePoint point)
Definition Win32.cs:215
static void SetCursorPosition(int x, int y)
Definition Win32.cs:206
const int MOUSEEVENTF_ABSOLUTE
Definition Win32.cs:48
const int MOUSEEVENTF_RIGHTDOWN
Definition Win32.cs:54
const int MOUSEEVENTF_HWHEEL
Definition Win32.cs:59
const int WM_KEYDOWN
Definition Win32.cs:11
static short GetAsyncKeyState(System.Windows.Forms.Keys vKey)
const int WM_KEYUP
Definition Win32.cs:12
const int MOUSEEVENTF_RIGHTUP
Definition Win32.cs:55
const int MOUSEEVENTF_MIDDLEUP
Definition Win32.cs:52
static bool GetKeyState(System.Windows.Forms.Keys vKey)
Definition Win32.cs:20
static int GetWindowText(IntPtr hWnd, StringBuilder text, int count)
static string GetActiveWindowTitle()
Definition Win32.cs:105
const int MOUSEEVENTF_XUP
Definition Win32.cs:58
static bool RunningOnWine()
Definition Win32.cs:93
const int MOUSEEVENTF_XDOWN
Definition Win32.cs:57
const int MOUSEEVENTF_MOVE
Definition Win32.cs:53
static bool GetWindowRect(IntPtr hwnd, ref Rect rectangle)
const int MOUSEEVENTF_LEFTDOWN
Definition Win32.cs:49
const int MOUSEEVENTF_MIDDLEDOWN
Definition Win32.cs:51
const int MOUSEEVENTF_WHEEL
Definition Win32.cs:56
static void MouseWheelDown()
MouseWheelDown() simulates a mouse wheel down event.
Definition Win32.cs:76
static void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo)
static IntPtr GetForegroundWindow()
static void SetFocus(string process)
Definition Win32.cs:84
static bool SetForegroundWindow(IntPtr hWnd)
static void MouseWheelUp()
"MouseWheelUp()" is a function that simulates a mouse wheel up event.
Definition Win32.cs:71
const int MOUSEEVENTF_LEFTUP
Definition Win32.cs:50