Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "setFullScreen()" and "maximizeWindow()" #32

Open
isinvon opened this issue Oct 28, 2024 · 2 comments
Open

Support "setFullScreen()" and "maximizeWindow()" #32

isinvon opened this issue Oct 28, 2024 · 2 comments

Comments

@isinvon
Copy link

isinvon commented Oct 28, 2024

No description provided.

@isinvon
Copy link
Author

isinvon commented Oct 30, 2024

I wrote an "SetFullScreenUtil" that makes the app window full screen covering the screen.

depandency

 <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>5.15.0</version> 
</dependency>

SetFullScreenUtil

package com.sinvon.api.utils;

import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.User32;

public class SetFullScreenUtil {

    public static void setFullScreen(String windowTitle) {
        if (windowTitle == null || windowTitle.isEmpty()) {
            System.out.println("The provided window title is null or empty; unable to set the window to full screen.");
            return;
        }

        User32 user32 = User32.INSTANCE;

        // Find window handle
        WinDef.HWND hWnd = user32.FindWindow(null, windowTitle);

        if (hWnd == null) {
            System.out.println("Failed to retrieve the window handle; unable to set the window to full screen.");
            return;
        }

        // Get the screen dimensions
        WinDef.RECT rect = new WinDef.RECT();
        user32.GetWindowRect(hWnd, rect);

        // Get screen width and height
        int screenWidth = User32.INSTANCE.GetSystemMetrics(User32.SM_CXSCREEN);
        int screenHeight = User32.INSTANCE.GetSystemMetrics(User32.SM_CYSCREEN);

        // Set the window to cover the entire screen
        user32.SetWindowLong(hWnd, User32.GWL_STYLE, user32.GetWindowLong(hWnd, User32.GWL_STYLE) & ~User32.WS_OVERLAPPEDWINDOW);
        user32.SetWindowPos(hWnd, null, 0, 0, screenWidth, screenHeight, User32.SWP_NOZORDER | User32.SWP_SHOWWINDOW);

        System.out.println("The window has been set to full screen.");
    }

use the utils.

public static void main(String[] args) {
       SpringApplication.run(ApiApplication.class, args);
       Webview wv = new Webview(false);
       String windowTitle = "gale-app";
       wv.setTitle(windowTitle);
       // Full screen coverage
       SetFullScreenUtil.setFullScreen(windowTitle);
       wv.loadURL("https://promisesaplus.com/");
       wv.run();
       wv.close();
   }

the results show

the.result.of.full.screen.show.mp4

There are some problem.The transition animations of "webview_java" are not very smooth. I would prefer it to directly display the size I want, such as full screen or a small-sized window, instead of having a process of the window size changing. This might feel rather jarring.

@isinvon
Copy link
Author

isinvon commented Oct 30, 2024

I wrote a "maximizeWindow()" that maximizes the app window, but for now I've only tested it on the window, and it works successfully

depandency


<dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna</artifactId>
        <version>5.15.0</version>
    </dependency>
    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna-platform</artifactId>
        <version>5.15.0</version>
    </dependency>

WindowUtils


package com.sinvon.api.utils;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;

public class WindowUtils {

    public interface User32Library extends Library {
        User32Library INSTANCE = Native.load("user32", User32Library.class);

        void ShowWindow(HWND hWnd, int nCmdShow);
    }

    public static void maximizeWindow(String windowTitle) {
        WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, windowTitle); // Retrieve handle through window title
        if (hwnd != null) {
            // 调用最大化方法
            User32Library.INSTANCE.ShowWindow(hwnd, 3); // 3 = SW_MAXIMIZE
        } else {
            System.out.println("Window not found.");
        }
    }
}

main.java

 public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
        Webview wv = new Webview(false);
        String windowTitle = "gale-app";
        wv.setTitle(windowTitle);
        // maximize the window
        WindowUtils.maximizeWindow(windowTitle);
        wv.loadURL("https://promisesaplus.com/");
        wv.run();
        wv.close();
    }

Look at the result

Maximize.the.window.mp4

@isinvon isinvon changed the title Hope suport "setFullScreen()" and "maximizeWindow()" Support "setFullScreen()" and "maximizeWindow()" Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant