SwingUtilities.getWindowAncestor(this).toFront();
}
public void update() {
GDI32 gdi = GDI32.INSTANCE;
User32 user = User32.INSTANCE;
Window win = SwingUtilities.getWindowAncestor(this);
int w = win.getWidth();
int h = win.getHeight();
HDC screenDC = user.GetDC(null);
HDC memDC = gdi.CreateCompatibleDC(screenDC);
HBITMAP hBitmap = null;
HANDLE oldBitmap = null;
try {
BufferedImage buf =
new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g = buf.createGraphics();
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, w, h);
g.setComposite(AlphaComposite.SrcOver);
Point origin = SwingUtilities.convertPoint(getParent(), 0, 0, win);
getParent().paint(g.create(origin.x, origin.y,
getWidth(), getHeight()));
BITMAPINFO bmi = new BITMAPINFO();
bmi.bmiHeader.biWidth = w;
bmi.bmiHeader.biHeight = h;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = GDI32.BI_RGB;
bmi.bmiHeader.biSizeImage = w * h * 4;
PointerByReference ppbits = new PointerByReference();
hBitmap = gdi.CreateDIBSection(memDC, bmi,
GDI32.DIB_RGB_COLORS,
ppbits, null, 0);
oldBitmap = gdi.SelectObject(memDC, hBitmap);
Pointer pbits = ppbits.getValue();
Raster raster = buf.getData();
int[] pixel = new int[4];
int[] bits = new int[w * h];
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
raster.getPixel(col, h - row - 1, pixel);
int alpha = (pixel[3] & 0xFF) << 24;
int red = (pixel[2] & 0xFF);
int green = (pixel[1] & 0xFF) << 8;
int blue = (pixel[0] & 0xFF) << 16;
bits[col + row * w] = alpha | red | green | blue;
}
}
pbits.write(0, bits, 0, bits.length);
SIZE size = new SIZE();
size.cx = w;
size.cy = h;
POINT srcLoc = new POINT();
BLENDFUNCTION blend = new BLENDFUNCTION();
POINT loc = new POINT();
loc.x = win.getX();
loc.y = win.getY();
HWND hWnd = getHWnd(win);
// extract current constant alpha setting, if possible
ByteByReference bref = new ByteByReference();
IntByReference iref = new IntByReference();
byte level = getAlpha(win);
if (user.GetLayeredWindowAttributes(hWnd, null, bref, iref)
&& (iref.getValue() & User32.LWA_ALPHA) != 0) {
level = bref.getValue();
}
blend.SourceConstantAlpha = level;
blend.AlphaFormat = User32.AC_SRC_ALPHA;
user.UpdateLayeredWindow(hWnd, screenDC, loc, size, memDC,
srcLoc, 0, blend, User32.ULW_ALPHA);
}
finally {
user.ReleaseDC(null, screenDC);
if (hBitmap != null) {
gdi.SelectObject(memDC, oldBitmap);
gdi.DeleteObject(hBitmap);
}
gdi.DeleteDC(memDC);