Search code examples
javajavafxtitlebar

Manage Windows Aero shake feature with custom title bar


There are many tutorials or questions related to custom title bar with JavaFX

I created a custom title bar like this:

custom bar

I can move the windows but now (because the windows is UNDECORATED I can't apply any native Windows feature like the Aero shake (if you shake a window, all other app are reduced)

Is there any solution to manage this type of feature with a custom title bar?

Here is my code:

start.java:

@Override
public void start(Stage primaryStage) throws Exception{
    setPrimaryStage(primaryStage);
    prStage = primaryStage;
    Parent root = FXMLLoader.load(getClass().getResource("../gui/main.fxml"));
    prStage.initStyle(StageStyle.UNDECORATED);

    //prStage.setOpacity(0.75);
    Scene scene = new Scene(root, 640, 360);
    prStage.setScene(scene);
    prStage.getIcons().add(new Image("/resource/Images/icon.png"));
    scene.getStylesheets().add(getClass().getResource("..//gui/css/NewUICSS.css").toExternalForm());
    prStage.show();
}

main controller:

public class NewUIController {
    private static double xOffset = 0;
    private static double yOffset = 0;

    public void initialize() {
        moveBar.setOnMousePressed(this::mousePressed);
        moveBar.setOnMouseDragged(this::mouseDrag);
        moveBar.setOnMouseReleased(this::mouseRealease);

        mnuBar.setOnMousePressed(this::mousePressed);
        mnuBar.setOnMouseDragged(this::mouseDrag);
        mnuBar.setOnMouseReleased(this::mouseRealease);
    }

    private void mouseDrag(MouseEvent event){
        MainJavaFx.getPrimaryStage().setX(event.getScreenX() - xOffset);
        MainJavaFx.getPrimaryStage().setY(event.getScreenY() - yOffset);
    }

    private void mouseRealease(MouseEvent event){
        if(event.getSceneY() == 0){
            MainJavaFx.getPrimaryStage().setY(0);
        }else if(MainJavaFx.getPrimaryStage().getY() < 0){
            MainJavaFx.getPrimaryStage().setY(0);
        }
    }

    private void mousePressed(MouseEvent event){
        xOffset = event.getSceneX();
        yOffset = event.getSceneY();
    }

    public void close(){
        ((Stage)pnPrincipal.getScene().getWindow()).close();
    }
}

Solution

  • All these problems exist only on windows, so we need to use an other "style" than the undecorated style to manage all windows features.

    You need to use two libraries:

    • jna : point on you windows
    • jna-platform : create an appropriate style
    public void start(Stage primaryStage) throws Exception{
        setPrimaryStage(primaryStage);
        prStage = primaryStage;
        Parent root = FXMLLoader.load(getClass().getResource("../gui/NewUI2.fxml"));
    
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.setScene(new Scene(root, 1280, 720));
        Main.getPrimaryStage().getScene().getStylesheets().add(getClass().getResource("..//gui/css/main-black.css").toExternalForm());
    
        primaryStage.show();
    
        // verify if it is windows
        if(System.getProperty("os.name").indexOf("win") >= 0)
            long lhwnd = com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow();
            Pointer lpVoid = new Pointer(lhwnd);
            WinDef.HWND hwnd = new WinDef.HWND(lpVoid);
            final User32 user32 = User32.INSTANCE;
            int oldStyle = user32.GetWindowLong(hwnd, WinUser.GWL_STYLE);
            int newStyle = oldStyle | 0x00020000;//WS_MINIMIZEBOX
            user32.SetWindowLong(hwnd, WinUser.GWL_STYLE, newStyle);
        }
    }
    

    But I don't know if it is the best way to do that.