使用 Java 在 Windows 中获取前台应用程序屏幕截图,没有额外的边缘

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


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

}

我得到如下前景截图

http://img3.mukewang.com/633ff2e70001d64612380792.jpg

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

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

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

User32.INSTANCE.GetForegroundWindow()

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

我觉得这个链接中的答案应该有效。WinApi中的GetClientRect和GetWindowRect有什么区别?


跃然一笑
浏览 473回答 1
1回答

缥缈止盈

获取客户端矩形:客户坐标指定客户区的左上角和右下角。因为客户坐标是相对于窗口客户区的左上角的,所以左上角的坐标是(0,0)。这是一个相对坐标系。您还应该调用ClientToScreen将客户端坐标转换为屏幕坐标。注意ClientToScreen只接收POINT(不是a RECT)的参数,你可以在这里POINT找到类。编辑:GetWindowRect将获得“额外”尺寸。但是,GetClientRect完全不包括它(以及其他额外信息,如标题栏、窗口边框等)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java