使用winapi

时间:2015-06-13 06:37:12

标签: java windows winapi capture

我正在使用winapi捕获Java中打开的软件应用程序的窗口。 下面的这个功能捕获软件应用程序的窗口并返回图像。

public static BufferedImage capture(HWND hWnd) {
   HDC hdcWindow = User32.INSTANCE.GetDC(hWnd);
   HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(hdcWindow);
   RECT bounds = new RECT();
   RECT bounds1 = new RECT();
   User32Extra.INSTANCE.GetWindowRect(hWnd, bounds);
   User32Extra.INSTANCE.GetClientRect(hWnd, bounds1);

   int extraGap = (bounds.right-bounds.left-bounds1.right);
   int width = bounds.right-bounds.left-extraGap;
   int height = bounds.bottom-bounds.top-extraGap ;
   HBITMAP hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcWindow, width, height);

   HANDLE hOld = GDI32.INSTANCE.SelectObject(hdcMemDC, hBitmap);
   GDI32Extra.INSTANCE.BitBlt(hdcMemDC,0, 0, width, height, hdcWindow, bounds.left+bounds1.right-bounds.right+extraGap, bounds.top+bounds1.bottom-bounds.bottom+extraGap, WinGDIExtra.MERGECOPY);
   GDI32.INSTANCE.SelectObject(hdcMemDC, hOld);

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

   System.out.println(GDI32.INSTANCE.DeleteObject(hBitmap));
   System.out.println(GDI32.INSTANCE.DeleteObject(hdcMemDC));
   System.out.println(User32.INSTANCE.ReleaseDC(hWnd, hdcWindow));

   return image;
} 

捕获的图像有一些错误,如: -

  1. 这是带有Eclipse标题栏的Flash Builder窗口的图像。

    Flash Builder Image with Eclipse title Bar

  2. 这是没有窗口按钮的Chrome浏览器图片。

    Chrome window without window buttons

  3. 这是带有Eclipse标题栏的文件浏览器的图像。

    File Browser window with

  4. 有时标题栏未在图像中捕获。

  5. 你知道为什么,以及如何解决这个问题?我的代码有什么不对吗?

0 个答案:

没有答案