Delphi Active Window Screenshot

前端 未结 2 2016
梦谈多话
梦谈多话 2020-12-22 01:06

I am trying to add capture screenshot of active window using this code

procedure ScreenShot(activeWindow: bool; destBitmap : TBitmap) ;
 var
    w,h : intege         


        
相关标签:
2条回答
  • 2020-12-22 01:34

    I have tested and got the same result.

    enter image description here

    original with border

    enter image description here

    But if you set

    sSkinProvider1.AllowExtBorders:=False;
    

    you get a screenshot without the transparent roundet border.

    enter image description here

    then set back

    sSkinProvider1.AllowExtBorders:=True;
    

    No need to do after that a second

    Form1.Repaint;
    

    You will see only a short switch.

    procedure TForm1.BitBtn1Click(Sender: TObject);
    var
      path:string;
      b:TBitmap;
    begin
       sSkinProvider1.AllowExtBorders:=False;
       Form1.Repaint;
       path:= ExtractFilePath(Application.ExeName) + 'Screenshot\';
       b := TBitmap.Create;
       try
         ScreenShot(TRUE, b) ;
         b.SaveToFile(path + 'Screenshot_1.png');
       finally
         b.FreeImage;
         FreeAndNil(b) ;
         sSkinProvider1.AllowExtBorders:=True;
    [...]
    

    btw. do not set the path like

    path:= ExtractFilePath(Application.ExeName) + '/Screenshot/';
    

    use windows style backslash and only one

    path:= ExtractFilePath(Application.ExeName) + 'Screenshot\'; 
    

    Tested with Delphi5

    0 讨论(0)
  • 2020-12-22 01:46

    I don't know what kind of visual styling components you're using (the form icon indicates it's Delphi 7, though).

    This code works perfectly for me:

    function CaptureWindow(const WindowHandle: HWnd): TBitmap;
    var
      DC: HDC;
      wRect: TRect;
      Width, Height: Integer;
    begin
      DC := GetWindowDC(WindowHandle);
      Result := TBitmap.Create;
      try
        GetWindowRect(WindowHandle, wRect);
        Width := wRect.Right - wRect.Left;
        Height := wRect.Bottom - wRect.Top;
        Result.Width := Width;
        Result.Height := Height;
        Result.Modified := True;
        BitBlt(Result.Canvas.Handle, 0, 0, Width, Height, DC, 0, 0, SRCCOPY);
      finally
        ReleaseDC(WindowHandle, DC);
      end;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Capture: TBitmap;
    begin
      //  For active window, change Handle to GetForegroundWindow()
      Capture := CaptureWindow(Handle);  
      try
        Capture.SaveToFile('E:\TempFiles\ScreenCapture2014.bmp');
      finally
        Capture.Free;
      end;
    end;
    

    Here's the image I captured:

    Sample form window capture

    0 讨论(0)
提交回复
热议问题