当前位置: 首页 > 面试题库 >

JavaFX最小化未修饰的阶段

山森
2023-03-14
问题内容

我有一个未经修饰的JavaFX Stage,还有我自己的最小化,最大化和关闭按钮。但不幸的是,与经过修饰的行为相比,在Windows
7中单击任务栏图标不会自动最小化该阶段。

通过单击任务栏图标,是否可以通过纯Java代码最小化未修饰的阶段?如果不是,我该如何使用JNA?

编辑: 好的,我一直在尝试使用JNA解决此问题,但是几乎没有C / C ++ /
JNA完成,因此设置起来有些麻烦。如果有人帮助我把这些拼凑在一起,我将不胜感激。

到目前为止,这是我的代码:

public final class Utils {

   static {
    if (PlatformUtil.isWin7OrLater()) {
            Native.register("shell32");
            Native.register("user32");
        }
    }

    // Apparently, this is the event I am after
    public static final int WM_ACTIVATEAPP = 0x1C;


    public static void registerMinimizeHandler(Stage stage) {
       // Hacky way to get a pointer to JavaFX Window
       Pointer pointer = getWindowPointer(stage);

       WinDef.HWND hwnd = new WinDef.HWND(pointer);

       // Here's my minimize/activate handler
       WinUser.WindowProc windowProc = new MinimizeHandler(stage);

       Pointer magicPointer = ... set this to point to windowProc?

       // This.. apparently, re-sets the WndProc? But how do I get the "magicPointer" that is "attached" to the windowProc?
       User32.INSTANCE.SetWindowLong(hwnd, User32.GWL_WNDPROC, magicPointer);
    }
}

 private static class MinimizeHandler implements WinUser.WindowProc {

    private Stage stage;

    private MinimizeHandler(Stage stage) {
        this.stage = stage;
    }

    @Override
    public WinDef.LRESULT callback(WinDef.HWND hWnd, int uMsg, WinDef.WPARAM wParam, WinDef.LPARAM lParam) {
        if (uMsg == WM_ACTIVATEAPP) {
            System.out.println("ACTIVATE");
        }
        return User32.INSTANCE.DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
}

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;
    }
}

编辑2:
我最终对此有了进一步的了解,但是当我重新设置WNDPROC时,我未修饰的窗口没有响应任何事件。.我为一个独立的示例提供了100个声誉的赏金一个可行的解决方案。仅Windows(7+)可以,我什至不知道它在其他平台上的行为。

编辑3: 好吧,我有点放弃了这一点。我正确设置了所有内容,并接收了事件,但是在弄清楚要监听的正确事件时遇到了问题。

由于对该问题引起了一定的兴趣,因此,如果有人想尝试继续进行下去,这是我的最终代码(希望它可以“开箱即用”地工作):

public final class Utils {

    static interface ExtUser32 extends StdCallLibrary, User32 {
        ExtUser32 INSTANCE = (ExtUser32) Native.loadLibrary(
                        "user32",
                        ExtUser32.class,
                        W32APIOptions.DEFAULT_OPTIONS);

        WinDef.LRESULT CallWindowProcW(
                        Pointer lpWndProc,
                        Pointer hWnd,
                        int msg,
                        WinDef.WPARAM wParam,
                        WinDef.LPARAM lParam);

        int SetWindowLong(HWND hWnd, int nIndex, com.sun.jna.Callback wndProc) throws LastErrorException;
    }

    // Some possible event types
    public static final int WM_ACTIVATE = 0x0006;
    public static final int WM_ACTIVATEAPP = 0x1C;
    public static final int WM_NCACTIVATE = 0x0086;

