2022企业服务(新型)提供商TOP150
全世界各行各业联合起来,internet一定要实现!

利用Delphi实现远程屏幕抓取

2004-02-14 eNet&Ciweek

  其中ScreenCap是自定义函数,截取屏幕指定区域,

  代码如下:

  procedure TClient.ScreenCap(LeftPos,TopPos,

  RightPos,BottomPos:integer);

  var

  RectWidth,RectHeight:integer;

  SourceDC,DestDC,Bhandle:integer;

  Bitmap:TBitmap;

  begin

  RectWidth:=RightPos-LeftPos;

  RectHeight:=BottomPos-TopPos;

  SourceDC:=CreateDC('DISPLAY','','',nil);

  DestDC:=CreateCompatibleDC(SourceDC);

  Bhandle:=CreateCompatibleBitmap(SourceDC,

  RectWidth,RectHeight);

  SelectObject(DestDC,Bhandle);

  BitBlt(DestDC,0,0,RectWidth,RectHeight,SourceDC,

  LeftPos,TopPos,SRCCOPY);

  Bitmap:=TBitmap.Create;

  Bitmap.Handle:=BHandle;

  BitMap.SaveToStream(BmpStream);

  BmpStream.Position:=0;

  LeftSize:=BmpStream.Size;

  Bitmap.Free;

  DeleteDC(DestDC);

  ReleaseDC(Bhandle,SourceDC);

  end;

  存为“C:\VClient\ClnUnit.pas”和

  “C:\VClient\VClient.dpr”,

  并编译。

  ---- 第二步,编制VServer.exe文件。新建Delphi工程,将窗体的Name属性设为“Server”。加入TNMUDP控件,Name属性设为“SUDP”;LocalPort属性设为“2222”,让控件SUDP监视主控机的2222端口,当有数据发送到该口时,触发控件SUDP的OnDataReceived事件;RemotePort属性设为“1111”,当控件SUDP发送数据时,将数据发到受控机的1111口。加入控件Image1,Align属性设为“alClient”;加入控件Button1,Caption属性设为“截屏”;加入控件Label1,Caption属性设为“左:上:右:下”;加入控件Edit1,Text属性设为“0:0:100:100”;加入控件Label2,

  Caption属性设为“受控机IP地址”;加入控件Edit2,Text属性设为“127.0.0.1”;  

  在implementation后面加入变量定义

  const BufSize=2048;

  var

  RsltStream,TmpStream:TMemoryStream;  

  为Server的OnCreate事件添加代码:

  procedure TServer.FormCreate(Sender: TObject);

  begin

  RsltStream:=TMemoryStream.Create;

  TmpStream:=TMemoryStream.Create;

  end;

  为Client的OnDestroy事件添加代码:

  procedure TServer.FormDestroy(Sender: TObject);

  begin

  RsltStream.Free;

  TmpStream.Free;

  end;

  为控件Button1的OnClick事件添加代码:

  procedure TServer.Button1Click(Sender: TObject);

  var ReqCode:array[0..29] of char;ReqCodeStr:string;

  begin

  ReqCodeStr:='show'+Edit1.Text;

  StrpCopy(ReqCode,ReqCodeStr);

  TmpStream.Clear;

  RsltStream.Clear;

  SUDP.RemoteHost:=Edit2.Text;

  SUDP.SendBuffer(ReqCode,30);

  end;

  

  为控件SUDP的OnDataReceived事件添加代码:

  procedure TServer.SUDPDataReceived(Sender: TComponent;

  NumberBytes: Integer; FromIP: String);

  var ReqCode:array[0..29] of char;ReqCodeStr:string;

  begin

  ReqCodeStr:='show'+Edit1.text;

  StrpCopy(ReqCode,ReqCodeStr);

  SUDP.ReadStream(TmpStream);

  RsltStream.CopyFrom(TmpStream,NumberBytes);

  if NumberBytes< BufSize then { 数据已读完 }

  begin

  RsltStream.Position:=0;

  Image1.Picture.Bitmap.LoadFromStream(RsltStream);

  TmpStream.Clear;

  RsltStream.Clear;

  end

  else

  begin

  TmpStream.Clear;

  ReqCode:='show';

  SUDP.RemoteHost:=Edit2.Text;

  SUDP.SendBuffer(ReqCode,30);

  end;

  end;  

  存为“C:\VServer\SvrUnit.pas”和

  “C:\VServer\VServer.dpr”,并编译。  

  ---- 四、测试。  

  ---- 1、本地机测试:在本地机同时运行Vserver.exe和VClient.exe,利用程序的默认设置,即可实现截屏。查看“控制面板”-“网络”-“TCP/IP”-“IP地址”,将程序的“客户IP地址”设为该地址 ,同样正常运行。

  ---- 2、远程测试:选一台受控机,运行VClient.exe;另选一台主控机,运行VServer.exe,将“受控机IP地址”即Edit2的内容设为受控机的IP地址,“截屏”即可。以上简要介绍了远程屏幕抓取的实现方法,至于在主控机上一屏同时监视多个受控机,读者可自行完善。以上程序,在Windows98对等网、Delphi 4.0下调试通过。

相关频道: eNews

您对本文或本站有任何意见,请在下方提交,谢谢!

投稿信箱:tougao@enet16.com