Delphi 截取被遮挡窗口图片

(对最小化窗口无效)
(对斗地主无效,截出来的是黑屏,肯定是它做处理了,可以用另外的方法,先显示出窗口,在截屏)

Delphi截图,Delphi 截取被遮挡窗口图片,Delphi遮挡窗口截图
使用dELPHI用TCanvas 的CopyRect或者bitblt 对窗口进行截图的时候窗口对象容易被遮挡,
截出来的图像混杂着遮挡窗体,PrintWindow这个个函数可以帮我们
解决掉这个问题,不管是隐藏的窗体还是北遮挡的窗体都可以截取的到。
dELPHI中没有声明这个函数,需要我们自己动手声明

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

procedure TForm1.Button2Click(Sender: TObject);
var
  LBmp: TBitmap;
  LWnd: cardinal;
  LRect: TRect;
begin
  LWnd := FindWindow(nil, 'abc'); // 查找窗口句柄
  if LWnd = 0 then
    Exit;
  if not GetWindowRect(LWnd, LRect) then
    Exit;
  LBmp := TBitmap.Create;
  try
    LBmp.Width := LRect.Right - LRect.Left;
    LBmp.Height := LRect.Bottom - LRect.Top;
    LBmp.PixelFormat := pf24bit;
    PrintWindow(LWnd, LBmp.Canvas.Handle, 0);
    LBmp.SaveToFile('cao.bmp');
  finally
    LBmp.Free;
  end;
end;


(这个方法存在问题是,当有窗口在截屏窗口前面时,会遮挡,可以设置窗口为最前,就像任务管理器)
procedure TForm1.Button1Click(Sender: TObject);
var
  LWindow: HWND;
  LDc: HDC;
  LBmp: TBitmap;
  LRect: TRect;
begin
  LWindow := FindWindow(nil, 'abc');
  if LWindow = 0 then
    Exit;
  if not ShowWindow(LWindow, SW_SHOWNORMAL) then
    Exit;
  if not GetWindowRect(LWindow, LRect) then
    Exit;
  if not SetWindowPos(LWindow, HWND_TOPMOST, 0, 0, LRect.Right - LRect.Left, LRect.Bottom - LRect.Top, SW_SHOWNORMAL) then
    Exit;
  if not BringWindowToTop(LWindow) then
    Exit;
  
  Sleep(100);

  LDc := GetDC(LWindow);
  if LDc = 0 then
    Exit;

  LBmp := TBitmap.Create;
  try
    LBmp.Width := LRect.Right - LRect.Left;
    LBmp.Height := LRect.Bottom - LRect.Top;
    BitBlt(LBmp.Canvas.Handle, 0, 0, LBmp.Width, LBmp.Height, LDc, 0, 0, SRCCOPY);
    LBmp.SaveToFile('1.bmp');
  finally
    ReleaseDC(LWindow, LDc);
    LBmp.Free;
  end;
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值