(四)創建抓取圖象的單元文件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; //設備描述表的句柄
begin
result:=TBitmap.Create ;
with Result,ARect do
begin
Width :=Right-left;
Height:=Bottom-Top;
ScreenDC:=GetDC(0); //獲取一個窗口的設備描述表的句柄,0參數返回屏幕窗口設備描述表的句柄
try
//BOOL BitBlt(hdcDest,nXDest,nYDest,nWidth,nHeight,hdcSrc,nXSrc,nYSrc,dwRop)
//把位圖從源設備描述表hdcSrc復制到目標設備描述表hdcDest,
//光柵操作碼dwRop指定了 源圖的組合方式
BitBlt(Canvas.Handle ,0,0,Width,Height,ScreenDC,left,top,SRCCOPY);
finally
ReleaseDC(0,ScreenDC);
end;
end;
end;
//全屏抓圖
function CaptureScreen:TBitmap;
begin
with Screen do
result:=CaptureScreenRect(Rect(0,0,width,height));
end;
//抓取一個窗體或控件的客戶區圖象
function CaptureClIEntImage(Control:TControl):TBitmap;
begin
//Control.ClientOrigin是控件客戶區的左上角位置。x,y是 ClIEntOrigin的變量
with Control,Control.ClIEntOrigin do
result:=CaptureScreenRect(Bounds(x,y,ClientWidth,ClIEntHeight));
end;
// 抓取一整個窗體或控件
function CaptureControlImage(Control:TControl):TBitmap;
begin
with Control do
if Parent=nil then //無父窗體,根據它的位置,直接抓取
result:=CaptureScreenRect(Bounds(left,top,width,height))
else //有父窗體,把它轉化為相對於屏幕坐標,再 抓取
with Parent.ClIEntToScreen(Point(Left,top))do
result:=CaptureScreenRect(Bounds(x,y,width,height));
end;
//根據窗體句柄進行抓取
function CaptureWindowImage(Wnd:HWND):TBitmap;
var R:TRect;
begin
GetWindowRect(wnd,R); //把窗口句柄指定的窗口坐標放入TRect
result:=CaptureScreenRect(R);
end;
end.