要在Delphi中截取Active Window的截图,可以使用以下步骤:
uses
Winapi.Windows, Winapi.GDIPAPI, Winapi.GDIPOBJ, System.SysUtils, System.Classes, Vcl.Graphics;
function CaptureScreen(hWnd: HWND): TBitmap;
var
DC: HDC;
rc: TRect;
begin
Result := TBitmap.Create;
try
GetWindowRect(hWnd, rc);
Result.Width := rc.Right - rc.Left;
Result.Height := rc.Bottom - rc.Top;
DC := GetWindowDC(hWnd);
try
Result.Canvas.CopyRect(Rect(0, 0, Result.Width, Result.Height),
TCanvas.Create, Rect(0, 0, Result.Width, Result.Height));
finally
ReleaseDC(hWnd, DC);
end;
except
Result.Free;
raise;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
hWnd: HWND;
bmp: TBitmap;
begin
hWnd := GetForegroundWindow;
bmp := CaptureScreen(hWnd);
try
Image1.Picture.Assign(bmp);
finally
bmp.Free;
end;
end;
在上面的代码中,CaptureScreen函数用于截取指定窗口的屏幕截图,并将其返回为TBitmap对象。Button1Click事件处理程序中,使用GetForegroundWindow函数获取当前活动窗口的句柄,然后调用CaptureScreen函数截取屏幕截图,并将其显示在TImage控件中。
注意:在使用上述代码时,需要确保已经引入了Winapi.Windows、Winapi.GDIPAPI、Winapi.GDIPOBJ、System.SysUtils、System.Classes、Vcl.Graphics单元。
领取专属 10元无门槛券
手把手带您无忧上云