如何获取应用程序所在屏幕的屏幕截图?

发布于 2025-01-09 04:53:46 字数 1437 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热风软妹 2025-01-16 04:53:46

您可以使用此功能来截取所有可见表单占用的桌面区域的屏幕截图:

USES System.Math, WinAPI.DwmApi;

FUNCTION ScreenGrab(IncludeShadow : BOOLEAN = FALSE ; BorderSize : Cardinal = 8 ; BackgroundColor : TColor = clBlack) : TBitMap;
  VAR
    MinX,MinY,MaxX,MaxY,I : INTEGER;
    F                     : TForm;
    DC                    : HDC;
    R                     : TRect;

  FUNCTION GetWindowRect(F : TForm) : TRect;
    BEGIN
      IF IncludeShadow THEN
        Result:=F.BoundsRect
      ELSE
        DwmGetWindowAttribute(F.Handle,DWMWA_EXTENDED_FRAME_BOUNDS,@Result,SizeOf(TRect))
    END;

  BEGIN
    MinX:=MAXINT; MinY:=MinX; MaxX:=-MAXINT; MaxY:=MaxX;
    FOR I:=0 TO PRED(Screen.FormCount) DO BEGIN
      F:=Screen.Forms[I];
      IF F.Visible THEN BEGIN
        R:=GetWindowRect(F);
        MinX:=Min(MinX,R.Left); MinY:=Min(MinY,R.Top);
        MaxX:=Max(MaxX,R.Left+R.Width); MaxY:=Max(MaxY,R.Top+R.Height)
      END
    END;
    DEC(MinX,BorderSize); DEC(MinY,BorderSize);
    INC(MaxX,BorderSize); INC(MaxY,BorderSize);
    Result:=TBitMap.Create(MaxX-MinX,MaxY-MinY);
    TRY
      Result.PixelFormat:=pf24bit;
      Result.Canvas.Brush.Style:=TBrushStyle.bsSolid;
      Result.Canvas.Brush.Color:=BackgroundColor;
      Result.Canvas.FillRect(Rect(0,0,Result.Width,Result.Height));
      DC:=GetDC(0);
      TRY
        FOR I:=0 TO PRED(Screen.FormCount) DO BEGIN
          F:=Screen.Forms[I];
          IF F.Visible THEN BEGIN
            R:=GetWindowRect(F);
            BitBlt(Result.Canvas.Handle,R.Left-MinX,R.Top-MinY,R.Width,R.Height,DC,R.Left,R.Top,SRCCOPY)
          END
        END
      FINALLY
        ReleaseDC(0,DC)
      END
    EXCEPT
      Result.Free;
      RAISE
    END
  END;

使用“IncludeShadow”包含表单周围的阴影区域(可能包括表单后面窗口的屏幕信息)。您还可以指定抓取区域周围的“BorderSize”,并且可以指定返回图像的背景颜色(表单未占用的区域)。

如果您的表单分布在多个显示器上,它也适用。但请注意,如果您的显示器具有不同的 DPI,则合成图像是原始像素,因此最终 .BMP 文件中的表单可能具有不同的比例,具体取决于它们所在的显示器。

You can use this function to take a screen grab of the desktop area occupied by all of your visible forms:

USES System.Math, WinAPI.DwmApi;

FUNCTION ScreenGrab(IncludeShadow : BOOLEAN = FALSE ; BorderSize : Cardinal = 8 ; BackgroundColor : TColor = clBlack) : TBitMap;
  VAR
    MinX,MinY,MaxX,MaxY,I : INTEGER;
    F                     : TForm;
    DC                    : HDC;
    R                     : TRect;

  FUNCTION GetWindowRect(F : TForm) : TRect;
    BEGIN
      IF IncludeShadow THEN
        Result:=F.BoundsRect
      ELSE
        DwmGetWindowAttribute(F.Handle,DWMWA_EXTENDED_FRAME_BOUNDS,@Result,SizeOf(TRect))
    END;

  BEGIN
    MinX:=MAXINT; MinY:=MinX; MaxX:=-MAXINT; MaxY:=MaxX;
    FOR I:=0 TO PRED(Screen.FormCount) DO BEGIN
      F:=Screen.Forms[I];
      IF F.Visible THEN BEGIN
        R:=GetWindowRect(F);
        MinX:=Min(MinX,R.Left); MinY:=Min(MinY,R.Top);
        MaxX:=Max(MaxX,R.Left+R.Width); MaxY:=Max(MaxY,R.Top+R.Height)
      END
    END;
    DEC(MinX,BorderSize); DEC(MinY,BorderSize);
    INC(MaxX,BorderSize); INC(MaxY,BorderSize);
    Result:=TBitMap.Create(MaxX-MinX,MaxY-MinY);
    TRY
      Result.PixelFormat:=pf24bit;
      Result.Canvas.Brush.Style:=TBrushStyle.bsSolid;
      Result.Canvas.Brush.Color:=BackgroundColor;
      Result.Canvas.FillRect(Rect(0,0,Result.Width,Result.Height));
      DC:=GetDC(0);
      TRY
        FOR I:=0 TO PRED(Screen.FormCount) DO BEGIN
          F:=Screen.Forms[I];
          IF F.Visible THEN BEGIN
            R:=GetWindowRect(F);
            BitBlt(Result.Canvas.Handle,R.Left-MinX,R.Top-MinY,R.Width,R.Height,DC,R.Left,R.Top,SRCCOPY)
          END
        END
      FINALLY
        ReleaseDC(0,DC)
      END
    EXCEPT
      Result.Free;
      RAISE
    END
  END;

Use "IncludeShadow" to include the shadow area around your forms (which may include screen info from the windows behind your forms). You can also specify a "BorderSize" around the grabbed area, and you may specify the background colour (of the area not occupied by your forms) of the returned image.

It also works if your forms are spread across multiple monitors. Please note, however, that if your monitors have different DPI, the composite image is of the raw pixels, so the forms may be of different scale in the final .BMP file, depending on which monitor they are located on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文