gpt4 book ai didi

java - 隐藏的 JDialog 在 Windows 10 任务栏预览中仍然可见

转载 作者:行者123 更新时间:2023-12-05 06:04:57 24 4
gpt4 key购买 nike

配置了 HIDE_ON_CLOSEJDialog 在关闭后变得不可见。但它在 Windows 任务栏的速览预览中仍然可见。只有在主窗口最小化并再次最大化后,预览才会得到纠正。当然,可以处理对话框而不是隐藏它,但这不是我的目标。

  • 这是 Java Swing 错误吗?
  • 是否可以强制刷新任务栏预览?
  • 还有其他解决方法吗?

代码示例:

    public static void main( String[] args ) throws Exception {
JFrame frame = new JFrame();
frame.setTitle( "frame" );
frame.setSize( 700, 700 );
frame.setVisible( true );
frame.setDefaultCloseOperation( JDialog.EXIT_ON_CLOSE );

JDialog d = new JDialog( frame ); // modeless dialog
d.setTitle( "dialog" );
d.setSize( 300, 300 );
d.setDefaultCloseOperation( JDialog.HIDE_ON_CLOSE );
d.setVisible( true );
}

GIF describing the problem

最佳答案

请尝试直接在对话框上设置 WS_EX_TOOLWINDOW 样式,然后再隐藏它。然后立即删除它(这样它就不会影响后续节目的渲染)。

任务栏上不允许使用工具栏窗口,因此无法预览。这是一个 hack,但它对 Windows 11 中的类似问题有用。

这是在 Windows 11 上对我有用的代码。我能够使用 JDK17 在 Windows 11 上重现错误。

你需要https://github.com/java-native-access/jna(jna.jarplatform.jar)。没有它,我找不到设置 win32 窗口样式的方法。

javac -cp .;./jna.jar;./platform.jar jdtest.java

import javax.swing.JFrame;
import javax.swing.JDialog;
import java.awt.event.*;

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;
import com.sun.jna.platform.win32.WinUser;
public class jdtest
{

public static int WS_EX_TOOLWINDOW = 0x00000080;
public static void changeStyle(HWND hwnd, boolean hiding) {
int exStyle = User32.INSTANCE.GetWindowLong(hwnd, WinUser.GWL_EXSTYLE);
if (hiding) {
User32.INSTANCE.SetWindowLong(hwnd, WinUser.GWL_EXSTYLE, exStyle | WS_EX_TOOLWINDOW);
} else {
User32.INSTANCE.SetWindowLong(hwnd, WinUser.GWL_EXSTYLE, exStyle & ~WS_EX_TOOLWINDOW);
}
}

private static HWND getHWnd(JDialog d) {
HWND hwnd = new HWND();
hwnd.setPointer(Native.getWindowPointer(d));
return hwnd;
}

public static void main( String[] args ) throws Exception {
JFrame frame = new JFrame();
frame.setTitle( "frame" );
frame.setSize( 700, 700 );
frame.setVisible( true );
frame.setDefaultCloseOperation( JDialog.EXIT_ON_CLOSE );

JDialog d = new JDialog( frame ); // modeless dialog
d.setTitle( "dialog" );
d.setSize( 300, 300 );
d.setDefaultCloseOperation( JDialog.HIDE_ON_CLOSE );

d.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
changeStyle(getHWnd(d), true);
}
});
d.addWindowListener(new WindowAdapter() {
public void windowOpening(WindowEvent e) {
changeStyle(getHWnd(d), false);
}
});

d.setVisible( true );
}
}

关于java - 隐藏的 JDialog 在 Windows 10 任务栏预览中仍然可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66159456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com