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
  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('cao.bmp');
  finally
    bmp.Free;
  end;
end;

解决方案 »

  1.   

    招聘C# delphi软件工程师 
      

  2.   


    const
      PW_CLIENTONLY = 1;var
      PrintWindow: function(sHandle: HWND; dHandle: HDC; nFlags: UINT): BOOL; stdcall;function Screenshot(wnd: HWND; const bmp: TBitmap32): Boolean;
    var
      rec: TRect;
    begin  GetClientRect(wnd, rec);
      bmp.SetSize((rec.Right - rec.Left), (rec.Bottom - rec.Top));  bmp.Canvas.Lock;
      try
        Result := PrintWindow(wnd, bmp.Canvas.Handle, PW_CLIENTONLY);  finally
        bmp.Canvas.Unlock;
      end;
    end;initialization
      @PrintWindow := GetProcAddress(LoadLibrary('user32.dll'), 'PrintWindow');
      

  3.   

    bmp.SaveToFile('cao.bmp');这句亮了。
      

  4.   

    前提是那个窗口必须处理了WM_Print或者WM_PrintClient消息.好在VCL,MFC框架默认处理了.
    有些没处理或者故意屏蔽此消息的窗口还是不行的