开发者

How to make entire window aero glass in Java?

I am trying to program a small application but I would like the entire window 开发者_如何学Cto be glass with buttons and labels on top of it. Is it possible to do this within Java?


Assuming Java SWT and friends do not have built-in support for Windows Aero technology, you're going to have to call a native API via JNI. The native API you'll need to call is

DwmExtendFrameIntoClientArea(int windowHandle, MARGINS margins);

This native API is found in the DWMAPI.dll native library in Windows Vista and Windows 7, and is documented on MSDN.

There's lot of documentation on the web about how to call this function. For example, here's an article on doing this in C#. That should get you started.


Of course this feature is highly platform-dependent (Windows, in this case) so JNI is needed.

Let's assume you have already checked that glass windows are enabled. The steps are:

  1. ensure that the window is layered;
  2. extend the glass frame into the client area;
  3. choose a color that identifies transparent areas;
  4. set that color as window background.

Steps 1-3 are written in C. Let HWND hwnd a handle to the window you want to glassify and let COLORREF color a color (the more unusual, the best):

// 1.
SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
  SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
// 2.
MARGINS margins = {-1, -1, -1, -1};
DwmExtendFrameIntoClientArea(hwnd, &margins);
// 3.
SetLayeredWindowAttributes(hwnd, color, 0, LWA_COLORKEY);

Step 4 is simple Java, something like

window.getContentPane().setBackground(color);

where color is the Java version of the color chosen before.


Hmm. I think that all Java GUI must be displayed, directly or indirectly, in a Window, and that's a heavyweight component. Not sure if you can make it transparent/translucent.

Try building a JFrame and setting its background color to new Color(255, 255, 255, 20) or so, where 20 is the alpha. That should either make it mostly transparent - or it won't work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