webview_java icon indicating copy to clipboard operation
webview_java copied to clipboard

Support “setWindowTop( )”

Open isinvon opened this issue 1 year ago • 1 comments

I wrote a plan , hope will be useful to you.

depandency

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

WindowTopUtil.java

package com.sinvon.api.utils;

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

public class WindowTopUtil {

    // Topmost window handle
    public static final WinDef.HWND HWND_TOPMOST = new WinDef.HWND(new Pointer(-1));

    public static void setWindowTop(String windowTitle) {
        if (windowTitle == null || windowTitle.isEmpty()) {
            System.out.println("The provided window title is null or empty; unable to set the window to topmost."); // debug
            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 topmost.");
            return;
        }

        // Set window to topmost
        user32.SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, User32.SWP_NOMOVE | User32.SWP_NOSIZE);
    }
}

main.java

// set window title
String windowTitle = "My Webview App"; 
wv.setTitle(windowTitle);

// set WebView Window Top
WindowTopUtil.setWindowTop(windowTitle);

it works.

isinvon avatar Oct 28 '24 18:10 isinvon

Code works! Implemented in https://github.com/NotJustAnna/webview_java/commit/5339af114f5c304e146986cc7b51b853c92d951f

NotJustAnna avatar Apr 08 '25 23:04 NotJustAnna