如何在JavaFX中获取舞台的窗口句柄(hWnd)? [英] How can I get the window handle (hWnd) for a Stage in JavaFX?

查看:583
本文介绍了如何在JavaFX中获取舞台的窗口句柄(hWnd)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在Windows中构建JavaFX应用程序,我们希望能够做一些事情来操纵我们的应用程序在Windows 7/8任务栏中的显示方式.这需要修改Windows变量,称为"

We're building a JavaFX application in Windows, and we want to be able to do some things to manipulate how our application appears in the Windows 7/8 taskbar. This requires modifying a Windows variable called the "Application User Model ID".

通过使用 JNA ,我们已经设法在Swing中实现了我们想要的功能,并且我们想在JavaFX中重复我们的解决方案.不幸的是,要做到这一点,我们需要能够为应用程序中的每个窗口检索hWnd(窗口句柄).可以通过JNA Native.getWindowPointer()方法,该方法可与java.awt.Window一起使用,但我想不出一种使用javafx.stage.Window的好方法.

We've already managed to do exactly what we want in Swing by using JNA, and we'd like to repeat our solution in JavaFX. Unfortunately, to do this, we need to be able to retrieve the hWnd (window handle) for each window in our application. This can be done in Swing/AWT via the JNA Native.getWindowPointer() method, which works with java.awt.Window, but I can't figure out a good way to do this with a javafx.stage.Window.

有人知道有什么方法可以获取StagehWnd吗?

Does anyone know of any way to do get hWnd for a Stage?

推荐答案

这是JavaFX2版本(使用Stage而不是Window):

Here's a JavaFX2 version (uses Stage rather than Window):

private static Pointer getWindowPointer(Stage stage) {
    try {
        TKStage tkStage = stage.impl_getPeer();
        Method getPlatformWindow = tkStage.getClass().getDeclaredMethod("getPlatformWindow" );
        getPlatformWindow.setAccessible(true);
        Object platformWindow = getPlatformWindow.invoke(tkStage);
        Method getNativeHandle = platformWindow.getClass().getMethod( "getNativeHandle" );
        getNativeHandle.setAccessible(true);
        Object nativeHandle = getNativeHandle.invoke(platformWindow);
        return new Pointer((Long) nativeHandle);
    } catch (Throwable e) {
        System.err.println("Error getting Window Pointer");
        return null;
    }
}

这篇关于如何在JavaFX中获取舞台的窗口句柄(hWnd)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