java - 使用 Java 在没有额外边缘的情况下在 Windows 中截取前台应用程序屏幕截图

标签 java winapi jna

我可以使用下面的代码截取前景图像

void startScreencapture(){
    RECT dimensionsOfWindow = new RECT();
    User32.INSTANCE.GetWindowRect(User32.INSTANCE.GetForegroundWindow(), dimensionsOfWindow );//now in the dimensionsOfWindow you have the dimensions
    Robot robot = new Robot();
    buf = robot.createScreenCapture( dimensionsOfWindow.toRectangle() );
}

public interface User32 extends StdCallLibrary {
    User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
    HWND GetForegroundWindow();  // add this
    int GetWindowTextA(PointerType hWnd, byte[] lpString, int nMaxCount);
    public boolean GetWindowRect(HWND hWnd, RECT rect);
}

我得到如下的前景截图 enter image description here

如果您注意到,屏幕截图在窗口边框处有一些额外的图像。

我不希望我的屏幕截图有额外的图像部分。

是否有可能以某种方式操纵

User32.INSTANCE.GetForegroundWindow()

这样我就可以得到没有多余部分的屏幕截图?

我觉得这个链接中的答案应该有效。 What is the difference between GetClientRect and GetWindowRect in WinApi?

但是当我用 GetClientRect 替换 GetWindowRect 时,我得到以下屏幕截图: enter image description here

理想情况下,我应该只获得前台应用程序的屏幕截图。

编辑: Daniel Widdis 好心地为我找到了一个类似的问题:getwindowrect-returns-a-size-including-invisible-borders

这有一个可能的答案,即获取 Windows 10 中的边框厚度并调整此厚度以获得我想要的屏幕截图。但是这个答案使用 DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &frame, sizeof(RECT)); 这可能是 C 代码。

如果我能找到如何在 Java 中查找边框粗细,它就会解决我的问题。

最佳答案

GetClientRect :

The client coordinates specify the upper-left and lower-right corners of the client area. Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).

这是一个相对坐标系。

您还应该调用 ClientToScreen将客户端坐标转换为屏幕坐标。

注意ClientToScreen只接收POINT的参数(不是RECT),你可以找到POINThere .

编辑:

GetWindowRect 将获得“额外”尺寸。但是,GetClientRect 确实不包含它(以及其他额外信息,如标题栏、窗口边框等)。

关于java - 使用 Java 在没有额外边缘的情况下在 Windows 中截取前台应用程序屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55684450/

相关文章:

java - Android studio 弹出窗口?

c - GetVolumeInformation 返回的 FileSystemName 字符串的可能值是什么?

python - 尝试在 python 中的文本文件中写入复制的数据

java - JNA 3.0.9 .so(64bit) 加载引用另一个 .so(64bit)

java - 卡夫卡中的空指针异常

java - 使用 JUNG 在 java 中需要相同的对象

c++ - 如何通过 shellexecute (visual studio c++/mfc) 运行 java 类

java - 如何使用 JNA 正确映射 `MagImageScalingCallback`?

pointers - JNA 传递指针 ByReference

Java fork join算法分析