    public static void registerMinimizeHandler(Stage stage) {
        Pointer pointer = getWindowPointer(stage);
        WinDef.HWND hwnd = new WinDef.HWND(pointer);
        long old = ExtUser32.INSTANCE.GetWindowLong(hwnd, User32.GWL_WNDPROC);
        MinimizeHandler handler = new MinimizeHandler(stage, old);
        ExtUser32.INSTANCE.SetWindowLong(hwnd, User32.GWL_WNDPROC, handler);
    }

    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;
    }
}

    private static class MinimizeHandler implements WinUser.WindowProc, StdCallLibrary.StdCallCallback {

        private Pointer mPrevWndProc32;

        private Stage stage;

        private MinimizeHandler(Stage stage, long oldPtr) {
            this.stage = stage;

            mPrevWndProc32 = new Pointer(oldPtr);

            // Set up an event pump to deliver messages for JavaFX to handle
            Thread thread = new Thread() {
                @Override
                public void run() {
                    int result;
                    WinUser.MSG msg = new WinUser.MSG();
                    while ((result = User32.INSTANCE.GetMessage(msg, null, 0, 0)) != 0) {
                        if (result == -1) {
                            System.err.println("error in get message");
                            break;
                        }
                        else {
                            System.out.println("got message: " + result);
                            User32.INSTANCE.TranslateMessage(msg);
                            User32.INSTANCE.DispatchMessage(msg);
                        }
                    }
                }
            };
            thread.start();
        }

        @Override
        public WinDef.LRESULT callback(WinDef.HWND hWnd, int uMsg, WinDef.WPARAM wParam, WinDef.LPARAM lParam) {

            if (uMsg == WM_ACTIVATEAPP) {
                // Window deactivated (wParam == 0)... Here's where I got stuck and gave up,
                // this is probably not the best event to listen to.. the app
                // does indeed get iconified now by pressing the task-bar button, but it
                // instantly restores afterwards..
                if (wParam.intValue() == 0) {
                    stage.setIconified(true);
                }
                return new WinDef.LRESULT(0);
            }

            // Let JavaFX handle other events
            return ExtUser32.INSTANCE.CallWindowProcW(
                            mPrevWndProc32,
                            hWnd.getPointer(),
                            uMsg,
                            wParam,
                            lParam);
        }
    }
}

问题答案:

您可以只设置适当的窗口样式。它可以在XP中运行,但在Windows 7 32位环境中应该可以。我认为(但无法测试)是否使用64位,然后更改为Ptr
Windows函数,即。GetWindowLongPtr。

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.WinUser;
import static com.sun.jna.platform.win32.WinUser.GWL_STYLE;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JNATest extends Application {
    public static void main(String[] args) { launch(args); }

    @Override
    public void start(Stage stage) {
        TextArea ta = new TextArea("output\n");
        VBox root = new VBox(5,ta);
        Scene scene = new Scene(root,800,200);
        stage.setTitle("Find this window");
        stage.setScene(scene);
        stage.show();
        //gets this window (stage)
        long lhwnd = com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow();
        Pointer lpVoid = new Pointer(lhwnd);
        //gets the foreground (focused) window
        final User32 user32 = User32.INSTANCE;
        char[] windowText = new char[512];
        HWND hwnd = user32.GetForegroundWindow();
        //see what the title is
        user32.GetWindowText(hwnd, windowText, 512);
        //user32.GetWindowText(new HWND(lpVoid), windowText, 512);//to use the hwnd from stage
        String text=(Native.toString(windowText));
        //see if it's the same pointer
        ta.appendText("HWND java:" + lpVoid + " HWND user32:"+hwnd+" text:"+text+"\n");
        //change the window style if it's the right title
        if (text.equals(stage.getTitle())){
            //the style to change 
            int WS_DLGFRAME = 0x00400000;//s/b long I think
            //not the same constant here??
            ta.appendText("windows api:"+WS_DLGFRAME+" JNA: "+WinUser.SM_CXDLGFRAME);
            int oldStyle = user32.GetWindowLong(hwnd, GWL_STYLE);
            int newStyle = oldStyle & ~0x00400000; //bitwise not WS_DLGFRAME means remove the style
            newStyle = newStyle & ~0x00040000;//WS_THICKFRAME   
            user32.SetWindowLong(hwnd, GWL_STYLE, newStyle);
        }
    }

}

我的猜测是您将最后3行替换为

            long oldStyleLong = user32.GetWindowLongPtr(hwnd, GWL_STYLE).longValue();
            long newStyleLong = oldStyleLong & ~ 0x00400000l;
            user32.SetWindowLongPtr(hwnd, GWL_STYLE, new BaseTSD.LONG_PTR(newStyleLong));

