Reputation:
I have this code below that capture a specific window, the window is captured with sucess, but only part of window is showed.
How solve?
Thank you.
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;
Upvotes: 3
Views: 244
Reputation: 125620
You've got a lot of unnecessary, overly complicated code there.
This works for me:
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;
Upvotes: 4