Reorganise some remaining code
This commit is contained in:
@@ -9,12 +9,15 @@ package org.synthuse;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.synthuse.interfaces.Gdi32Ex;
|
||||
import org.synthuse.interfaces.Kernel32Ex;
|
||||
import org.synthuse.interfaces.PsapiEx;
|
||||
import org.synthuse.interfaces.User32Ex;
|
||||
import org.synthuse.objects.LVITEM_VISTA;
|
||||
import org.synthuse.objects.MenuItem;
|
||||
|
||||
import com.sun.jna.Native;
|
||||
import com.sun.jna.Pointer;
|
||||
@@ -27,7 +30,6 @@ import com.sun.jna.platform.win32.WinDef.HMENU;
|
||||
import com.sun.jna.platform.win32.WinDef.HPEN;
|
||||
import com.sun.jna.platform.win32.WinDef.HWND;
|
||||
import com.sun.jna.platform.win32.WinDef.LPARAM;
|
||||
import com.sun.jna.platform.win32.WinDef.LRESULT;
|
||||
import com.sun.jna.platform.win32.WinDef.RECT;
|
||||
import com.sun.jna.platform.win32.WinDef.WPARAM;
|
||||
import com.sun.jna.platform.win32.WinNT.HANDLE;
|
||||
@@ -36,8 +38,6 @@ import com.sun.jna.platform.win32.WinReg;
|
||||
import com.sun.jna.platform.win32.WinUser;
|
||||
import com.sun.jna.ptr.IntByReference;
|
||||
import com.sun.jna.ptr.PointerByReference;
|
||||
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
|
||||
import com.sun.jna.win32.W32APIOptions;
|
||||
|
||||
public class Api {
|
||||
public static boolean EXACT = false;
|
||||
@@ -166,6 +166,8 @@ public class Api {
|
||||
public PsapiEx psapi;
|
||||
public Kernel32Ex kernel32;
|
||||
|
||||
protected HashMap<HWND, HMENU> windowMap;
|
||||
protected HashMap<MenuItem, WPARAM> menuItemMap;
|
||||
protected HWND hWndFound;
|
||||
|
||||
public static final int POINT_Y(long i) {
|
||||
@@ -180,28 +182,12 @@ public class Api {
|
||||
return ((long) (((short) ((int) (low) & 0xffff)) | ((int) ((short) ((int) (high) & 0xffff))) << 16));
|
||||
}
|
||||
|
||||
interface WNDPROC extends StdCallCallback {
|
||||
|
||||
LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);
|
||||
|
||||
}
|
||||
|
||||
public interface Gdi32Ex extends W32APIOptions {
|
||||
Gdi32Ex instance = (Gdi32Ex) Native.loadLibrary("gdi32", Gdi32Ex.class, DEFAULT_OPTIONS);
|
||||
|
||||
HANDLE SelectObject(HDC hdc, HANDLE hgdiobj);
|
||||
|
||||
HANDLE GetStockObject(int fnObject);
|
||||
|
||||
boolean Rectangle(HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
|
||||
|
||||
HPEN CreatePen(int fnPenStyle, int nWidth, int crColor);
|
||||
}
|
||||
|
||||
public Api() {
|
||||
user32 = User32Ex.instance;
|
||||
psapi = PsapiEx.instance;
|
||||
kernel32 = Kernel32Ex.instance;
|
||||
windowMap = new HashMap<HWND, HMENU>();
|
||||
menuItemMap = new HashMap<MenuItem, WPARAM>();
|
||||
}
|
||||
|
||||
public static Long GetHandleAsLong(HWND hWnd) {
|
||||
@@ -702,4 +688,45 @@ public class Api {
|
||||
}, null);
|
||||
return hwndList.toArray(new HWND[0]);
|
||||
}
|
||||
|
||||
public boolean activateItem(MenuItem menuItem) throws Exception {
|
||||
HWND hWnd = menuItem.hWnd;
|
||||
HMENU hMenu = menuItem.hMenu;
|
||||
String[] path = menuItem.path;
|
||||
boolean exact = menuItem.exact;
|
||||
WPARAM wParam;
|
||||
if (menuItemMap.containsKey(menuItem)) {
|
||||
wParam = menuItemMap.get(menuItem);
|
||||
} else {
|
||||
wParam = loadMenuItem(hWnd, hMenu, exact, path);
|
||||
}
|
||||
return user32.PostMessage(hWnd, Api.WM_COMMAND, wParam, null).intValue() > 0;
|
||||
}
|
||||
|
||||
public boolean activateItem(HWND hWnd, boolean exact, String... path) throws Exception {
|
||||
MenuItem menuItem = loadMenuItem(hWnd, exact, path);
|
||||
return activateItem(menuItem);
|
||||
}
|
||||
|
||||
public MenuItem loadMenuItem(HWND hWnd, boolean exact, String... path) throws Exception {
|
||||
HMENU hMenu;
|
||||
if (windowMap.containsKey(hWnd)) {
|
||||
hMenu = windowMap.get(hWnd);
|
||||
} else {
|
||||
hMenu = user32.GetMenu(hWnd);
|
||||
windowMap.put(hWnd, hMenu);
|
||||
}
|
||||
MenuItem menuItem = new MenuItem(hWnd, hMenu, path);
|
||||
WPARAM wParam = loadMenuItem(hWnd, hMenu, exact, path);
|
||||
menuItemMap.put(menuItem, wParam);
|
||||
return menuItem;
|
||||
}
|
||||
|
||||
protected WPARAM loadMenuItem(MenuItem menuItem) throws Exception {
|
||||
HWND hWnd = menuItem.hWnd;
|
||||
HMENU hMenu = menuItem.hMenu;
|
||||
String[] path = menuItem.path;
|
||||
boolean exact = menuItem.exact;
|
||||
return loadMenuItem(hWnd, hMenu, exact, path);
|
||||
}
|
||||
}
|
||||
|
||||
19
src/main/java/org/synthuse/interfaces/Gdi32Ex.java
Normal file
19
src/main/java/org/synthuse/interfaces/Gdi32Ex.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package org.synthuse.interfaces;
|
||||
|
||||
import com.sun.jna.Native;
|
||||
import com.sun.jna.platform.win32.WinDef.HDC;
|
||||
import com.sun.jna.platform.win32.WinDef.HPEN;
|
||||
import com.sun.jna.platform.win32.WinNT.HANDLE;
|
||||
import com.sun.jna.win32.W32APIOptions;
|
||||
|
||||
public interface Gdi32Ex extends W32APIOptions {
|
||||
Gdi32Ex instance = (Gdi32Ex) Native.loadLibrary("gdi32", Gdi32Ex.class, DEFAULT_OPTIONS);
|
||||
|
||||
HANDLE SelectObject(HDC hdc, HANDLE hgdiobj);
|
||||
|
||||
HANDLE GetStockObject(int fnObject);
|
||||
|
||||
boolean Rectangle(HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
|
||||
|
||||
HPEN CreatePen(int fnPenStyle, int nWidth, int crColor);
|
||||
}
|
||||
11
src/main/java/org/synthuse/interfaces/WNDPROC.java
Normal file
11
src/main/java/org/synthuse/interfaces/WNDPROC.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package org.synthuse.interfaces;
|
||||
|
||||
import com.sun.jna.platform.win32.WinDef.HWND;
|
||||
import com.sun.jna.platform.win32.WinDef.LPARAM;
|
||||
import com.sun.jna.platform.win32.WinDef.LRESULT;
|
||||
import com.sun.jna.platform.win32.WinDef.WPARAM;
|
||||
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
|
||||
|
||||
interface WNDPROC extends StdCallCallback {
|
||||
LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);
|
||||
}
|
||||
@@ -5,22 +5,16 @@ import java.util.Random;
|
||||
import org.synthuse.Api;
|
||||
import org.synthuse.objects.MenuItem;
|
||||
|
||||
import com.sun.jna.platform.win32.WinDef.HMENU;
|
||||
import com.sun.jna.platform.win32.WinDef.HWND;
|
||||
import com.sun.jna.platform.win32.WinDef.WPARAM;
|
||||
|
||||
public class Test {
|
||||
protected Api api;
|
||||
protected HWND hWndFound;
|
||||
protected HashMap<HWND, HMENU> windowMap;
|
||||
protected HashMap<MenuItem, WPARAM> menuItemMap;
|
||||
|
||||
protected HashMap<Slider, HashMap<Amount, MenuItem>> sliderMap;
|
||||
protected HashMap<Slider, HWND> valueMap;
|
||||
|
||||
public Test() {
|
||||
api = new Api();
|
||||
windowMap = new HashMap<HWND, HMENU>();
|
||||
menuItemMap = new HashMap<MenuItem, WPARAM>();
|
||||
sliderMap = new HashMap<Slider, HashMap<Amount, MenuItem>>();
|
||||
valueMap = new HashMap<Slider, HWND>();
|
||||
}
|
||||
@@ -34,15 +28,15 @@ public class Test {
|
||||
for (int k = 0; k < 5; ++k) {
|
||||
Slider slider = Slider.values()[new Random().nextInt(Slider.values().length)];
|
||||
for (int j = 0; j < 5; ++j) {
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
test.moveSlider(slider, Amount.DECREASE_MUCH);
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
test.moveSlider(slider, Amount.INCREASE_LITTLE);
|
||||
System.out.println(test.getValue(slider));
|
||||
Thread.sleep(100);
|
||||
Thread.sleep(200);
|
||||
}
|
||||
Thread.sleep(200);
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
test.moveSlider(slider, Amount.INCREASE_MUCH);
|
||||
Thread.sleep(100);
|
||||
Thread.sleep(400);
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
test.moveSlider(slider, Amount.INCREASE_LITTLE);
|
||||
Thread.sleep(200);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,7 +44,7 @@ public class Test {
|
||||
|
||||
protected void moveSlider(Slider slider, Amount amount) throws Exception {
|
||||
MenuItem menuItem = sliderMap.get(slider).get(amount);
|
||||
activateItem(menuItem);
|
||||
api.activateItem(menuItem);
|
||||
}
|
||||
|
||||
protected float getValue(Slider slider) {
|
||||
@@ -77,7 +71,7 @@ public class Test {
|
||||
for (Amount amount : Amount.values()) {
|
||||
String label = slider.getLabel(amount);
|
||||
path[2] = String.format(" %s", label);
|
||||
MenuItem menuItem = loadMenuItem(hWndTopWindow, true, path);
|
||||
MenuItem menuItem = api.loadMenuItem(hWndTopWindow, true, path);
|
||||
amountMap.put(amount, menuItem);
|
||||
}
|
||||
sliderMap.put(slider, amountMap);
|
||||
@@ -115,44 +109,5 @@ public class Test {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean activateItem(MenuItem menuItem) throws Exception {
|
||||
HWND hWnd = menuItem.hWnd;
|
||||
HMENU hMenu = menuItem.hMenu;
|
||||
String[] path = menuItem.path;
|
||||
boolean exact = menuItem.exact;
|
||||
WPARAM wParam;
|
||||
if (menuItemMap.containsKey(menuItem)) {
|
||||
wParam = menuItemMap.get(menuItem);
|
||||
} else {
|
||||
wParam = api.loadMenuItem(hWnd, hMenu, exact, path);
|
||||
}
|
||||
return api.user32.PostMessage(hWnd, Api.WM_COMMAND, wParam, null).intValue() > 0;
|
||||
}
|
||||
|
||||
public boolean activateItem(HWND hWnd, boolean exact, String... path) throws Exception {
|
||||
MenuItem menuItem = loadMenuItem(hWnd, exact, path);
|
||||
return activateItem(menuItem);
|
||||
}
|
||||
|
||||
protected MenuItem loadMenuItem(HWND hWnd, boolean exact, String... path) throws Exception {
|
||||
HMENU hMenu;
|
||||
if (windowMap.containsKey(hWnd)) {
|
||||
hMenu = windowMap.get(hWnd);
|
||||
} else {
|
||||
hMenu = api.user32.GetMenu(hWnd);
|
||||
windowMap.put(hWnd, hMenu);
|
||||
}
|
||||
MenuItem menuItem = new MenuItem(hWnd, hMenu, path);
|
||||
WPARAM wParam = api.loadMenuItem(hWnd, hMenu, exact, path);
|
||||
menuItemMap.put(menuItem, wParam);
|
||||
return menuItem;
|
||||
}
|
||||
|
||||
protected WPARAM loadMenuItem(MenuItem menuItem) throws Exception {
|
||||
HWND hWnd = menuItem.hWnd;
|
||||
HMENU hMenu = menuItem.hMenu;
|
||||
String[] path = menuItem.path;
|
||||
boolean exact = menuItem.exact;
|
||||
return api.loadMenuItem(hWnd, hMenu, exact, path);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user