(四)创建抓取图象的单元文件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.