gpt4 book ai didi

delphi - 如何完整捕获一个窗口?

转载 作者:行者123 更新时间:2023-12-03 15:51:36 25 4
gpt4 key购买 nike

我有下面的代码捕获特定窗口,该窗口被成功捕获,但只显示了窗口的一部分。

如何解决?

谢谢。

procedure ScreenShotWindow;
var
c: TCanvas;
r, t: TRect;
h: THandle;
Bild: TBitMap;
begin
c := TCanvas.Create;
h := FindWindow(nil, 'Untitled - Notepad');
c.Handle := GetWindowDC(h);
GetWindowRect(h, t);
try
r := Rect(0, 0, t.Right - t.Left, t.Bottom - t.Top);
Bild := TBitMap.Create;
try
Bild.Width := t.Right - t.Left;
Bild.Height := t.Bottom - t.Top;
Bild.Canvas.CopyRect(r, c, t);
Bild.SaveToFile('test'+ RandomPassword(10)+'.bmp');
finally
Bild.Free;
end;
finally
ReleaseDC(0, c.Handle);
c.Free;
end;
end;

enter image description here

最佳答案

那里有很多不必要的、过于复杂的代码。

这对我有用:

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;

关于delphi - 如何完整捕获一个窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42009352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com