简体   繁体   English

从Java到前面的窗口

[英]Bring window to front from Java

Is there any way to bring a window to the front using Java? 有没有办法使用Java将窗口带到前面? Maybe using some operating system library? 也许使用一些操作系统库?

It seems it is possible, but then your solution would be very OS specific. 这似乎是可能的,但那时你的解决方案将是非常特定于操作系统的。

Theoretically it can be done by placing a call to win32 API in the following sequence: 从理论上讲,可以通过按以下顺序调用win32 API来完成:

  1. FindWindow Function FindWindow函数
  2. ShowWindow Function OR, ShowWindow功能 OR,
  3. BringWindowToTop Function BringWindowToTop功能

Now the problem comes 'how to call them from java?'. 现在问题来自“如何从java调用它们”。 Well all the above functions are defined in user32.dll and it can be accessed by JNA . 以上所有函数都在user32.dll中定义, JNA可以访问它。

Some sample references to user32 API using JNA are: 使用JNA对user32 API的一些示例引用是:

  1. How can I read the window title with JNI or JNA? 如何使用JNI或JNA读取窗口标题?
  2. call FindWindow method of User32.dll using java 使用java调用User32.dll的FindWindow方法

Use google to find more. 使用谷歌查找更多。

Hope this will help. 希望这会有所帮助。

SWT is nice for Win32 calls. SWT很适合Win32调用。

import org.eclipse.swt.internal.win32.OS; import org.eclipse.swt.internal.win32.OS;

@SuppressWarnings("restriction") @SuppressWarnings( “限制”)

int hwnd = OS.FindWindowW(null, "Titlein".toCharArray()); int hwnd = OS.FindWindowW(null,“Titlein”.toCharArray());

 package focus; import com.sun.jna.Native; import com.sun.jna.platform.win32.WinDef.HWND; import com.sun.jna.win32.StdCallLibrary; public class ForegroundWindow { private interface User32 extends StdCallLibrary { final User32 instance = (User32) Native.loadLibrary("user32", User32.class); boolean SetForegroundWindow(HWND handle); HWND FindWindowA(String className, String windowName); HWND GetForegroundWindow(); } private String getWindowName(String winName) { String winText = ""; if (winText.contains(winName)) { return winText; } return null; } public boolean bringWindowToFront(String className, String winName) { HWND hWnd = User32.instance.FindWindowA(className, getWindowName(winName)); if (hWnd == null) { return false; } return User32.instance.SetForegroundWindow(hWnd); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM