gpt4 book ai didi

java - HWND 的 native@0xc41bcc 输出到实际窗口 ID

转载 作者:行者123 更新时间:2023-12-02 02:25:41 25 4
gpt4 key购买 nike

嘿,我在尝试让这个 JNA findwindow 工作时遇到了问题,它为我提供了 Windows ID (HWND),而不是一些疯狂的输出 (native@0xc41bcc)。

通过使用 C#,我知道当使用 findwindow 时,它会提供找到的窗口的 ID,然后我可以使用该 ID 来移动/调整该窗口的大小等。

我的代码:

public String exec(ITestExecutionServices tes, String[] args) {
try {
writer = new PrintWriter("c:/temp/the-file-name.txt", "UTF-8");
} catch (FileNotFoundException | UnsupportedEncodingException e1) {
e1.printStackTrace();
}

try {

final User32 user32 = User32.INSTANCE;

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

user32.EnumWindows(new WNDENUMPROC() {
@Override
public boolean callback(HWND hWnd, Pointer arg1) {
byte[] windowText = new byte[512];
user32.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText);
wText = (wText.isEmpty()) ? "" : wText;

if (wText.toLowerCase().contains("- funct test -")) {
writer.println("text: " + wText);
writer.println("HWND: " + hWnd);
//User32.INSTANCE.SetWindowPos(hwnd, new HWND(Pointer.createConstant(HWND_BOTTOM)), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
WinDef.HWND hwndFound = User32.INSTANCE.FindWindow(null, wText);
writer.println("hwndFound: " + hwndFound);
writer.println("t/f: " + User32.INSTANCE.MoveWindow(hwndFound, 500, 500, 10, 100, true));
//User32.INSTANCE.SetWindowPos(hWnd, new HWND(Pointer.createConstant(HWND_BOTTOM)), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
return true;
}
}, null);
} catch (Exception f) {
String tmp = f.toString();

for (StackTraceElement el : f.getStackTrace()) {
tmp = tmp + System.getProperty("line.separator") + el.toString();
}

JOptionPane.showMessageDialog(null, "ERROR!");

return "false";
}
writer.close();
return "true";
}

public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);

HWND FindWindow(String lpClassName, String lpWindowName);
HWND FindWindowExA(HWND parent, HWND child, String className, String window);
HWND GetDesktopWindow();
HWND SetFocus(HWND hWnd);
HWND WindowFromPoint(long point);
HWND ChildWindowFromPointEx(HWND hwndParent, long point, int uFlags);

HMENU GetMenu(HWND hWnd);
HMENU GetSystemMenu(HWND hWnd, boolean bRevert);
HMENU GetSubMenu(HMENU hMenu, int nPos);

HDC GetWindowDC(HWND hWnd);

LRESULT CallWindowProc(LONG_PTR proc, HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);

int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);
int GetWindowRect(HWND handle, int[] rect);
int GetClassNameA(HWND in, byte[] lpString, int size);
int SendMessageA(HWND hWnd, int msg, WPARAM wParam, LPARAM lParam);
int PostMessageA(HWND hWnd, int msg, WPARAM wParam, LPARAM lParam);
int ReleaseDC(HWND hWnd, HDC hDC);
int GetTopWindow(HWND hWnd);
int GetWindow(HWND hWnd, int flag);
int GetWindowModuleFileName(HWND hWnd, char[] buffer2, int i);
int GetWindowThreadProcessId(HWND hWnd, PointerByReference pref);
int SetWindowLongPtr(HWND hWnd, int nIndex, Callback callback);
int GetMenuString(HMENU hMenu, int uIDItem, char[] buffer, int nMaxCount, int uFlag);
int GetMenuItemCount(HMENU hMenu);
int GetMenuItemID(HMENU hMenu, int nPos);
int GetDlgCtrlID(HWND hwndCtl);
int GetDlgItemText(HWND hDlg, int nIDDlgItem, byte[] buffer, int nMaxCount);

boolean MoveWindow(WinDef.HWND hWnd,int X, int Y, int nWidth, int nHeight, boolean bRepaint);
boolean InvalidateRect(HWND hWnd, long lpRect, boolean bErase);
boolean UpdateWindow(HWND hWnd);
boolean RedrawWindow(HWND hWnd, long lprcUpdate, long hrgnUpdate, int flags);
boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
boolean EnumChildWindows(HWND parent, WNDENUMPROC callback, LPARAM info);
boolean SetForegroundWindow(HWND in);
boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
boolean DestroyWindow(HWND hWnd);
boolean IsWindowVisible(HWND hWnd);
boolean IsWindow(HWND hWnd);
boolean ShowWindow(HWND hWnd, int nCmdShow);
boolean GetCursorPos(long[] lpPoint); //use macros POINT_X() and POINT_Y() on long lpPoint[0]
boolean ClientToScreen(HWND hWnd, long[] lpPoint);//use macros POINT_X() and POINT_Y() on long lpPoint[0]
boolean ScreenToClient(HWND hWnd, long[] lpPoint);//use macros POINT_X() and POINT_Y() on long lpPoint[0]
boolean IsMenu(HMENU hMenu);
boolean TrackPopupMenu(HMENU hMenu, int uFlags, int x, int y, int nReserved, HWND hWnd, long prcRect);
boolean GetMenuItemRect(HWND hWnd, HMENU hMenu, int uItem, RECT rect);

void SwitchToThisWindow(HWND hWnd, boolean fAltTab);

long GetWindowLong(HWND hWnd, int index);

interface WNDENUMPROC extends StdCallCallback {boolean callback(HWND hWnd, Pointer arg);}
}

最佳答案

我认为这可能有帮助:

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;

这个函数:

public long getWindowId(String title) {
WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, title);

return Pointer.nativeValue(hwnd.getPointer());
}

关于java - HWND 的 native@0xc41bcc 输出到实际窗口 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57255422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com