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;
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.
发布评论
评论(1)
您可以使用此功能来截取所有可见表单占用的桌面区域的屏幕截图:
使用“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:
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.