前言

笔者上班略显枯燥,想闲暇时间搬个砖(ps:DNF) 但DNF窗体太大了。
笔者灵机一动,决定操作窗体大小,在不影响游戏内存的情况下实现上班搬砖。
另外支持了老板键隐藏 CTRL + X
嘿嘿🤭嘿…直接上代码。

依赖

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.9.0</version>
</dependency>


<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.9.0</version>
</dependency>

代码

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
package cc.yybk;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinUser;

import java.util.Scanner;
import java.util.logging.Logger;

public class WindowManipulator {

public static void main(String[] args) {
while (true) {
System.out.println("INFO -- 请输入窗口标题");
Scanner scanner = new Scanner(System.in);
if (scanner.hasNext()) {
final User32 user32 = User32.INSTANCE;
String windowTitle = scanner.next(); // 设置需要获取窗口句柄的应用程序窗口标题
HWND hwnd = user32.FindWindow(null, windowTitle);
if (hwnd != null) {
// 获取窗口大小
RECT rect = new RECT();
user32.GetWindowRect(hwnd, rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
System.out.println("INFO -- 该窗口原来的大小为:{" + width + "X" + height + "}");
Integer newWidth = 0;
Integer newHeight = 0;
// 修改窗口大小
//宽度交互
do {
System.out.println("INFO -- 输入你想修改的宽度(只支持整数)");
Scanner sw = new Scanner(System.in);
if (sw.hasNext()) {
newWidth = sw.nextInt();
break;
} else {
System.out.println("WARN -- 输入合法的宽度,不要违规操作,重新输入 = =、");
}
} while (true);
//高度交互
do {
System.out.println("INFO -- 输入你想修改的高度(只支持整数)");
Scanner sh = new Scanner(System.in);
if (sh.hasNext()) {
newHeight = sh.nextInt();
break;
} else {
System.out.println("WARN -- 输入合法的高度,不要违规操作,重新输入 = =、");
}
} while (true);
user32.MoveWindow(hwnd, rect.left, rect.top, newWidth, newHeight, true);
System.out.println("INFO -- 成功修改窗口!");
System.out.println("INFO -- 请勿关闭本程序!否则会失效!");
//开一个线程监听老板键
Thread thread = new Thread(() -> {
final int VK_X = 0x58;
final int MOD_CONTROL = 0x0002;
boolean isShow = true; //是否显示窗口
boolean result = user32.RegisterHotKey(null, 1, MOD_CONTROL, VK_X);
if (!result) {
System.out.println("ERROR -- 热键注册失败!可能存在冲突");
return;
}
WinUser.MSG msg = new WinUser.MSG();
try {
while (user32.GetMessage(msg,null,0,0) != 0){
if (msg.message == User32.WM_HOTKEY){
int wmId = msg.wParam.intValue();
if (wmId == 1){
if (isShow){
user32.ShowWindow(user32.FindWindow(null,windowTitle),User32.SW_HIDE);
isShow = false;
System.out.println("窗口已隐藏");
}else {
user32.ShowWindow(user32.FindWindow(null,windowTitle),User32.SW_SHOW);
isShow = true;
System.out.println("呼出窗口");
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
// 注销热键
User32.INSTANCE.UnregisterHotKey(null, 1);
}
});
thread.start();
listenHwnd(windowTitle, newHeight, newWidth, hwnd);//开启监听
break;
} else {
System.out.println("ERROR -- 找不到指定的窗口,尝试打开任务管理器查看应用名称或使用其他查找窗体句柄工具。");
}
} else {
System.out.println("ERROR -- 请有效输入窗口标题");
}
}
}

//监听句柄变化 修改窗体大小
public static void listenHwnd(String windowTitle, final Integer finalHeight, final Integer finalWidth, HWND changeHwnd) {
User32 user32 = User32.INSTANCE;
while (true) {
HWND hwnd = User32.INSTANCE.FindWindow(null, windowTitle);
// 判断原始句柄是否存在
if (hwnd.equals(changeHwnd) ) {
// 窗口存在
RECT rect = new RECT();
User32.INSTANCE.GetWindowRect(changeHwnd, rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
if (width != finalWidth || height != finalHeight) {
User32.INSTANCE.MoveWindow(changeHwnd, rect.left, rect.top, finalWidth, finalHeight, true);
}
} else {
// 获取窗口大小
RECT rect = new RECT();
User32.INSTANCE.GetWindowRect(hwnd, rect);
System.out.println(changeHwnd);
System.out.println("WARN -- 句柄发生变化,新句柄为: " + hwnd + " - 重新定义窗口大小");
User32.INSTANCE.MoveWindow(hwnd, rect.left, rect.top, finalWidth, finalHeight, true);
changeHwnd = hwnd;
}
try {
// 等待一段时间后再次查询
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}