Aha*_*med 12 java windows winapi jna
我是JNA的新手.我试图获得所有窗口的句柄,包括最小化的窗口.我需要HWND所有的窗户.我已经解决了Windows的问题:如何获取所有可见窗口的列表?这有助于我获取窗口列表,但它的hWnd类型为int.我不能将它com.sun.jna.platform.win32.User32用于要求hWnd类型的功能com.sun.jna.platform.win32.WinDef.HWND.那么,有没有办法获得类型的所有窗口句柄com.sun.jna.platform.win32.WinDef.HWND而不是int指针?最后,为什么差异int和HWND?它如何接受两者?我有点困惑.谢谢.
我有以下代码(从Hovercreft的答案编辑):
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
public class TryWithHWND {
public static void main(String[] args) {
final User32 user32 = User32.INSTANCE;
user32.EnumWindows(new WNDENUMPROC() {
int count = 0;
public boolean callback(HWND hWnd, Pointer arg1) {
char[] windowText = new char[512];
user32.GetWindowText(hWnd, windowText, 512);
String wText = Native.toString(windowText);
RECT rectangle = new RECT();
user32.GetWindowRect(hWnd, rectangle);
// get rid of this if block if you want all windows regardless
// of whether
// or not they have text
// second condition is for visible and non minimised windows
if (wText.isEmpty() || !(User32.INSTANCE.IsWindowVisible(hWnd)
&& rectangle.left > -32000)) {
return true;
}
System.out.println("Found window with text " + hWnd
+ ", total " + ++count + " Text: " + wText);
return true;
}
}, null);
}
}
Run Code Online (Sandbox Code Playgroud)
我试图只使用(不是自定义界面)默认User32类.它工作正常.我怀疑,为什么我们使用用户定义的界面而不是现有的界面呢?还有一件事,用户定义的方法签名和已有的方法签名之间总是存在差异.例如,变量windowText是char[],而Hovercraft的变量是类型byte[].有人能解释一下吗?谢谢.
Hov*_*els 12
最新版本的JNA已经有了一些应该解决的问题(作为JNA的作者之一,Luke Quinane,在这里指出).如果您使用最新版本并检查JNA API,您将看到WinUser.WNDENUMPROC接口的方法实际上使用WinDef.HWND作为其参数,而不是long或int.
例如:
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.win32.StdCallLibrary;
public class TryWithHWND {
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);
}
public static void main(String[] args) {
final User32 user32 = User32.INSTANCE;
user32.EnumWindows(new WNDENUMPROC() {
int count = 0;
@Override
public boolean callback(HWND hWnd, Pointer arg1) {
byte[] windowText = new byte[512];
user32.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText);
// get rid of this if block if you want all windows regardless of whether
// or not they have text
if (wText.isEmpty()) {
return true;
}
System.out.println("Found window with text " + hWnd + ", total " + ++count
+ " Text: " + wText);
return true;
}
}, null);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21034 次 |
| 最近记录: |