<br><br> 第一步,编制VClient.exe文件。新建Delphi工程,将默认窗体的Name属性设为“Client”。加入TNMUDP控件,Name属性设为“CUDP”;LocalPort属性设为“1111”,让控件CUDP监视受控机的1111端口,当有数据发送到该口时,触发控件CUDP的OnDataReceived事件;RemotePort属性设为“2222”,当控件CUDP发送数据时,将数据发到主控机的2222口。<br> 在implementation后面加入变量定义 <br><br>const BufSize=2048;{ 发送每一笔数据的缓冲区大小 }<br>var<br>BmpStream:TMemoryStream;<br>LeftSize:Longint;{ 发送每一笔数据后剩余的字节数 }<br><br>为Client的OnCreate事件添加代码:<br>procedure TClient.FormCreate(Sender: TObject);<br>begin<br>BmpStream:=TMemoryStream.Create;<br>end;<br><br>为Client的OnDestroy事件添加代码:<br>procedure TClient.FormDestroy(Sender: TObject);<br>begin<br>BmpStream.Free;<br>end;<br><br>为控件CUDP的OnDataReceived事件添加代码:<br>procedure TClient.CUDPDataReceived(Sender: TComponent;<br>NumberBytes: Integer; FromIP: String);<br>var<br>CtrlCode:array[0..29] of char;<br>Buf:array[0..BufSize-1] of char;<br>TmpStr:string;<br>SendSize,LeftPos,TopPos,RightPos,BottomPos:integer;<br>begin<br>CUDP.ReadBuffer(CtrlCode,NumberBytes);{ 读取控制码 }<br>if CtrlCode[0]+CtrlCode[1]+CtrlCode[2]+CtrlCode[3]='show' then<br>begin { 控制码前4位为“show”表示主控机发出了抓屏指令 } <br>if BmpStream.Size=0 then { 没有数据可发,必须截屏生成数据 }<br>begin<br>TmpStr:=StrPas(CtrlCode);<br>TmpStr:=Copy(TmpStr,5,Length(TmpStr)-4);<br>LeftPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));<br>TmpStr:=Copy(TmpStr,Pos(':',TmpStr)+1,Length(TmpStr)<br>-Pos(':',TmpStr));<br>TopPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));<br>TmpStr:=Copy(TmpStr,Pos(':',TmpStr)+1,Length(TmpStr)-<br>Pos(':',TmpStr));<br>RightPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));<br>BottomPos:=StrToInt(Copy(TmpStr,Pos(':',TmpStr<br>)+1,Length(TmpStr)-Pos(':',TmpStr)));<br>ScreenCap(LeftPos,TopPos,RightPos,BottomPos); {<br>截取屏幕 }<br>end;<br>if LeftSize>BufSize then SendSize:=BufSize<br>else SendSize:=LeftSize;<br>BmpStream.ReadBuffer(Buf,SendSize);<br>LeftSize:=LeftSize-SendSize;<br>if LeftSize=0 then BmpStream.Clear;{ 清空流 }<br>CUDP.RemoteHost:=FromIP; { FromIP为主控机IP地址 }<br>CUDP.SendBuffer(Buf,SendSize); { 将数据发到主控机的2222口 }<br>end;<br>end;<br><br>其中ScreenCap是自定义函数,截取屏幕指定区域,<br>代码如下:<br>procedure TClient.ScreenCap(LeftPos,TopPos,<br>RightPos,BottomPos:integer);<br>var<br>RectWidth,RectHeight:integer;<br>SourceDC,DestDC,Bhandle:integer;<br>Bitmap:TBitmap;<br>begin<br>RectWidth:=RightPos-LeftPos;<br>RectHeight:=BottomPos-TopPos;<br>SourceDC:=CreateDC('DISPLAY','','',nil);<br>DestDC:=CreateCompatibleDC(SourceDC);<br>Bhandle:=CreateCompatibleBitmap(SourceDC,<br>RectWidth,RectHeight);<br>SelectObject(DestDC,Bhandle);<br>BitBlt(DestDC,0,0,RectWidth,RectHeight,SourceDC,<br>LeftPos,TopPos,SRCCOPY);<br>Bitmap:=TBitmap.Create;<br>Bitmap.Handle:=BHandle;<br>BitMap.SaveToStream(BmpStream);<br>BmpStream.Position:=0;<br>LeftSize:=BmpStream.Size;<br>Bitmap.Free;<br>DeleteDC(DestDC);<br>ReleaseDC(Bhandle,SourceDC);<br>end;<br>存为“C:/VClient/ClnUnit.pas”和“C:/VClient/VClient.dpr”,<br>并编译。<br><br> 第二步,编制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”; <br>在implementation后面加入变量定义<br>const BufSize=2048;<br>var<br>RsltStream,TmpStream:TMemoryStream;<br><br>为Server的OnCreate事件添加代码:<br>procedure TServer.FormCreate(Sender: TObject);<br>begin<br>RsltStream:=TMemoryStream.Create;<br>TmpStream:=TMemoryStream.Create;<br>end;<br><br>为Client的OnDestroy事件添加代码:<br>procedure TServer.FormDestroy(Sender: TObject);<br>begin<br>RsltStream.Free;<br>TmpStream.Free;<br>end;<br><br>为控件Button1的OnClick事件添加代码:<br>procedure TServer.Button1Click(Sender: TObject);<br>var ReqCode:array[0..29] of char;ReqCodeStr:string;<br>begin<br>ReqCodeStr:='show'+Edit1.Text;<br>StrpCopy(ReqCode,ReqCodeStr);<br>TmpStream.Clear;<br>RsltStream.Clear;<br>SUDP.RemoteHost:=Edit2.Text;<br>SUDP.SendBuffer(ReqCode,30);<br>end;<br><br>为控件SUDP的OnDataReceived事件添加代码:<br>procedure TServer.SUDPDataReceived(Sender: TComponent;<br>NumberBytes: Integer; FromIP: String);<br>var ReqCode:array[0..29] of char;ReqCodeStr:string;<br>begin<br>ReqCodeStr:='show'+Edit1.text;<br>StrpCopy(ReqCode,ReqCodeStr);<br>SUDP.ReadStream(TmpStream);<br>RsltStream.CopyFrom(TmpStream,NumberBytes);<br>if NumberBytes< BufSize then { 数据已读完 }<br>begin<br>RsltStream.Position:=0;<br>Image1.Picture.Bitmap.LoadFromStream(RsltStream);<br>TmpStream.Clear;<br>RsltStream.Clear;<br>end<br>else<br>begin<br>TmpStream.Clear;<br>ReqCode:='show';<br>SUDP.RemoteHost:=Edit2.Text;<br>SUDP.SendBuffer(ReqCode,30);<br>end;<br>end;<br><br>