delphi 后台截图

function PrintWindow(SourceWindow: hwnd; Destination: hdc; nFlags: cardinal): bool; stdcall; external 'user32.dll' name 'PrintWindow';



procedure TForm1.btn1Click(Sender: TObject);

var

  bmp : TBitmap;

  wnd : cardinal;

  rec : TRect;

begin

   wnd := FindWindow(nil, '计算器'); // 查找窗口句柄,这里用计算器演示

   GetWindowRect(wnd, rec); //获取到计算器窗口的矩形坐标

   bmp := TBitmap.Create;

   try

     bmp.Width := rec.Right - rec.Left;

     bmp.Height := rec.Bottom - rec.Top;

     bmp.PixelFormat := pf24bit;

     PrintWindow(wnd, bmp.Canvas.Handle, 0);

     bmp.SaveToFile('test.bmp');

   finally

     bmp.Free;

  end;

end;

你可能感兴趣的:(Delphi)