在Windows10(可能还有W8)之前,我们可以使用一个简单的BitBlt来捕获一个窗口:
function WindowSSToBmp(h: hWnd; var b: TBitmap): Boolean;
Var DC: HDC;
r: TRect;
begin
Windows.GetWindowRect(h,r);
b.Width := r.Right - r.Left;
b.height := r.Bottom - r.Top;
DC := GetWindowDC(GetDesktopWindow);
BitBlt(b.Canvas.Handle,0,0,b.Width,b.height,DC,r.Left,r.Top,SRCCOPY);
ReleaseDC(0,DC);
Result := True;
end;
但是从W10开始,这会在窗口周围添加一些额外的像素,可能因为矩形包含边缘处阴影轮廓的一部分,因此它捕获了其下方其他窗口的一部分:
在W10中执行此操作的正确方法是什么?请注意,如果您使用Alt + PrtScr,它将获得窗口的正确实际可见区域,而不会有额外的边缘。