使用Java在Windows中获取前台应用程序屏幕截图,无需额外边

问题描述 投票:1回答:1

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

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中找到边框厚度,它将解决我的问题。

java winapi jna
1个回答
3
投票

GetClientRect

客户端坐标指定客户区的左上角和右下角。因为客户端坐标是相对于窗口客户区左上角的,所以左上角的坐标是(0,0)。

这是一个相对坐标系。

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

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

编辑:

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

© www.soinside.com 2019 - 2024. All rights reserved.