use of com.sun.jna.platform.win32.WinGDI.BITMAPINFO in project jna by java-native-access.
the class GDI32Test method testBITMAPINFO.
public void testBITMAPINFO() {
BITMAPINFO info = new BITMAPINFO();
assertEquals("Wrong size for BITMAPINFO()", 44, info.size());
info = new BITMAPINFO(2);
assertEquals("Wrong size for BITMAPINFO(2)", 48, info.size());
info = new BITMAPINFO(16);
assertEquals("Wrong size for BITMAPINFO(16)", 104, info.size());
}
use of com.sun.jna.platform.win32.WinGDI.BITMAPINFO in project jna by java-native-access.
the class AlphaMaskDemo method updateW32.
private void updateW32(boolean a, boolean i) {
User32 user = User32.INSTANCE;
GDI32 gdi = GDI32.INSTANCE;
HWND hWnd = null;
if (!alphaWindow.isDisplayable()) {
alphaWindow.pack();
hWnd = getHwnd(alphaWindow);
int flags = user.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE);
flags |= WinUser.WS_EX_LAYERED;
user.SetWindowLong(hWnd, WinUser.GWL_EXSTYLE, flags);
Window parent = alphaWindow.getOwner();
Point where = parent.getLocationOnScreen();
where.translate(parent.getWidth(), 0);
alphaWindow.setLocation(where);
} else {
hWnd = getHwnd(alphaWindow);
}
if (i) {
int w = image.getWidth(null);
int h = image.getHeight(null);
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);
Graphics g = buf.getGraphics();
g.drawImage(image, 0, 0, w, h, null);
BITMAPINFO bmi = new BITMAPINFO();
bmi.bmiHeader.biWidth = w;
bmi.bmiHeader.biHeight = h;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = WinGDI.BI_RGB;
bmi.bmiHeader.biSizeImage = w * h * 4;
PointerByReference ppbits = new PointerByReference();
hBitmap = gdi.CreateDIBSection(memDC, bmi, WinGDI.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 y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
raster.getPixel(x, h - y - 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[x + y * w] = alpha | red | green | blue;
}
}
pbits.write(0, bits, 0, bits.length);
SIZE size = new SIZE();
size.cx = w;
size.cy = h;
POINT loc = new POINT();
loc.x = alphaWindow.getX();
loc.y = alphaWindow.getY();
POINT srcLoc = new POINT();
BLENDFUNCTION blend = new BLENDFUNCTION();
blend.SourceConstantAlpha = (byte) (alpha * 255);
blend.AlphaFormat = WinUser.AC_SRC_ALPHA;
user.UpdateLayeredWindow(hWnd, screenDC, loc, size, memDC, srcLoc, 0, blend, WinUser.ULW_ALPHA);
} finally {
user.ReleaseDC(null, screenDC);
if (hBitmap != null) {
gdi.SelectObject(memDC, oldBitmap);
gdi.DeleteObject(hBitmap);
}
gdi.DeleteDC(memDC);
}
} else if (a) {
BLENDFUNCTION blend = new BLENDFUNCTION();
blend.SourceConstantAlpha = (byte) (alpha * 255);
blend.AlphaFormat = WinUser.AC_SRC_ALPHA;
user.UpdateLayeredWindow(hWnd, null, null, null, null, null, 0, blend, WinUser.ULW_ALPHA);
}
if (!alphaWindow.isVisible()) {
alphaWindow.setVisible(true);
}
}
use of com.sun.jna.platform.win32.WinGDI.BITMAPINFO in project jna by java-native-access.
the class GDI32Util method getScreenshot.
/**
* Takes a screenshot of the given window
*
* @param target
* The window to target
* @return the window captured as a screenshot, or null if the BufferedImage doesn't construct properly
* @throws IllegalStateException
* if the rectangle from GetWindowRect has a width and/or height
* of 0. <br>
* if the device context acquired from the original HWND doesn't
* release properly
*/
public static BufferedImage getScreenshot(HWND target) {
RECT rect = new RECT();
if (!User32.INSTANCE.GetWindowRect(target, rect)) {
throw new Win32Exception(Native.getLastError());
}
Rectangle jRectangle = rect.toRectangle();
int windowWidth = jRectangle.width;
int windowHeight = jRectangle.height;
if (windowWidth == 0 || windowHeight == 0) {
throw new IllegalStateException("Window width and/or height were 0 even though GetWindowRect did not appear to fail.");
}
HDC hdcTarget = User32.INSTANCE.GetDC(target);
if (hdcTarget == null) {
throw new Win32Exception(Native.getLastError());
}
Win32Exception we = null;
// device context used for drawing
HDC hdcTargetMem = null;
// handle to the bitmap to be drawn to
HBITMAP hBitmap = null;
// original display surface associated with the device context
HANDLE hOriginal = null;
// final java image structure we're returning.
BufferedImage image = null;
try {
hdcTargetMem = GDI32.INSTANCE.CreateCompatibleDC(hdcTarget);
if (hdcTargetMem == null) {
throw new Win32Exception(Native.getLastError());
}
hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcTarget, windowWidth, windowHeight);
if (hBitmap == null) {
throw new Win32Exception(Native.getLastError());
}
hOriginal = GDI32.INSTANCE.SelectObject(hdcTargetMem, hBitmap);
if (hOriginal == null) {
throw new Win32Exception(Native.getLastError());
}
// draw to the bitmap
if (!GDI32.INSTANCE.BitBlt(hdcTargetMem, 0, 0, windowWidth, windowHeight, hdcTarget, 0, 0, GDI32.SRCCOPY)) {
throw new Win32Exception(Native.getLastError());
}
BITMAPINFO bmi = new BITMAPINFO();
bmi.bmiHeader.biWidth = windowWidth;
bmi.bmiHeader.biHeight = -windowHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = WinGDI.BI_RGB;
Memory buffer = new Memory(windowWidth * windowHeight * 4);
int resultOfDrawing = GDI32.INSTANCE.GetDIBits(hdcTarget, hBitmap, 0, windowHeight, buffer, bmi, WinGDI.DIB_RGB_COLORS);
if (resultOfDrawing == 0 || resultOfDrawing == WinError.ERROR_INVALID_PARAMETER) {
throw new Win32Exception(Native.getLastError());
}
int bufferSize = windowWidth * windowHeight;
DataBuffer dataBuffer = new DataBufferInt(buffer.getIntArray(0, bufferSize), bufferSize);
WritableRaster raster = Raster.createPackedRaster(dataBuffer, windowWidth, windowHeight, windowWidth, SCREENSHOT_BAND_MASKS, null);
image = new BufferedImage(SCREENSHOT_COLOR_MODEL, raster, false, null);
} catch (Win32Exception e) {
we = e;
} finally {
if (hOriginal != null) {
// per MSDN, set the display surface back when done drawing
HANDLE result = GDI32.INSTANCE.SelectObject(hdcTargetMem, hOriginal);
// failure modes are null or equal to HGDI_ERROR
if (result == null || WinGDI.HGDI_ERROR.equals(result)) {
Win32Exception ex = new Win32Exception(Native.getLastError());
if (we != null) {
ex.addSuppressed(we);
}
we = ex;
}
}
if (hBitmap != null) {
if (!GDI32.INSTANCE.DeleteObject(hBitmap)) {
Win32Exception ex = new Win32Exception(Native.getLastError());
if (we != null) {
ex.addSuppressed(we);
}
we = ex;
}
}
if (hdcTargetMem != null) {
// get rid of the device context when done
if (!GDI32.INSTANCE.DeleteDC(hdcTargetMem)) {
Win32Exception ex = new Win32Exception(Native.getLastError());
if (we != null) {
ex.addSuppressed(we);
}
we = ex;
}
}
if (hdcTarget != null) {
if (0 == User32.INSTANCE.ReleaseDC(target, hdcTarget)) {
throw new IllegalStateException("Device context did not release properly.");
}
}
}
if (we != null) {
throw we;
}
return image;
}
use of com.sun.jna.platform.win32.WinGDI.BITMAPINFO in project HearthStats.net-Uploader by HearthStats.
the class ProgramHelperWindows method _getScreenCaptureWindows.
private BufferedImage _getScreenCaptureWindows(HWND hWnd) {
HDC hdcWindow = User32.INSTANCE.GetDC(hWnd);
HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(hdcWindow);
RECT bounds = new RECT();
User32Extra.INSTANCE.GetClientRect(hWnd, bounds);
// check to make sure the window's not minimized
if (bounds.toRectangle().width >= 1024) {
if (isMinimised) {
_notifyObserversOfChangeTo("Hearthstone window restored");
isMinimised = false;
}
if (isFullScreen(bounds.toRectangle())) {
if (!isFullscreenFlag) {
_notifyObserversOfChangeTo("Hearthstone running in fullscreen");
isFullscreenFlag = true;
}
return null;
} else {
int width = bounds.right - bounds.left;
int height = bounds.bottom - bounds.top;
HBITMAP hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcWindow, width, height);
HANDLE hOld = GDI32.INSTANCE.SelectObject(hdcMemDC, hBitmap);
GDI32Extra.INSTANCE.BitBlt(hdcMemDC, 0, 0, width, height, hdcWindow, 0, 0, WinGDIExtra.SRCCOPY);
GDI32.INSTANCE.SelectObject(hdcMemDC, hOld);
GDI32.INSTANCE.DeleteDC(hdcMemDC);
BITMAPINFO bmi = new BITMAPINFO();
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = -height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = WinGDI.BI_RGB;
Memory buffer = new Memory(width * height * 4);
GDI32.INSTANCE.GetDIBits(hdcWindow, hBitmap, 0, height, buffer, bmi, WinGDI.DIB_RGB_COLORS);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, width, height, buffer.getIntArray(0, width * height), 0, width);
GDI32.INSTANCE.DeleteObject(hBitmap);
User32.INSTANCE.ReleaseDC(hWnd, hdcWindow);
return image;
}
}
if (!isMinimised) {
// that the window has been minimised.
if (minimisedCount < ITERATIONS_FOR_MINIMISE) {
minimisedCount++;
} else {
_notifyObserversOfChangeTo("Warning! Hearthstone minimized. No detection possible.");
isMinimised = true;
minimisedCount = 0;
}
}
return null;
}
Aggregations