我能够使用以下代码拍摄前景图像的屏幕截图
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);
}我得到了如下的前台截图

如果你注意到了,屏幕截图在窗口的边框上有一些额外的图像。
我不想要我的截图额外的图像部分。
有没有可能以某种方式操纵
User32.INSTANCE.GetForegroundWindow()
这样我就可以在没有额外部分的情况下得到截图了?
我觉得在这个链接的答案应该是有效的。What is the difference between GetClientRect and GetWindowRect in WinApi?
但是当我用GetClientRect替换GetWindowRect时,我会看到下面的屏幕截图:

理想情况下,我应该只得到前台应用程序的屏幕截图。
编辑: Daniel Widdis很友好地为我找到了一个类似的问题:getwindowrect-returns-a-size-including-invisible-borders
这有一个可能的答案,即在Windows10中获取边框粗细,并调整此粗细以获得我想要的屏幕截图。但是这个答案使用了DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &frame, sizeof(RECT));,它可能是C代码。
如果我能在Java中找到边界粗细,我的问题就解决了。
发布于 2019-04-16 09:10:36
工作区坐标指定工作区的左上角和右下角。因为工作区坐标是相对于窗口工作区的左上角的,所以左上角的坐标是(0,0)。
这是一个相对坐标系。
您还应该调用ClientToScreen将客户端坐标转换为屏幕坐标。
请注意,ClientToScreen只接收POINT的参数(而不是RECT),并且您可以找到POINT类here。
编辑:
GetWindowRect会得到一个“额外”的尺寸。然而,GetClientRect完全不包括它(以及其他额外的信息,如标题栏、窗口边框等)。
https://stackoverflow.com/questions/55684450
复制相似问题