![](/img/trans.png)
[英]How to take screenshot of any java native application without bringing it to foreground in Java?
[英]Take foreground application screenshot in Windows using Java without extra edges
我可以使用下面的代码截取前景图像的截图
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()
这样我就可以获得没有额外部分的截图?
我觉得这个链接的答案应该有效。 WinApi中的GetClientRect和GetWindowRect有什么区别?
但是当我用GetClientRect替换GetWindowRect时,我得到以下屏幕截图:
理想情况下,我应该只有前台应用程序的截图。
编辑:丹尼尔Widdis亲切地为我找到了一个类似的问题: getwindowrect-returns-a-size-including-invisible-borders
这有一个可能的答案,即在Windows 10中获取边框厚度并调整此厚度以获取我想要的屏幕截图。 但是这个答案使用了DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &frame, sizeof(RECT));
这可能是C代码。
如果我能找到如何在Java中找到边框厚度,它将解决我的问题。
客户端坐标指定客户区的左上角和右下角。 因为客户端坐标是相对于窗口客户区左上角的,所以左上角的坐标是(0,0)。
这是一个相对坐标系。
您还应该调用ClientToScreen将Client坐标转换为屏幕坐标。
请注意, ClientToScreen
只接收POINT
(不是RECT
)的参数,您可以在此处找到POINT
类。
编辑:
GetWindowRect
将获得“额外”大小。 但是, GetClientRect
确实不包含它(以及其他额外信息,如标题栏,窗口边框等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.