0
点赞
收藏
分享

微信扫一扫

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;

举报

相关推荐

0 条评论