发布时间:2025-12-09 12:05:09 浏览次数:1
import com.sun.jna.platform.win32.WinDef.HDC; //导入依赖的package包/类/** * Gets the icon that corresponds to a given icon handler. * * @param hIcon * Handler to the icon to get * @return The icon that corresponds to a given icon handler */public static BufferedImage getIcon(final HICON hIcon) {final int width = ICON_SIZE;final int height = ICON_SIZE;final short depth = ICON_DEPTH;final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);final Memory lpBitsColor = new Memory(width * height * depth / ICON_BYTE_SIZE);final Memory lpBitsMask = new Memory(width * height * depth / ICON_BYTE_SIZE);final BITMAPINFO info = new BITMAPINFO();final BITMAPINFOHEADER hdr = new BITMAPINFOHEADER();info.bmiHeader = hdr;hdr.biWidth = width;hdr.biHeight = height;hdr.biPlanes = 1;hdr.biBitCount = depth;hdr.biCompression = WinGDI.BI_RGB;final HDC hDC = User32.INSTANCE.GetDC(null);final ICONINFO piconinfo = new ICONINFO();User32.INSTANCE.GetIconInfo(hIcon, piconinfo);GDI32.INSTANCE.GetDIBits(hDC, piconinfo.hbmColor, 0, height, lpBitsColor, info, WinGDI.DIB_RGB_COLORS);GDI32.INSTANCE.GetDIBits(hDC, piconinfo.hbmMask, 0, height, lpBitsMask, info, WinGDI.DIB_RGB_COLORS);int r, g, b, a, argb;int x = 0, y = height - 1;for (int i = 0; i < lpBitsColor.size(); i = i + 3) {b = lpBitsColor.getByte(i) & 0xFF;g = lpBitsColor.getByte(i + 1) & 0xFF;r = lpBitsColor.getByte(i + 2) & 0xFF;a = 0xFF - lpBitsMask.getByte(i) & 0xFF;argb = a << 24 | r << 16 | g << 8 | b;image.setRGB(x, y, argb);x = (x + 1) % width;if (x == 0) {y--;}}User32.INSTANCE.ReleaseDC(null, hDC);GDI32.INSTANCE.DeleteObject(piconinfo.hbmColor);GDI32.INSTANCE.DeleteObject(piconinfo.hbmMask);return image;} import com.sun.jna.platform.win32.WinDef.HDC; //导入依赖的package包/类public BufferedImage capture(final HWND hWnd) { final HDC hdcWindow = User32.INSTANCE.GetDC(hWnd); final HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(hdcWindow); final RECT bounds = new RECT(); User32Extra.INSTANCE.GetClientRect(hWnd, bounds); final int width = bounds.right - bounds.left; final int height = bounds.bottom - bounds.top; if (width * height <= 0) { return null; } final HBITMAP hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcWindow, width, height); final 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); final 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; final Memory buffer = new Memory(width * height * 4); GDI32.INSTANCE.GetDIBits(hdcWindow, hBitmap, 0, height, buffer, bmi, WinGDI.DIB_RGB_COLORS); final 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; } import com.sun.jna.platform.win32.WinDef.HDC; //导入依赖的package包/类/** * @param args (ignored) */public static void main(String[] args){System.out.println("Installed Physical Monitors: " + User32.INSTANCE.GetSystemMetrics(WinUser.SM_CMONITORS));User32.INSTANCE.EnumDisplayMonitors(null, null, new MONITORENUMPROC() {@Overridepublic int apply(HMONITOR hMonitor, HDC hdc, RECT rect, LPARAM lparam){enumerate(hMonitor);return 1;}}, new LPARAM(0));} import com.sun.jna.platform.win32.WinDef.HDC; //导入依赖的package包/类public static BufferedImage getScreenshot( final Rectangle bounds ) {HDC windowDC = GDI.GetDC( USER.GetDesktopWindow() );HBITMAP outputBitmap = GDI.CreateCompatibleBitmap( windowDC, bounds.width, bounds.height );try {HDC blitDC = GDI.CreateCompatibleDC( windowDC );try {HANDLE oldBitmap = GDI.SelectObject( blitDC, outputBitmap );try {GDI.BitBlt( blitDC, 0, 0, bounds.width, bounds.height, windowDC, bounds.x, bounds.y, GDI32.SRCCOPY );} finally {GDI.SelectObject( blitDC, oldBitmap );}BITMAPINFO bi = new BITMAPINFO( 40 );bi.bmiHeader.biSize = 40;boolean ok = GDI.GetDIBits( blitDC, outputBitmap, 0, bounds.height, (byte[]) null, bi, WinGDI.DIB_RGB_COLORS );if ( ok ) {BITMAPINFOHEADER bih = bi.bmiHeader;bih.biHeight = -Math.abs( bih.biHeight );bi.bmiHeader.biCompression = 0;return bufferedImageFromBitmap( blitDC, outputBitmap, bi );} elsereturn null;} finally {GDI.DeleteObject( blitDC );}} finally {GDI.DeleteObject( outputBitmap );}}