Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java在Windows中拍摄前景应用程序屏幕截图,无需额外边缘_Java_Winapi_Jna - Fatal编程技术网

使用Java在Windows中拍摄前景应用程序屏幕截图,无需额外边缘

使用Java在Windows中拍摄前景应用程序屏幕截图,无需额外边缘,java,winapi,jna,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 rob

我能用下面的代码拍摄前景图像的截图

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

这样我就可以在没有多余部分的情况下得到截图了

我觉得这个链接中的答案应该有用

但当我用GetClientRect替换GetWindowRect时,我看到了下面的屏幕截图:

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

编辑: Daniel Widdis善意地为我找到了一个类似的问题:

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

如果我能找到如何在Java中查找边框厚度,它将解决我的问题。

:

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

这是一个相对坐标系

还应调用将客户端坐标转换为屏幕坐标

请注意,
ClientToScreen
仅接收
POINT
的参数(而不是
RECT
),您可以找到
POINT

编辑:


GetWindowRect
将获得“额外”大小。但是,
GetClientRect
并没有包括它(以及其他额外的信息,如标题栏、窗口边框等)。

JNA的整个用途可能是为了让C代码可以访问!)如果函数还没有内置到JNA中,您可以添加一个接口来声明它。这并不能回答用户的问题,即如何去除(透明)边框。我尝试过ClientToScreen,但它需要int点。我试着把球传直,传左,传右。但是结果是一样的。@Daniel Widdis,GetClientRect不包括(不可见的)边界和标题栏、窗口边界等。@Promod,您需要新建2
s:{left,top}和{right,botom}才能从客户端坐标到屏幕坐标。但不仅仅是传递
int
。顺便说一句,您遇到的问题是一个win32api。不,您应该
pointpt1=新点(RECT.LEFT,RECT.TOP)User32.INSTANCE.ClientToScreen(hwnd,pt1)
;与pt2相同(RECT.RIGHT,RECT.BOTOM)。最后,根据该点重置
dimensionsOfWindow
的RECT值。