(IV) Create a unit file that captures images ScrnCpt
unit ScrnCpt;
interface
uses windows, forms, controls, classes, Graphics;
function CaptureScreenRect(ARect:TRect):TBitmap;
function CaptureScreen:TBitmap;
function CaptureClientImage(Control:TControl):TBitmap;
function CaptureControlImage(Control:TControl):TBitmap;
function CaptureWindowImage(Wnd:HWND):TBitmap;
Implementation
function CaptureScreenRect(ARect:TRect):TBitmap;
var ScreenDC:HDC; // Handle of device description table
Begin
result:=TBitmap.Create;
with Result,ARect do
Begin
Width :=Right-left;
Height:=Bottom-Top;
ScreenDC:=GetDC(0); //Get the handle of the device description table of a window, and the 0 parameter returns the handle of the device description table of the screen window
try
//BOOL BitBlt(hdcDest,nXDest,nYDest,nWidth,nHeight,hdcSrc,nXSrc,nYSrc,dwRop)
//Copy the bitmap from the source device description table hdcSrc to the target device description table hdcDest,
//The raster opcode dwRop specifies the combination method of the source diagram
BitBlt(Canvas.Handle,0,0,Width,Height,ScreenDC,left,top,SRCCOPY);
Finally
ReleaseDC(0,ScreenDC);
end;
end;
end;
//Full screen image capture
function CaptureScreen:TBitmap;
Begin
with Screen do
result:=CaptureScreenRect(Rect(0,0,width,height));
end;
//Crawl the client area image of a form or control
function CaptureClientImage(Control:TControl):TBitmap;
Begin
//Control.ClientOrigin is the upper left corner of the control client area. x,y is a variable of ClientOrigin
with Control,Control.ClientOrigin do
result:=CaptureScreenRect(Bounds(x,y,ClientWidth,ClientHeight));
end;
// Crawl an entire form or control
function CaptureControlImage(Control:TControl):TBitmap;
Begin
with Control do
if Parent=nil then //There is no parent form, directly grab it according to its location
result:=CaptureScreenRect(Bounds(left,top,width,height))
else //There is a parent form, convert it into relative to the screen coordinates, and then grab it
with Parent.ClientToScreen(Point(Left,top))do
result:=CaptureScreenRect(Bounds(x,y,width,height));
end;
//Crawl according to the form handle
function CaptureWindowImage(Wnd:HWND):TBitmap;
var R:TRect;
Begin
GetWindowRect(wnd,R); //Put the window coordinates specified by the window handle into TRect
result:=CaptureScreenRect(R);
end;
end.