-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.pde
executable file
·363 lines (322 loc) · 10.8 KB
/
Window.pde
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
359
360
361
362
363
import java.awt.Point;
import java.awt.Rectangle;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.Shell32;
import com.sun.jna.platform.win32.User32;
//The following import throw up errors, but actually work, and are necessary (maybe should investigate).
import com.sun.jna.platform.win32.WinBase.PROCESS_INFORMATION;
import com.sun.jna.platform.win32.WinBase.STARTUPINFO;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.INT_PTR;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.ptr.IntByReference;
/**
* This is a wrapper for the HWND type that is in JNA 4.0.
* Based on a class written by John Henckel, Oct 2014
* which can be found at: https://answers.launchpad.net/sikuli/+question/255504
*/
public class Window
{
public HWND hWnd;
public Window(HWND hwnd)
{
this.hWnd = hwnd;
}
public boolean isNull()
{
return hWnd == null || hWnd.hashCode() == 0;
}
public Window getForegroundWindow()
{
return new Window(User32.INSTANCE.GetForegroundWindow());
}
public void setForeground()
{
User32.INSTANCE.SetForegroundWindow(hWnd);
minimize();
restore();
}
/**
* Flash the window three times
*/
public void flash()
{
WinUser.FLASHWINFO pfwi = new WinUser.FLASHWINFO();
pfwi.cbSize = 20;
pfwi.hWnd = hWnd;
pfwi.dwFlags = 3; // 3 = FLASHW_ALL
pfwi.uCount = 3;
pfwi.dwTimeout = 250;
User32.INSTANCE.FlashWindowEx(pfwi);
sleep(1000);
}
/**
* Returns the class of the window, such as "SWT_Window0" for any SWT application. See AutoIt Window Info Tool
*/
public String getClassName()
{
char[] buffer = new char[2048];
User32.INSTANCE.GetClassName(hWnd, buffer, 1024);
return Native.toString(buffer);
}
public boolean isVisible()
{
return User32.INSTANCE.IsWindowVisible(hWnd);
}
/**
* 3=SW_MAXIMIZE, etc
*/
private void showWindow(int nCmdShow)
{
User32.INSTANCE.ShowWindow(hWnd, nCmdShow);
}
public void minimize()
{
showWindow(6);
}
public void maximize()
{
showWindow(3);
}
/**
* Activate and displays the window in normal mode (not minimized or maximized)
*/
public void restore()
{
showWindow(9);
}
/**
* Return true if the window message queue is idle, false if timeout
*/
public boolean waitForInputIdle(int timeout_ms)
{
IntByReference lpdwProcessId = new IntByReference();
User32.INSTANCE.GetWindowThreadProcessId(hWnd, lpdwProcessId);
return User32.INSTANCE.WaitForInputIdle(new HANDLE(lpdwProcessId.getPointer()), new DWORD(timeout_ms)).intValue() == 0;
}
public void setRectangle(Rectangle rect)
{
User32.INSTANCE.MoveWindow(hWnd, rect.x, rect.y, rect.width, rect.height, true);
}
public String getTitle()
{
char[] buffer = new char[2048];
User32.INSTANCE.GetWindowText(hWnd, buffer, 1024);
return Native.toString(buffer);
}
public int getProcessID()
{
IntByReference processID = new IntByReference();
User32.INSTANCE.GetWindowThreadProcessId(hWnd, processID);
return processID.getValue();
}
public Rectangle getRectangle()
{
RECT rect = new RECT();
User32.INSTANCE.GetWindowRect(hWnd, rect);
return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
}
/**
* In windows the user can customize the border and title bar size.
* @return x = border thickness in pixels, y = x + titlebar height
*/
public Point getClientOffset()
{
return new Point(getBorderSize(), getTitleHeight());
}
/**
* In windows the user can customize the border and title bar size.
* @return titlebar height
*/
public int getTitleHeight()
{
int f = User32.INSTANCE.GetSystemMetrics(33); // 33 = SM_CYSIZEFRAME
return User32.INSTANCE.GetSystemMetrics(4) + f; // 4 = SM_CYCAPTION
}
/**
* In windows the user can customize the border and title bar size.
* @return border thickness in pixels
*/
public int getBorderSize()
{
int f = User32.INSTANCE.GetSystemMetrics(32); // 32 = SM_CXSIZEFRAME
return User32.INSTANCE.GetSystemMetrics(5) + f; // 5 = SM_CXBORDER
}
/**
* Find the FIRST top level window with specified class and title.
* @param className such as "SWT_Window0" or null
* @param title such as "QREADS" or null
* @return first window found, or null if not found.
*/
public Window findWindow(String className, String title)
{
return new Window(User32.INSTANCE.FindWindow(className, title));
}
/**
* Get the next top-level window in Z-order, (from foreground to background).
*
* To iterate all windows, use
*
* for (Window w = getForegroundWindow(); !w.isNull(); w = w.next()) ...
*
* @return
*/
public Window next()
{
return new Window(User32.INSTANCE.GetWindow(hWnd, new DWORD(2))); // 2 = GW_HWNDNEXT
}
/**
* This is used to gather results of EnumWindows
*/
private class WindowList implements WinUser.WNDENUMPROC
{
ArrayList<Window> list = new ArrayList<Window>();
Pattern titlePattern = null;
int processID;
@Override
public boolean callback(HWND hWnd, Pointer data)
{
Window w = new Window(hWnd);
if (titlePattern == null || titlePattern.matcher(w.getTitle()).matches())
{
if (processID == 0 || processID == w.getProcessID())
{
list.add(new Window(hWnd));
if (processID > 0)
{
return false; // if matching processID, only need one result
}
}
}
return true; // keep going
}
// Convert the list into an ordinary array
Window[] toArray()
{
return list.toArray(new Window[list.size()]);
}
}
public Window[] getTopLevelWindows()
{
WindowList result = new WindowList();
User32.INSTANCE.EnumWindows(result, null);
return result.toArray();
}
public Window[] getTopLevelWindows(String titleRegex)
{
WindowList result = new WindowList();
result.titlePattern = Pattern.compile(titleRegex);
User32.INSTANCE.EnumWindows(result, null);
return result.toArray();
}
public Window[] getChildren()
{
WindowList result = new WindowList();
User32.INSTANCE.EnumChildWindows(hWnd, result, null);
return result.toArray();
}
public Window getProcessWindow(int processID)
{
WindowList result = new WindowList();
result.processID = processID;
User32.INSTANCE.EnumWindows(result, null);
return result.list.size() > 0 ? result.list.get(0) : null;
}
/**
* Sleep for milliseconds.
*/
public void sleep(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
private long getProcessHandle(Process process)
{
String n = process.getClass().getName();
if (n.equals("java.lang.ProcessImpl") || n.equals("java.lang.Win32Process"))
{
try
{
Field field = process.getClass().getDeclaredField("handle");
field.setAccessible(true);
return field.getLong(process);
}
catch (Exception e)
{
e.printStackTrace();
}
}
return 0;
}
public int getProcessID(Process process)
{
long id = getProcessHandle(process);
if (id != 0)
{
HANDLE handle = new HANDLE();
handle.setPointer(Pointer.createConstant(id));
return Kernel32.INSTANCE.GetProcessId(handle);
}
return 0;
}
/**
* Runs a program and returns the process
*/
public Process runCommand(List<String> cmdline) throws IOException
{
ProcessBuilder builder = new ProcessBuilder();
builder.command(cmdline).redirectErrorStream(true);
return builder.start();
}
/**
* Runs a program and returns the process
*/
public Process runCommand(String... cmdline) throws IOException
{
return runCommand(Arrays.asList(cmdline));
}
/**
* Runs a program and returns the process id. Note: use runCommand instead of this.
*/
/*
public int createProcess(String program, String args, String currentDirectory)
{
STARTUPINFO startupInfo = new STARTUPINFO(); // input
PROCESS_INFORMATION processInformation = new PROCESS_INFORMATION(); // output
String cmdline = (args == null || args.length() == 0) ? null : program + " " + args;
boolean ok = Kernel32.INSTANCE.CreateProcess(program, cmdline, null, null, false, new DWORD(0), null, currentDirectory, startupInfo, processInformation);
if (!ok) System.out.println("CreateProcess failed err="+Kernel32.INSTANCE.GetLastError());
return processInformation.dwProcessId.intValue();
}
*/
/**
* Note: use runCommand instead of this, if possible.
* Use this to open a program or any kind of file, like pdf, jpeg, html, etc.
* @return true on success.
*/
public boolean shellExecute(String filename, String args, String currentDirectory)
{
String verb = null; // possible values 'open' (or null), 'edit', 'print', etc.
INT_PTR intPtr = Shell32.INSTANCE.ShellExecute(null, verb, filename, args, currentDirectory, 1); // 1=SW_SHOWNORMAL
int rc = intPtr.intValue();
if (rc <= 32) System.out.println("ShellExecute failed err="+rc+" for filename="+filename);
return rc > 32;
}
}