[英]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.