首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在Delphi中截取Active Window的截图?

要在Delphi中截取Active Window的截图,可以使用以下步骤:

  1. 首先,需要在Delphi中添加一个TImage控件,用于显示截图。
  2. 然后,在代码中添加以下代码:
代码语言:txt
复制
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单元。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的视频

领券