在Java中激活其他进程的窗口 [英] Active other process's window in Java
本文介绍了在Java中激活其他进程的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个Java swing应用程序(意味着在两个JVM中运行)。有没有办法在它们之间切换?通过Java代码激活另一个应用程序的窗口?
I have two Java swing applications (means running in two JVM). Is there any way to switch between them? Active another application's window by Java code?
推荐答案
您可以尝试使用JNA。我会给你一些使用Maven的Windows代码(或多或少会用于其他系统):
(对不起但我无法正确格式化)
You can try using JNA. I'll give you some code for Windows (more or less will be for other systems) using Maven: (sorry but I cannot get formatting right)
-
创建Maven项目,并添加依赖项:
Create Maven project, and add dependencies:
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>platform</artifactId>
<version>3.4.0</version>
</dependency>
创建界面
Create interface
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
HWND GetParent(HWND hWnd);
HWND FindWindow(String lpClassName, String lpWindowName);
HWND SetFocus(HWND hWnd);
HWND FindWindowEx(HWND hwndParent, HWND hwndChildAfter, String lpszClass, String lpszWindow);
int GetWindowText(HWND hWnd, char[] lpString, int nMaxCount);
}
创建课程
Create class
public final class Win32WindowUtils {
private static final int WIN_TITLE_MAX_SIZE = 512;
public HWND GetWindowHandle(String strSearch, String strClass) {
char[] lpString = new char[WIN_TITLE_MAX_SIZE];
String strTitle;
int iFind = -1;
HWND hWnd = User32.INSTANCE.FindWindow(strClass, null);
while(hWnd != null) {
User32.INSTANCE.GetWindowText(hWnd, lpString, WIN_TITLE_MAX_SIZE);
strTitle = new String(lpString);
strTitle = strTitle.toUpperCase();
iFind = strTitle.indexOf(strSearch);
if(iFind != -1) {
return hWnd;
}
hWnd = User32.INSTANCE.FindWindowEx(null, hWnd, strClass, null);
}
return hWnd;
}
}
并调用
And invoke
User32.INSTANCE.SetFocus(Win32WindowUtils.GetWindowHandle(windowTitle.toUpperCase(), null);
当然 - windowTitle
是你的窗口标题(要聚焦的 String
)。
Of course - windowTitle
is your window title (String
) that you want to focus.
这篇关于在Java中激活其他进程的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文