English 中文(简体)
禁用JFrame中的背景绘制以正确显示Aero(DWM)效果
原标题:Disable Background drawing in JFrame in order to properly display Aero (DWM) effects

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

但在某个地方(我不知道在哪里),在Aero效果上画了一个背景,效果就消失了。

我已经尝试过:

  • 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效果?

非常感谢你的帮助。

Screenshot

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

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

Odd behavior

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

请参阅此屏幕截图:http://d-gfx.kognetwork.ch/java_aero_bug.png

Example code

这是我使用的代码。

需要jna.jarplatform.jar。可从jna主页获得。

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);

这给了你想要的视觉效果,但不幸的是,你无法点击窗口!

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

然后我尝试使用反射。反射式地将<code>Window.oopaque</code>设置为true会得到与使用<code>Window Utils</code〕相同的结果。

最后,我尝试将其添加到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);
    }
}

成功了!“窗口”仍然对鼠标事件做出正确响应,但未绘制背景。这幅画有点小毛病,但应该会让你走上正轨。

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

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

//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));
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签