使用JNA获取GetForegroundWindow();
我在上一个线程中提出了类似的问题(https:// stackoverflow.com/questions/5206633/java-find-out-what-application-window-is-in-focus) 但我被引导使用 JNI,但我并没有取得太大成功。 。我已经阅读了一些教程,虽然有些教程可以正常工作,但其他教程却不能,我仍然无法获取我需要的信息,即前台窗口的标题。
现在我正在研究 JNA,但我不知道如何访问 GetForegroundWindow() ...我想一旦使用此代码(在另一个线程上找到)获得窗口句柄,我就可以打印文本:
import com.sun.jna.*;
import com.sun.jna.win32.*;
public class jnatest {
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
int GetWindowTextA(PointerType hWnd, byte[] lpString, int nMaxCount);
}
public static void main(){
byte[] windowText = new byte[512];
PointerType hwnd = //GetForegroundWindow() (?)...
User32.INSTANCE.GetWindowTextA(hwnd, windowText, 512);
System.out.println(Native.toString(windowText));
}
}
任何建议?谢谢!
I asked a similar question on a previous thread (https://stackoverflow.com/questions/5206633/java-find-out-what-application-window-is-in-focus) but I was guided to use JNI, and I'm not having much success with it... I've read some tutorials and while some work fine, others don't I still can't get the information I need, which is the title of the window on the foreground.
Now I'm looking into JNA but I can't figure out how to access GetForegroundWindow() ... I think I can print the text once I get the handle to the window using this code (found on another thread):
import com.sun.jna.*;
import com.sun.jna.win32.*;
public class jnatest {
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
int GetWindowTextA(PointerType hWnd, byte[] lpString, int nMaxCount);
}
public static void main(){
byte[] windowText = new byte[512];
PointerType hwnd = //GetForegroundWindow() (?)...
User32.INSTANCE.GetWindowTextA(hwnd, windowText, 512);
System.out.println(Native.toString(windowText));
}
}
Any suggestions? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如何简单地添加一个方法调用来将本机 GetForegroundWindow 与您的界面相匹配,如下所示:
How about simply adding a method call to match the native GetForegroundWindow to your interface, something like so:
如果您只想获取窗口标题,则不必明确
加载
user32
库。 JNA 附带在platform.jar
文件中(位于至少在 v3.4 中是这样)。
我在这里工作:
请参阅 User32 的 Javadoc。它几乎拥有user32库中的所有功能。
If getting the window title is all you want to do, you don't have to explicitly
load the
user32
library. JNA comes with it, in theplatform.jar
file (atleast in v3.4 it does).
I got this working here:
See more on User32's Javadoc. Its got almost all the functions in the user32 library.