WPF and regular Win32 windows are now supported
Added support for dragging target on WPF windows and building simple xpath statements. Fixed some null results in native library and increased performance of enumerating wpf windows. Still need to tweak filters for picking up Silverlight handles too.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package org.synthuse.commands;
|
||||
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.sql.Timestamp;
|
||||
@@ -9,14 +10,13 @@ import java.util.List;
|
||||
|
||||
import org.synthuse.*;
|
||||
|
||||
import com.sun.jna.platform.win32.WinDef.HWND;
|
||||
|
||||
public class BaseCommand {
|
||||
|
||||
static String WIN_XML = "";
|
||||
static long LAST_UPDATED_XML = 0;
|
||||
|
||||
protected Api api = new Api();
|
||||
protected WpfBridge wpf = new WpfBridge();
|
||||
protected CommandProcessor parentProcessor = null;
|
||||
|
||||
protected int getExecuteErrorCount() {
|
||||
@@ -92,12 +92,12 @@ public class BaseCommand {
|
||||
return cmdResult;
|
||||
}
|
||||
|
||||
public HWND findHandleWithXpath(String xpath) {
|
||||
public WinPtr findHandleWithXpath(String xpath) {
|
||||
return findHandleWithXpath(xpath, false);
|
||||
}
|
||||
|
||||
public HWND findHandleWithXpath(String xpath, boolean ignoreFailedFind) {
|
||||
HWND result = null;
|
||||
public WinPtr findHandleWithXpath(String xpath, boolean ignoreFailedFind) {
|
||||
WinPtr result = new WinPtr();
|
||||
double secondsFromLastUpdate = ((double)(System.nanoTime() - LAST_UPDATED_XML) / 1000000000);
|
||||
if (secondsFromLastUpdate > CommandProcessor.XML_UPDATE_THRESHOLD) { //default 5 second threshold
|
||||
WIN_XML = WindowsEnumeratedXml.getXml();
|
||||
@@ -109,18 +109,34 @@ public class BaseCommand {
|
||||
for(String item: resultList) {
|
||||
if (item.contains("hwnd=")) {
|
||||
List<String> hwndList = WindowsEnumeratedXml.evaluateXpathGetValues(item, "//@hwnd");
|
||||
resultStr = hwndList.get(0);
|
||||
if (hwndList.size() > 0)
|
||||
resultStr = hwndList.get(0); //get first hwnd;
|
||||
}
|
||||
else
|
||||
resultStr = item;
|
||||
break;
|
||||
}
|
||||
result = Api.GetHandleFromString(resultStr);
|
||||
if (result == null && !ignoreFailedFind)
|
||||
if (WinPtr.isWpfRuntimeIdFormat(resultStr))
|
||||
result.runtimeId = resultStr;
|
||||
else {
|
||||
result.hWnd = Api.GetHandleFromString(resultStr);
|
||||
if (!api.user32.IsWindow(result.hWnd))
|
||||
appendError("Error: Failed to located HWND(" + resultStr + ") from : " + xpath);
|
||||
}
|
||||
if (result.isEmpty())
|
||||
appendError("Error: Failed to find window handle matching: " + xpath);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Point getCenterWindowPosition(WinPtr handle) {
|
||||
Point p = null;
|
||||
if (handle.isWin32())
|
||||
p = api.getWindowPosition(handle.hWnd);
|
||||
else
|
||||
p = wpf.getCenterOfElement(handle.runtimeId);
|
||||
return p;
|
||||
}
|
||||
|
||||
public String convertListToString(List<String> listStr, String delimiter) {
|
||||
StringBuilder result = new StringBuilder("");
|
||||
for (String item: listStr) {
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.awt.Toolkit;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.synthuse.*;
|
||||
import com.sun.jna.platform.win32.WinDef.HWND;
|
||||
|
||||
public class MainCommands extends BaseCommand {
|
||||
|
||||
@@ -55,12 +54,12 @@ public class MainCommands extends BaseCommand {
|
||||
long totalAttempts = (long) (CommandProcessor.WAIT_TIMEOUT_THRESHOLD / (CommandProcessor.XML_UPDATE_THRESHOLD * 1000));
|
||||
long attemptCount = 0;
|
||||
String xpath = "/EnumeratedWindows/win[@TEXT='" + WindowsEnumeratedXml.escapeXmlAttributeValue(args[0].toUpperCase()) + "']";
|
||||
HWND handle = findHandleWithXpath(xpath, true);
|
||||
if (handle != null)// first test without a timeout
|
||||
WinPtr handle = findHandleWithXpath(xpath, true);
|
||||
if (!handle.isEmpty())// first test without a timeout
|
||||
return true;
|
||||
while (attemptCount < totalAttempts) {
|
||||
handle = findHandleWithXpath(xpath, true);
|
||||
if (handle != null)
|
||||
if (!handle.isEmpty())
|
||||
break;
|
||||
try {Thread.sleep((long)(CommandProcessor.XML_UPDATE_THRESHOLD * 1000));} catch (Exception e) {e.printStackTrace();}
|
||||
++attemptCount;
|
||||
@@ -76,12 +75,12 @@ public class MainCommands extends BaseCommand {
|
||||
long totalAttempts = (long) (CommandProcessor.WAIT_TIMEOUT_THRESHOLD / (CommandProcessor.XML_UPDATE_THRESHOLD * 1000));
|
||||
long attemptCount = 0;
|
||||
String xpath = "//[@TEXT='" + WindowsEnumeratedXml.escapeXmlAttributeValue(args[0].toUpperCase()) + "']";
|
||||
HWND handle = findHandleWithXpath(xpath, true);
|
||||
if (handle != null)// first test without a timeout
|
||||
WinPtr handle = findHandleWithXpath(xpath, true);
|
||||
if (!handle.isEmpty())// first test without a timeout
|
||||
return true;
|
||||
while (attemptCount < totalAttempts) {
|
||||
handle = findHandleWithXpath(xpath, true);
|
||||
if (handle != null)
|
||||
if (!handle.isEmpty())
|
||||
break;
|
||||
try {Thread.sleep((long)(CommandProcessor.XML_UPDATE_THRESHOLD * 1000));} catch (Exception e) {e.printStackTrace();}
|
||||
++attemptCount;
|
||||
@@ -97,12 +96,12 @@ public class MainCommands extends BaseCommand {
|
||||
long totalAttempts = (long) (CommandProcessor.WAIT_TIMEOUT_THRESHOLD / (CommandProcessor.XML_UPDATE_THRESHOLD * 1000));
|
||||
long attemptCount = 0;
|
||||
String xpath = "//win[@CLASS='" + WindowsEnumeratedXml.escapeXmlAttributeValue(args[0].toUpperCase()) + "']";
|
||||
HWND handle = findHandleWithXpath(xpath, true);
|
||||
if (handle != null)// first test without a timeout
|
||||
WinPtr handle = findHandleWithXpath(xpath, true);
|
||||
if (!handle.isEmpty())// first test without a timeout
|
||||
return true;
|
||||
while (attemptCount < totalAttempts) {
|
||||
handle = findHandleWithXpath(xpath, true);
|
||||
if (handle != null)
|
||||
if (!handle.isEmpty())
|
||||
break;
|
||||
try {Thread.sleep((long)(CommandProcessor.XML_UPDATE_THRESHOLD * 1000));} catch (Exception e) {e.printStackTrace();}
|
||||
++attemptCount;
|
||||
@@ -117,12 +116,12 @@ public class MainCommands extends BaseCommand {
|
||||
return false;
|
||||
long totalAttempts = (long) (CommandProcessor.WAIT_TIMEOUT_THRESHOLD / (CommandProcessor.XML_UPDATE_THRESHOLD * 1000));
|
||||
long attemptCount = 0;
|
||||
HWND handle = findHandleWithXpath(args[0], true);
|
||||
if (handle != null)// first test without a timeout
|
||||
WinPtr handle = findHandleWithXpath(args[0], true);
|
||||
if (!handle.isEmpty())// first test without a timeout
|
||||
return true;
|
||||
while (attemptCount < totalAttempts) {
|
||||
handle = findHandleWithXpath(args[0], true);
|
||||
if (handle != null)
|
||||
if (!handle.isEmpty())
|
||||
break;
|
||||
try {Thread.sleep((long)(CommandProcessor.XML_UPDATE_THRESHOLD * 1000));} catch (Exception e) {e.printStackTrace();}
|
||||
++attemptCount;
|
||||
|
||||
@@ -4,8 +4,6 @@ import java.awt.Point;
|
||||
|
||||
import org.synthuse.*;
|
||||
|
||||
import com.sun.jna.platform.win32.WinDef.HWND;
|
||||
|
||||
public class MouseCommands extends BaseCommand {
|
||||
|
||||
public MouseCommands(CommandProcessor commandProcessor) {
|
||||
@@ -15,10 +13,12 @@ public class MouseCommands extends BaseCommand {
|
||||
public boolean cmdClick(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
//System.out.println("cmdClick1: " + args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
Point p = api.getWindowPosition(handle);
|
||||
Point p = getCenterWindowPosition(handle);
|
||||
//System.out.println("cmdClick3: " + p.x + "," + p.y);
|
||||
RobotMacro.mouseMove(p.x + parentProcessor.targetOffset.x, p.y + parentProcessor.targetOffset.y);
|
||||
RobotMacro.leftClickMouse();
|
||||
return true;
|
||||
@@ -27,10 +27,10 @@ public class MouseCommands extends BaseCommand {
|
||||
public boolean cmdDoubleClick(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
Point p = api.getWindowPosition(handle);
|
||||
Point p = getCenterWindowPosition(handle);
|
||||
RobotMacro.mouseMove(p.x + parentProcessor.targetOffset.x, p.y + parentProcessor.targetOffset.y);
|
||||
RobotMacro.doubleClickMouse();
|
||||
return true;
|
||||
@@ -39,10 +39,10 @@ public class MouseCommands extends BaseCommand {
|
||||
public boolean cmdRightClick(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
Point p = api.getWindowPosition(handle);
|
||||
Point p = getCenterWindowPosition(handle);
|
||||
RobotMacro.mouseMove(p.x + parentProcessor.targetOffset.x, p.y + parentProcessor.targetOffset.y);
|
||||
RobotMacro.rightClickMouse();
|
||||
return true;
|
||||
@@ -71,10 +71,10 @@ public class MouseCommands extends BaseCommand {
|
||||
public boolean cmdMouseMove(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
Point p = api.getWindowPosition(handle);
|
||||
Point p = getCenterWindowPosition(handle);
|
||||
RobotMacro.mouseMove(p.x + parentProcessor.targetOffset.x, p.y + parentProcessor.targetOffset.y);
|
||||
//System.out.println("point " + p.x + "," + p.y);
|
||||
return true;
|
||||
@@ -101,30 +101,30 @@ public class MouseCommands extends BaseCommand {
|
||||
public boolean cmdWinClick(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
api.sendClick(handle);
|
||||
api.sendClick(handle.hWnd);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean cmdWinDoubleClick(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
api.sendDoubleClick(handle);
|
||||
api.sendDoubleClick(handle.hWnd);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean cmdWinRightClick(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
api.sendRightClick(handle);
|
||||
api.sendRightClick(handle.hWnd);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package org.synthuse.commands;
|
||||
|
||||
import org.synthuse.*;
|
||||
|
||||
import com.sun.jna.platform.win32.WinDef.HWND;
|
||||
|
||||
public class WindowsCommands extends BaseCommand {
|
||||
|
||||
public WindowsCommands(CommandProcessor cp) {
|
||||
@@ -13,10 +11,10 @@ public class WindowsCommands extends BaseCommand {
|
||||
public boolean cmdWindowFocus(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
api.activateWindow(handle);
|
||||
api.activateWindow(handle.hWnd);
|
||||
//api.showWindow(handle);
|
||||
return true;
|
||||
}
|
||||
@@ -24,60 +22,60 @@ public class WindowsCommands extends BaseCommand {
|
||||
public boolean cmdWindowMinimize(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
api.minimizeWindow(handle);
|
||||
api.minimizeWindow(handle.hWnd);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean cmdWindowMaximize(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
api.maximizeWindow(handle);
|
||||
api.maximizeWindow(handle.hWnd);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean cmdWindowRestore(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
api.restoreWindow(handle);
|
||||
api.restoreWindow(handle.hWnd);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean cmdWindowHide(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
api.hideWindow(handle);
|
||||
api.hideWindow(handle.hWnd);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean cmdWindowShow(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
api.showWindow(handle);
|
||||
api.showWindow(handle.hWnd);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean cmdWindowSwitchToThis(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
api.switchToThisWindow(handle, true);
|
||||
api.switchToThisWindow(handle.hWnd, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -85,29 +83,29 @@ public class WindowsCommands extends BaseCommand {
|
||||
public boolean cmdWindowClose(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
api.closeWindow(handle);
|
||||
api.closeWindow(handle.hWnd);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean cmdSetText(String[] args) {
|
||||
if (!checkArgumentLength(args, 2))
|
||||
return false;
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return false;
|
||||
api.sendWmSetText(handle, args[1]);
|
||||
api.sendWmSetText(handle.hWnd, args[1]);
|
||||
return true;
|
||||
}
|
||||
|
||||
public String cmdGetText(String[] args) {
|
||||
if (!checkArgumentLength(args, 1))
|
||||
return "";
|
||||
HWND handle = findHandleWithXpath(args[0]);
|
||||
if (handle == null)
|
||||
WinPtr handle = findHandleWithXpath(args[0]);
|
||||
if (handle.isEmpty())
|
||||
return "";
|
||||
return api.sendWmGetText(handle);
|
||||
return api.sendWmGetText(handle.hWnd);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user