Skip to content

Commit

Permalink
refactor: adjust user32 hwnd
Browse files Browse the repository at this point in the history
  • Loading branch information
isHarryh committed Sep 15, 2024
1 parent e6d5ebc commit f8279f5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 42 deletions.
3 changes: 1 addition & 2 deletions core/src/cn/harryh/arkpets/ArkPets.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ public void resize(int x, int y) {
@Override
public void dispose() {
Logger.info("App", "Dispose");
HWndCtrlFactory.free();
}

/* INTERFACES */
Expand Down Expand Up @@ -439,7 +438,7 @@ private void promiseToolwindowStyle(int maxRetries) {
isToolwindowStyle = true;
break;
}
hWndMine.setForeground();
hWndMine.setForeground();
}
}
}
Expand Down
72 changes: 33 additions & 39 deletions core/src/cn/harryh/arkpets/platform/User32HWndCtrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

public class User32HWndCtrl extends HWndCtrl {
protected final HWND hWnd;
protected final Pointer windowPointer;

public static final int WS_EX_TOPMOST = 0x00000008;
public static final int WS_EX_TRANSPARENT = 0x00000020;
Expand All @@ -42,7 +41,6 @@ public class User32HWndCtrl extends HWndCtrl {
protected User32HWndCtrl(HWND hWnd) {
super(getWindowText(hWnd), getWindowRect(hWnd));
this.hWnd = hWnd;
windowPointer = getWindowIdx(hWnd);
}

/** Finds a window.
Expand All @@ -64,7 +62,7 @@ public boolean isForeground() {

@Override
public boolean isVisible() {
return visible(hWnd);
return isVisible(hWnd);
}

@Override
Expand All @@ -77,14 +75,6 @@ public HWndCtrl updated() {
return new User32HWndCtrl(hWnd);
}

/** Gets the value of the window's extended styles.
* @return EX_STYLE value.
* @see WinUser
*/
public int getWindowExStyle() {
return User32.INSTANCE.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE);
}

@Override
public void setForeground() {
User32.INSTANCE.SetForegroundWindow(hWnd);
Expand All @@ -97,14 +87,6 @@ public void setWindowAlpha(float alpha) {
User32.INSTANCE.SetLayeredWindowAttributes(hWnd, 0, byteAlpha, User32.LWA_ALPHA);
}

/** Sets the window's extended styles.
* @param newLong New EX_STYLE value.
* @see WinUser
*/
public void setWindowExStyle(int newLong) {
User32.INSTANCE.SetWindowLong(hWnd, WinUser.GWL_EXSTYLE, newLong);
}

@Override
public void setWindowPosition(HWndCtrl insertAfter, int x, int y, int w, int h) {
User32.INSTANCE.SetWindowPos(hWnd, ((User32HWndCtrl)insertAfter).hWnd, x, y, w, h, WinUser.SWP_NOACTIVATE);
Expand Down Expand Up @@ -171,7 +153,7 @@ public void sendMouseEvent(MouseEvent msg, int x, int y) {
public static ArrayList<User32HWndCtrl> getWindowList(boolean only_visible) {
ArrayList<User32HWndCtrl> windowList = new ArrayList<>();
User32.INSTANCE.EnumWindows((hWnd, arg1) -> {
if (User32.INSTANCE.IsWindow(hWnd) && (!only_visible || visible(hWnd)))
if (User32.INSTANCE.IsWindow(hWnd) && (!only_visible || isVisible(hWnd)))
windowList.add(new User32HWndCtrl(hWnd));
return true;
}, null);
Expand All @@ -186,22 +168,50 @@ public static ArrayList<User32HWndCtrl> getWindowList(boolean only_visible) {
public static ArrayList<User32HWndCtrl> getWindowList(boolean only_visible, long exclude_ws_ex) {
ArrayList<User32HWndCtrl> windowList = new ArrayList<>();
User32.INSTANCE.EnumWindows((hWnd, arg1) -> {
if (User32.INSTANCE.IsWindow(hWnd) && (!only_visible || visible(hWnd))
if (User32.INSTANCE.IsWindow(hWnd) && (!only_visible || isVisible(hWnd))
&& (User32.INSTANCE.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE) & exclude_ws_ex) != exclude_ws_ex)
windowList.add(new User32HWndCtrl(hWnd));
return true;
}, null);
return windowList;
}

/** Gets the value of the window's extended styles.
* @return EX_STYLE value.
* @see WinUser
*/
protected int getWindowExStyle() {
return User32.INSTANCE.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE);
}

/** Sets the window's extended styles.
* @param newLong New EX_STYLE value.
* @see WinUser
*/
protected void setWindowExStyle(int newLong) {
User32.INSTANCE.SetWindowLong(hWnd, WinUser.GWL_EXSTYLE, newLong);
}

/** Gets the topmost window.
* @return The topmost window's HWndCtrl.
*/
public static User32HWndCtrl getTopmost() {
protected static User32HWndCtrl getTopmostWindow() {
return new User32HWndCtrl(new HWND(Pointer.createConstant(-1)));
}

private static boolean visible(HWND hWnd) {
protected static String getWindowText(HWND hWnd) {
char[] text = new char[1024];
User32.INSTANCE.GetWindowText(hWnd, text, 1024);
return Native.toString(text);
}

protected static WindowRect getWindowRect(HWND hWnd) {
RECT rect = new RECT();
User32.INSTANCE.GetWindowRect(hWnd, rect);
return new WindowRect(rect.top, rect.bottom, rect.left, rect.right);
}

protected static boolean isVisible(HWND hWnd) {
try {
if (!User32.INSTANCE.IsWindowVisible(hWnd) || !User32.INSTANCE.IsWindowEnabled(hWnd))
return false;
Expand All @@ -214,22 +224,6 @@ private static boolean visible(HWND hWnd) {
return true;
}

private static Pointer getWindowIdx(HWND hWnd) {
return hWnd.getPointer();
}

public static String getWindowText(HWND hWnd) {
char[] text = new char[1024];
User32.INSTANCE.GetWindowText(hWnd, text, 1024);
return Native.toString(text);
}

public static WindowRect getWindowRect(HWND hWnd) {
RECT rect = new RECT();
User32.INSTANCE.GetWindowRect(hWnd, rect);
return new WindowRect(rect.top, rect.bottom, rect.left, rect.right);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
2 changes: 1 addition & 1 deletion core/src/cn/harryh/arkpets/platform/WindowSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static List<? extends HWndCtrl> getWindowList(boolean onlyVisible) {
public static HWndCtrl getTopmostWindow() {
switch (PLATFORM) {
case USER32 -> {
return User32HWndCtrl.getTopmost();
return User32HWndCtrl.getTopmostWindow();
}
default -> {
return new NullHWndCtrl();
Expand Down
2 changes: 2 additions & 0 deletions desktop/src/cn/harryh/arkpets/EmbeddedLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ public void invoke(int error, long description) {
// Instantiate the App
Lwjgl3Application app = new Lwjgl3Application(new ArkPets(TITLE), config);
} catch (Exception e) {
WindowSystem.free();
Logger.error("System", "A fatal error occurs in the runtime of Lwjgl3Application, details see below.", e);
System.exit(-1);
}
WindowSystem.free();
Logger.info("System", "Exited from EmbeddedLauncher successfully");
System.exit(0);
}
Expand Down

0 comments on commit f8279f5

Please sign in to comment.