禁用 JFrame 中的背景绘图以正确显示 Aero (DWM) 效果

2022-01-24 00:00:00 java swing jframe jna dwm

我在 Java 窗口上使用 Windows Vista/7 的 DWM 功能时遇到问题.我想让我的框架的背景使用 Aero 风格.执行此操作的 Windows API 由 dwmapi 库中的函数 DwmExtendFrameIntoClientArea 提供.我已经设法通过 JNA 正确调用了该过程,并且它完成了它应该做的事情(例如,您可以看到,在调整框架大小时,在下一次重新绘制之前,您会在尚未绘制的区域中看到适当的航空效果,见附图).

I'm having problems using the DWM functionality of Windows Vista/7 on Java windows. I want to make the background of my frame use the Aero style. The Windows API to do so is provide by the function DwmExtendFrameIntoClientArea in the dwmapi library. I've managed to call the procedure properly via JNA, and it does what it is supposed to do (You can see that for example when resizing the frame, before the next repaint you see the proper aero effects in the area not yet painted, see the attached image).

但是在某个地方(我不知道在哪里)在 Aero 效果上绘制了背景并且效果丢失了.

But somewhere (I can't figure out where) a background is painted over the Aero effect and the effect is lost.

我已经尝试过的:

  • 使用不透明度设置为 false
  • 的自定义 ContentPane
  • LayeredPaneRootPane 的不透明度设置为 false
  • 使用 Frame 而不是 JFrame
  • JFrame/ContentPane的背景颜色设置为黑色/全透明
  • 使用 setLayersOpaque 及其自定义变体,有关详细信息,请参阅第一个答案
  • Using a custom ContentPane with opacity set to false
  • Setting the opacity of the LayeredPane and the RootPane to false
  • Using a Frame instead of a JFrame
  • Set the background color of the JFrame/ContentPane to black/fully transparent
  • Use setLayersOpaque and a custom variant thereof, see first answer for more details

到目前为止,我无法成功删除该背景.它是 AWT/Swing 的限制吗?如何移除该背景或正确使用 Aero 效果?

So far I could not succeed removing that background. Is it a limitation of AWT/Swing? How can I remove that background or use the Aero effect properly?

非常感谢您的帮助.

这是一个没有任何内容的框架的屏幕截图,已将 RootPane、LayeredPane 和 ContentPane 的不透明度设置为 false.我在调整大小时很快做到了.您会看到效果已正确应用于 Java 尚未绘制的区域.

Here a screenshot of a frame without any contents, having set the opacity of the RootPane, LayeredPane and ContentPane to false. I did it quickly while resizing. You see that the effect is properly applied to the area Java did not yet paint on.

http://i55.tinypic.com/v614qo.png(作为新用户我不能直接发布图片...)

http://i55.tinypic.com/v614qo.png (As a new user I cannot post the image directly...)

经过进一步调查,我发现了以下奇怪的行为.如果窗口大小为 150x150 或以下,内容将透明显示.这对于普通的窗口组件来说是非常有问题的.如果您通过覆盖 paint() 方法直接在框架上绘制,则所有内容都是半透明的.此外,坐标系似乎有点偏离,它显示为 JFrame 的零点设置为窗口的实际零点.因此 Swing 尝试绘制到实际窗口边框所在的区域,然后当然不可见.

Upon further investigation I came across the following odd behavior. If the window size is 150x150 or below the contents are displayed transparently. This is very glitchy for normal window components. If you paint directly on the frame by overriding the paint() method everything is drawn semi-transparent. Additionally the coordinate system seems to be a little off, it appears as the zero point of the JFrame is set to the actual zero point of the window. Thus Swing tries to paint to areas where actually the window border is located, which then of course is not visible.

查看此屏幕截图:http://d-gfx.kognetwork.ch/java_aero_bug.png

这是我使用的代码.

需要 jna.jarplatform.jar.可从 JNA 主页获取.

Requires jna.jar and platform.jar. Available from the JNA homepage.

import com.sun.jna.Function;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinNT.HRESULT;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;

public class AeroFrame extends JFrame {

    public AeroFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel label = new JLabel("Testlabel");
        label.setOpaque(false);

        add(label);

        pack();

        enableAeroEffect();
    }

    private void enableAeroEffect() {
        NativeLibrary dwmapi = NativeLibrary.getInstance("dwmapi");
        HWND aeroFrameHWND = new HWND(Native.getWindowPointer(this));
        MARGINS margins = new MARGINS();
        margins.cxLeftWidth = -1;
        margins.cxRightWidth = -1;
        margins.cyBottomHeight = -1;
        margins.cyTopHeight = -1;
        //DwmExtendFrameIntoClientArea(HWND hWnd, MARGINS *pMarInset)
        //http://msdn.microsoft.com/en-us/library/aa969512%28v=VS.85%29.aspx
        Function extendFrameIntoClientArea = dwmapi.getFunction("DwmExtendFrameIntoClientArea");
        HRESULT result = (HRESULT) extendFrameIntoClientArea.invoke(HRESULT.class,
                new Object[] { aeroFrameHWND, margins});
        if(result.intValue()!=0)
            System.err.println("Call to DwmExtendFrameIntoClientArea failed.");
    }

    /**
     * http://msdn.microsoft.com/en-us/library/bb773244%28v=VS.85%29.aspx
     */
    public class MARGINS extends Structure implements Structure.ByReference {
            public int cxLeftWidth;
            public int cxRightWidth;
            public int cyTopHeight;
            public int cyBottomHeight;
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            JFrame.setDefaultLookAndFeelDecorated(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
        new AeroFrame().setVisible(true);
    }

}

推荐答案

好问题.

最明显的答案是

WindowUtils.setWindowOpaque(this, false);

这为您提供了您想要的视觉效果,但不幸的是,您无法点击窗口!

That gives you the visual effects you want but unfortunately prevents you from being able to click on the Window!

我尝试的第二件事是重写 paint() 方法,以执行与 Window.paint()opaque 标志设置为 false 时所做的相同操作.那没有任何作用.

The second thing I tried was to override the paint() method to perform the same actions that Window.paint() does when the opaque flag is set to false. That didn't do anything.

然后我尝试使用反射.反射性地将 Window.opaque 设置为 true 与使用 WindowUtils 的结果相同.

Then I tried using Reflection. Reflectively setting Window.opaque to true gave the same results as using WindowUtils.

最后,我尝试将其添加到 enableAeroEffect():

Finally, I tried adding this to enableAeroEffect():

Method m = null;
try {
    m = Window.class.getDeclaredMethod("setLayersOpaque", Component.class, Boolean.TYPE);
    m.setAccessible(true);
    m.invoke(null, this, false);
} catch ( Exception e ) {
    //TODO: handle errors correctly
} finally {
    if ( m != null ) {
        m.setAccessible(false);
    }
}

这行得通!窗口仍能正确响应鼠标事件,但未绘制背景.绘图有点小问题,但应该能让你上路.

This worked! The Window still responds properly to mouse events, but the background isn't drawn. The drawing is a bit glitchy, but should get you on your way.

显然它很脆弱,因为它依赖于反射.如果我是你,我会看看 Window.setLayersOpaque() 做了什么,并尝试以不依赖反射的方式复制它.

Obviously it's fragile since it relies on Reflection. If I were you, I'd take a look at what Window.setLayersOpaque() does, and try to replicate that in a way that doesn't rely on Reflection.

编辑:在检查 setLayersOpaque 方法时,似乎真的可以归结为禁用透明组件上的双缓冲.从您的 enableAeroEffect() 方法调用此方法,您就可以开始了:

Edit: On inspection of the setLayersOpaque method, it really seems to boil down to disabling double-buffering on the transparent components. Call this method from your enableAeroEffect() method and you're on your way:

//original source: Sun, java/awt/Window.java, setLayersOpaque(Component, boolean)
private static void setLayersTransparent(JFrame frame) {
    JRootPane root = frame.getRootPane();
    root.setOpaque(false);
    root.setDoubleBuffered(false);

    Container c = root.getContentPane();
    if (c instanceof JComponent) {
        JComponent content = (JComponent) c;
        content.setOpaque(false);
        content.setDoubleBuffered(false);
    }
    frame.setBackground(new Color(0, 0, 0, 0));
}

相关文章