下面的内容旨在尝试将活动窗口的标题和存储区获取到title。
byte[] buffer = new byte[1024];
User32.instance.GetWindowTextA(hWnd, buffer, buffer.length);
String title = Native.toString(buffer);在title中,它应该存储一些类似于"猫の動画 - YouTube - Google Chrome"的东西;假设我在Chrome浏览器上打开一个YouTube选项卡,它的标题具有非字母字符,但UTF-8兼容。
然而,与我的预期相反,title实际上有一些类似于"??????? - YouTube - Google Chrome"的东西。
我还尝试了一些变体,如以下;但它们没有解决混淆的字符问题。
String title = new String( buffer, java.nio.charset.StandardCharsets.UTF_8 );我怎样才能解决这个问题?谢谢。
补充资料
以下是整个Java脚本:
package Main;
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.imageio.ImageIO;
import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;
public class windowcapture {
public static void main(String[] args) throws AWTException, IOException {
int hWnd = User32.instance.GetForegroundWindow();
WindowInfo window = getWindowInfo(hWnd);
}
public static WindowInfo getWindowInfo(int hWnd) {
RECT r = new RECT();
User32.instance.GetWindowRect(hWnd, r);
byte[] buffer = new byte[1024];
User32.instance.GetWindowTextA(hWnd, buffer, buffer.length);
String title = Native.toString(buffer);
WindowInfo info = new WindowInfo(hWnd, r, title);
return info;
}
public static interface WndEnumProc extends StdCallLibrary.StdCallCallback {
boolean callback(int hWnd, int lParam);
}
public static interface User32 extends StdCallLibrary {
public static final String SHELL_TRAY_WND = "Shell_TrayWnd";
public static final int WM_COMMAND = 0x111;
public static final int MIN_ALL = 0x1a3;
public static final int MIN_ALL_UNDO = 0x1a0;
final User32 instance = (User32) Native.loadLibrary("user32", User32.class);
boolean EnumWindows(WndEnumProc wndenumproc, int lParam);
boolean IsWindowVisible(int hWnd);
int GetWindowRect(int hWnd, RECT r);
void GetWindowTextA(int hWnd, byte[] buffer, int buflen);
int GetTopWindow(int hWnd);
int GetWindow(int hWnd, int flag);
boolean ShowWindow(int hWnd);
boolean BringWindowToTop(int hWnd);
int GetActiveWindow();
boolean SetForegroundWindow(int hWnd);
// HWND GetForegroundWindow();
int GetForegroundWindow();
int FindWindowA(String winClass, String title);
long SendMessageA(int hWnd, int msg, int num1, int num2);
final int GW_HWNDNEXT = 2;
}
public static class RECT extends Structure {
public int left, top, right, bottom;
@Override
protected List<String> getFieldOrder() {
List<String> order = new ArrayList<>();
order.add("left");
order.add("top");
order.add("right");
order.add("bottom");
return order;
}
}
public static class WindowInfo {
int hwnd;
RECT rect;
String title;
public WindowInfo(int hwnd, RECT rect, String title) {
this.hwnd = hwnd;
this.rect = rect;
this.title = title;
}
public String toString() {
return String.format("(%d,%d)-(%d,%d) : \"%s\"", rect.left, rect.top, rect.right, rect.bottom, title);
}
}
}最新(当前)版本
package Main;
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.imageio.ImageIO;
import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;
public class windowcapture {
public static void main(String[] args) throws AWTException, IOException {
int hWnd = User32.instance.GetForegroundWindow();
WindowInfo window = getWindowInfo(hWnd);
}
public static WindowInfo getWindowInfo(int hWnd) {
RECT r = new RECT();
User32.instance.GetWindowRect(hWnd, r);
byte[] buffer = new byte[1024];
// User32.instance.GetWindowTextA(hWnd, buffer, buffer.length);
User32.instance.GetWindowTextW(hWnd, buffer, buffer.length);
// FileOutputStream fos = null;
// try {
// fos = new FileOutputStream(new File("./out/hoge.txt"), true);
// fos.write(buffer);
// fos.flush();
// System.out.println("File Written Successfully");
// } catch (IOException ioe) {
// ioe.printStackTrace();
// } finally {
// try {
// if (fos != null) {
// fos.close();
// }
// } catch (IOException ioe) {
// System.out.println("Error in closing the Stream");
// }
// }
String title = Native.toString(buffer);
// String title = new String( buffer, Charset.forName("x-MS950-HKSCS") );
// String title = new String( buffer, Charset.forName("UTF_16BE") );
// String title = new String( buffer, Charset.forName("UTF-8") );
// String title = Native.toString(buffer, StandardCharsets.UTF_16BE);
WindowInfo info = new WindowInfo(hWnd, r, title);
return info;
}
public static interface WndEnumProc extends StdCallLibrary.StdCallCallback {
boolean callback(int hWnd, int lParam);
}
public static interface User32 extends StdCallLibrary {
public static final String SHELL_TRAY_WND = "Shell_TrayWnd";
public static final int WM_COMMAND = 0x111;
public static final int MIN_ALL = 0x1a3;
public static final int MIN_ALL_UNDO = 0x1a0;
final User32 instance = (User32) Native.loadLibrary("user32", User32.class);
boolean EnumWindows(WndEnumProc wndenumproc, int lParam);
boolean IsWindowVisible(int hWnd);
int GetWindowRect(int hWnd, RECT r);
void GetWindowTextA(int hWnd, byte[] buffer, int buflen);
void GetWindowTextW(int hWnd, byte[] buffer, int buflen);
int GetTopWindow(int hWnd);
int GetWindow(int hWnd, int flag);
boolean ShowWindow(int hWnd);
boolean BringWindowToTop(int hWnd);
int GetActiveWindow();
boolean SetForegroundWindow(int hWnd);
// HWND GetForegroundWindow();
int GetForegroundWindow();
int FindWindowA(String winClass, String title);
long SendMessageA(int hWnd, int msg, int num1, int num2);
final int GW_HWNDNEXT = 2;
}
public static class RECT extends Structure {
public int left, top, right, bottom;
@Override
protected List<String> getFieldOrder() {
List<String> order = new ArrayList<>();
order.add("left");
order.add("top");
order.add("right");
order.add("bottom");
return order;
}
}
public static class WindowInfo {
int hwnd;
RECT rect;
String title;
public WindowInfo(int hwnd, RECT rect, String title) {
this.hwnd = hwnd;
this.rect = rect;
this.title = title;
}
public String toString() {
return String.format("(%d,%d)-(%d,%d) : \"%s\"", rect.left, rect.top, rect.right, rect.bottom, title);
}
}
}发布于 2022-05-25 09:49:44
GetWindowTextA中的"A“代表ANSI,不是吗?ANSI没有这些字符,因此GetWindowTextA不可能提供您在这里想要的内容,因此没有多少java代码可以解决这个问题。
你想要GetWindowTextW。我认为这可能会返回UTF16LE,您可能不得不尝试不同的编码,但至少您从windows获得的数据仍然有您想要的(鉴于ANSI不支持这些汉字,已经删除了这些汉字的vs A )。
https://stackoverflow.com/questions/72374727
复制相似问题