答案 0 :(得分:4)
你那里有很多不必要的,过于复杂的代码。
这对我有用:
procedure ScreenShotWindow;
var
DC: HDC;
wRect: TRect;
Bmp: TBitmap;
Width, Height: Integer;
H: HWnd;
begin
H := FindWindow(nil, 'Untitled - Notepad');
if H = 0 then
raise Exception.Create('FindWindow failed.'); // GetLastError would tell you why.
// I leave that to you to add if needed
DC := GetWindowDC(H);
try
Bmp := TBitmap.Create;
try
GetWindowRect(H, wRect);
Width := wRect.Right - wRect.Left;
Height := wRect.Bottom - wRect.Top;
Bmp.SetSize(Width, Height);
BitBlt(Bmp.Canvas.Handle, 0, 0, Width, Height, DC, 0, 0, SRCCOPY);
Bmp.SaveToFile('test' + RandomPassword(10) + '.bmp');
finally
Bmp.Free;
end;
finally
ReleaseDC(H, DC);
end;
end;