其中ScreenCap是自定義函數(shù),截取屏幕指定區(qū)域,
代碼如下:
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屬性設(shè)為“Server”。加入TNMUDP控件,Name屬性設(shè)為“SUDP”;LocalPort屬性設(shè)為“2222”,讓控件SUDP監(jiān)視主控機(jī)的2222端口,當(dāng)有數(shù)據(jù)發(fā)送到該口時(shí),觸發(fā)控件SUDP的OnDataReceived事件;RemotePort屬性設(shè)為“1111”,當(dāng)控件SUDP發(fā)送數(shù)據(jù)時(shí),將數(shù)據(jù)發(fā)到受控機(jī)的1111口。加入控件Image1,Align屬性設(shè)為“alClient”;加入控件Button1,Caption屬性設(shè)為“截屏”;加入控件Label1,Caption屬性設(shè)為“左:上:右:下”;加入控件Edit1,Text屬性設(shè)為“0:0:100:100”;加入控件Label2,
Caption屬性設(shè)為“受控機(jī)IP地址”;加入控件Edit2,Text屬性設(shè)為“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 { 數(shù)據(jù)已讀完 }
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、本地機(jī)測試:在本地機(jī)同時(shí)運(yùn)行Vserver.exe和VClient.exe,利用程序的默認(rèn)設(shè)置,即可實(shí)現(xiàn)截屏。查看“控制面板”-“網(wǎng)絡(luò)”-“TCP/IP”-“IP地址”,將程序的“客戶IP地址”設(shè)為該地址 ,同樣正常運(yùn)行。
---- 2、遠(yuǎn)程測試:選一臺(tái)受控機(jī),運(yùn)行VClient.exe;另選一臺(tái)主控機(jī),運(yùn)行VServer.exe,將“受控機(jī)IP地址”即Edit2的內(nèi)容設(shè)為受控機(jī)的IP地址,“截屏”即可。以上簡要介紹了遠(yuǎn)程屏幕抓取的實(shí)現(xiàn)方法,至于在主控機(jī)上一屏同時(shí)監(jiān)視多個(gè)受控機(jī),讀者可自行完善。以上程序,在Windows98對(duì)等網(wǎng)、Delphi 4.0下調(diào)試通過。