winapi - 为什么自定义窗口过程停止处理窗口消息?

标签 winapi javafx

我正在尝试配置自定义窗口过程并且它可以工作。但是,过了一会儿,窗口停止对任何输入使用react。似乎在一个场景中进行的渲染越多,窗口就越早被打破。

如果我的自定义窗口过程只是调用默认窗口,甚至会发生这种情况。

复制者:

package com.example;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.BaseTSD;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.win32.W32APIOptions;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.stage.Stage;

import static com.sun.jna.platform.win32.WinUser.GWL_WNDPROC;

public class CustomWndProc {

  public static void main(String[] args) {
    CustomFrameApplication.launch(CustomFrameApplication.class, args);
  }

  public static class CustomFrameApplication extends Application {

    @Override
    public void start(Stage primaryStage) {
      primaryStage.setScene(new Scene(new ProgressIndicator(ProgressIndicator.INDETERMINATE_PROGRESS)));
      primaryStage.show();

      HWND hwnd = new HWND();
      hwnd.setPointer(User32.INSTANCE.GetActiveWindow().getPointer());

      BaseTSD.LONG_PTR defaultWindowProc = User32.INSTANCE.GetWindowLongPtr(hwnd, GWL_WNDPROC);

      WinUser.WindowProc windowProc = (hwnd1, uMsg, wParam, lParam) ->
        User32Ex.INSTANCE.CallWindowProc(defaultWindowProc, hwnd1, uMsg, wParam, lParam);

      User32Ex.INSTANCE.SetWindowLongPtr(hwnd, GWL_WNDPROC, windowProc);
    }
  }

  public interface User32Ex extends User32 {
    User32Ex INSTANCE = Native.load("user32", User32Ex.class, W32APIOptions.DEFAULT_OPTIONS);

    Pointer SetWindowLongPtr(HWND hWnd, int nIndex, WindowProc wndProc);

    LRESULT CallWindowProc(LONG_PTR proc, HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);
  }
}

给它几秒钟或几分钟,您将无法再移动、最小化、最大化或关闭窗口。

如果您想要保证卡住,请使用 WebView 而不是 ProgressIndicator:

      WebView webView = new WebView();
      webView.getEngine().load("https://www.google.com");
      primaryStage.setScene(new Scene(webView));

我想知道它是否与我的代码在 JavaFX 应用程序线程中运行(导致某些竞争条件)有关,但我假设默认窗口过程也是如此(我如何验证?)。

我正在尝试构建一个使用 custom frame 的 JavaFX 应用程序.

使用 JNA 5.5.0。

最佳答案

我认为这是正确的做法,但也许我错了.. 我从以下位置获得信息: Why my JNA using application doesn't react in a right way?

import com.sun.jna.LastErrorException;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.BaseTSD;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.win32.W32APIOptions;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.stage.Stage;


public class CustomWndProc {

    public static void main(String[] args) {
        CustomFrameApplication.launch(CustomFrameApplication.class, args);
    }

    public static class CustomFrameApplication extends Application {

        private BaseTSD.LONG_PTR baseWndProc;

        public User32Ex.WNDPROC listener = new User32Ex.WNDPROC() {
            public WinDef.LRESULT callback(HWND hWnd, int uMsg, WinDef.WPARAM uParam,
                    WinDef.LPARAM lParam) {
                // TODO handle the window message
                // calling the base WndProc
                return User32Ex.INSTANCE.CallWindowProc(baseWndProc, hWnd, uMsg, uParam, lParam);
            }
        };

        @Override
        public void start(Stage primaryStage) {
            primaryStage.setScene(new Scene(new ProgressIndicator(ProgressIndicator.INDETERMINATE_PROGRESS)));
            primaryStage.show();

            HWND hwnd = new HWND();
            hwnd.setPointer(User32.INSTANCE.GetActiveWindow().getPointer());

            this.baseWndProc = User32Ex.INSTANCE.GetWindowLongPtr(hwnd, User32Ex.GWL_WNDPROC);

            User32Ex.INSTANCE.SetWindowLongPtr(hwnd, User32Ex.GWL_WNDPROC, this.listener);
        }
    }

    public interface User32Ex extends User32 {

        User32Ex INSTANCE = Native.load("user32", User32Ex.class, W32APIOptions.DEFAULT_OPTIONS);

        interface WNDPROC extends StdCallCallback {

            LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);
        }

        LONG_PTR GetWindowLongPtr(HWND hWnd, int nIndex) throws LastErrorException;

        LRESULT CallWindowProc(LONG_PTR proc, HWND hWnd, int uMsg, WPARAM uParam, WinDef.LPARAM lParam) throws LastErrorException;

        LONG_PTR SetWindowLongPtr(HWND hWnd, int nIndex, WNDPROC wndProc) throws LastErrorException;
    }
}

关于winapi - 为什么自定义窗口过程停止处理窗口消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62477273/

相关文章:

c - 如何在 C 中将字符串复制到剪贴板?

delphi - 如何从外部进程获取进程环境 block (PEB)?

c++ - 使用 TerminateProcess 终止 "mstsc.exe"进程时出现错误代码 (5) 访问被拒绝

java - 选择包含未解析的引用

java - 如何根据多个条件从ArrayList中获取元素?

java - JavaFX 的 TextField 的值更改监听器

.net - 是否有任何理由在 .NET 上使用 Win32 API(C 或 C++)?

c++ - 具有相对路径的 CFileDialog

JavaFX 2 - Tableview 复选框不显示值

java - 为什么标签没有显示在事件处理程序方法中?