为64位。我认为我的User32.dll中没有这些功能,因此无法对其进行测试。那里有很多无关的代码,主要用于测试或教学。确定要执行的操作后,请删除未使用的行。

ps。不要添加newStyle = newStyle & ~0x00020000;//WS_MINIMIZEBOX。这是JavaFX不用于修饰的样式标志之一。这就是为什么最小化不可用的原因。也许如果您尝试将舞台设置为未修饰状态并添加(使用|,而不是&〜)最小化框标志,您将获得相同的结果。有一些工具可以从任何窗口查找所有样式标记。

这是使用阶段的HWND更改未装饰阶段的最简单的代码。

    public void start(Stage stage) {
        Scene scene = new Scene(new Pane(new Label("Hello World")));
        stage.initStyle(StageStyle.UNDECORATED);
        stage.setTitle("Find this window");
        stage.setScene(scene);
        stage.show();
        long lhwnd = com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow();
        Pointer lpVoid = new Pointer(lhwnd);
        HWND hwnd = new HWND(lpVoid);
        final User32 user32 = User32.INSTANCE;
        int oldStyle = user32.GetWindowLong(hwnd, GWL_STYLE);
        System.out.println(Integer.toBinaryString(oldStyle));
        int newStyle = oldStyle | 0x00020000;//WS_MINIMIZEBOX
        System.out.println(Integer.toBinaryString(newStyle));
        user32.SetWindowLong(hwnd, GWL_STYLE, newStyle);
    }

它可以在前后打印出样式标记,以便您查找设置了哪些样式。



 类似资料:
  • 我有一个未装饰的JavaFX舞台,我自己的最小化,最大化 有没有办法通过单击任务栏图标来最小化纯Java代码未修饰的阶段?如果不是的话,我该怎么做,比如说,JNA? 编辑:好的,我一直在尝试用JNA解决这个问题,但由于几乎没有C/C/JNA,我在设置这个方面有点麻烦。如果有人帮我把这些东西拼凑起来,我将不胜感激。。 到目前为止,这是我的代码: 编辑2:我最终在这一点上做得更进一步,但当我重新设置W

  • 我在JavaFX应用程序中有一个未修饰的阶段。为了最小化它,我需要一个最小化按钮。我在中创建了我的布局,其中有一个,但是当我尝试使用最小化位于控制器内部的这个按钮时,使用,它找不到阶段。 如何在控制器类中找到对该阶段的引用?

  • 我有一个未装饰的舞台,里面有一个定制的标题栏。这个标题栏包含“图标按钮”,它们只是保存图像视图的堆叠窗格。 我还使用此代码通过标题栏(静态编程语言代码)使舞台可拖动: 现在的问题是,即使鼠标在其中一个“图标按钮”上,舞台也是可以拖动的。对于普通的JavaFX按钮,这似乎不是问题。我不明白为什么会这样,以及如何解决它。

  • 我是JavaFX技术的新手,目前我正在开发JavaFX应用程序,其中有一个未装饰的阶段,我可以使用下面的代码在屏幕上移动它,但我无法从右下角调整此窗口的大小,有人能给我建议解决方案吗。

  • 问题内容: 我试图发出Windows PC Toast通知。现在,我混合使用了Swing和JavaFX,因为我没有找到使用FX制作未修饰窗口的方法。我更喜欢只使用JavaFX。 那么,如何制作未装饰的窗户? 编辑: 我发现您可以直接使用创建舞台。 现在我所需要知道的是如何初始化工具箱,以便可以在中调用我的方法。(其中) 我通常会打电话给我,但是这使我无法创建和初始化。 那么,如何做这些事情以允许我

  • 我创建了一个小库,其中包含一个自定义窗口/阶段,用于使我的所有应用程序具有恒定和统一的外观。基本上,这只是一个没有装饰的舞台,有一个锚具,包含定制的最小化/最大化/关闭和调整大小控件。 唯一的问题是按下最小化按钮时执行的方法。在我的主系统(ArchLinux)上运行应用程序时不会发生任何事情,看起来应用程序被冻结了,这里有一个可视化示例:在Imgur上上传的示例图像,还没有足够的代表来发布图像 最