unit ClnUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
NMUDP,jpeg, ExtCtrls;
type
TClient = class(TForm)
cUDP: TNMUDP;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure cUDPDataReceived(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
procedure ScreenCap(LeftPos,TopPos,RightPos,BottomPos:integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Client: TClient;
implementation
const BufSize=2048;{ 发送每一笔数据的缓冲区大小 }
var
BmpStream:TMemoryStream;
myjpeg: TJPEGImage;
LeftSize:Longint;{ 发送每一笔数据后剩余的字节数 }
Bitmap:TBitmap;
{$R *.DFM}
procedure TClient.FormCreate(Sender: TObject);
begin
BmpStream:=TMemoryStream.Create;
myjpeg := TJPEGImage.Create;
// 创建JPEG图象
Bitmap:=TBitmap.Create;
end;
procedure TClient.FormDestroy(Sender: TObject);
begin
BmpStream.Free;
myjpeg.Free;
// 释放资源
Bitmap.Free;
end;
procedure TClient.cUDPDataReceived(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
var
CtrlCode:array[0..29] of char;
Buf:array[0..BufSize-1] of char;
TmpStr:string;
SendSize,LeftPos,TopPos,RightPos,BottomPos:integer;
begin
CUDP.ReadBuffer(CtrlCode,NumberBytes);{ 读取控制码 }
if CtrlCode[0]+CtrlCode[1]+CtrlCode[2]+CtrlCode[3]='show' then
begin
{ 控制码前4位为“show”表示主控机发出了抓屏指令 }
if BmpStream.Size=0 then
{ 没有数据可发,必须截屏生成数据 }
begin
TmpStr:=StrPas(CtrlCode);
TmpStr:=Copy(TmpStr,5,Length(TmpStr)-4);
LeftPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
TmpStr:=Copy(TmpStr,Pos(':',TmpStr)+1,Length(TmpStr)
-Pos(':',TmpStr));
TopPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
TmpStr:=Copy(TmpStr,Pos(':',TmpStr)+1,Length(TmpStr)-
Pos(':',TmpStr));
RightPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
BottomPos:=StrToInt(Copy(TmpStr,Pos(':',TmpStr
)+1,Length(TmpStr)-Pos(':',TmpStr)));
ScreenCap(LeftPos,TopPos,RightPos,BottomPos);
{ 截取屏幕 }
end;
if LeftSize>BufSize then
SendSize:=BufSize
else
SendSize:=LeftSize;
BmpStream.ReadBuffer(Buf,SendSize);
LeftSize:=LeftSize-SendSize;
if LeftSize=0 then
BmpStream.Clear;{ 清空流 }
CUDP.RemoteHost:=FromIP;
{ FromIP为主控机IP地址 }
CUDP.SendBuffer(Buf,SendSize);
{ 将数据发到主控机的2222口 }
end;
//------------------
if CtrlCode[0]+CtrlCode[1]+CtrlCode[2]+CtrlCode[3]='move' then
begin
TmpStr:=StrPas(CtrlCode);
TmpStr:=Copy(TmpStr,5,Length(TmpStr)-4);
LeftPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
TmpStr:=Copy(TmpStr,Pos(':',TmpStr)+1,Length(TmpStr)-Pos(':',TmpStr));
TopPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
mouse_event(MOUSEEVENTF_MOVE or MOUSEEVENTF_ABSOLUTE,LeftPos*65535 div 800,TopPos * 65535 div 600,0,GetMessageExtraInfo());
end;
if CtrlCode[0]+CtrlCode[1]+CtrlCode[2]+CtrlCode[3]='ldon' then
begin
TmpStr:=StrPas(CtrlCode);
TmpStr:=Copy(TmpStr,5,Length(TmpStr)-4);
LeftPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
TmpStr:=Copy(TmpStr,Pos(':',TmpStr)+1,Length(TmpStr)-Pos(':',TmpStr));
TopPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
mouse_event(MOUSEEVENTF_LEFTDOWN or MOUSEEVENTF_ABSOLUTE,LeftPos*65535 div 800,TopPos * 65535 div 600,0,GetMessageExtraInfo());
end;
if CtrlCode[0]+CtrlCode[1]+CtrlCode[2]+CtrlCode[3]='lbup' then
begin
TmpStr:=StrPas(CtrlCode);
TmpStr:=Copy(TmpStr,5,Length(TmpStr)-4);
LeftPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
TmpStr:=Copy(TmpStr,Pos(':',TmpStr)+1,Length(TmpStr)-Pos(':',TmpStr));
TopPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
mouse_event(MOUSEEVENTF_LEFTUP or MOUSEEVENTF_ABSOLUTE,LeftPos*65535 div 800,TopPos * 65535 div 600,0,GetMessageExtraInfo());
end;
if CtrlCode[0]+CtrlCode[1]+CtrlCode[2]+CtrlCode[3]='rdon' then
begin
TmpStr:=StrPas(CtrlCode);
TmpStr:=Copy(TmpStr,5,Length(TmpStr)-4);
LeftPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
TmpStr:=Copy(TmpStr,Pos(':',TmpStr)+1,Length(TmpStr)-Pos(':',TmpStr));
TopPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
mouse_event(MOUSEEVENTF_RIGHTDOWN or MOUSEEVENTF_ABSOLUTE,LeftPos*65535 div 800,TopPos * 65535 div 600,0,GetMessageExtraInfo());
end;
if CtrlCode[0]+CtrlCode[1]+CtrlCode[2]+CtrlCode[3]='rbup' then
begin
TmpStr:=StrPas(CtrlCode);
TmpStr:=Copy(TmpStr,5,Length(TmpStr)-4);
LeftPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
TmpStr:=Copy(TmpStr,Pos(':',TmpStr)+1,Length(TmpStr)-Pos(':',TmpStr));
TopPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
mouse_event(MOUSEEVENTF_RIGHTUP or MOUSEEVENTF_ABSOLUTE,LeftPos*65535 div 800,TopPos * 65535 div 600,0,GetMessageExtraInfo());
end;
end;
procedure TClient.ScreenCap(LeftPos,TopPos,RightPos,BottomPos:integer);
begin
Bitmap.Width:=800;
Bitmap.Height:=600;
Bitmap.PixelFormat:=pf8bit;
Application.ProcessMessages;
BitBlt(bitmap.Canvas.Handle,0,0,800,600,getdc(0),
0,0,SRCCOPY);
myjpeg.Assign(bitmap);
Application.ProcessMessages;
//myjpeg.compress;
//myjpeg.CompressionQuality := 4;
myjpeg.SaveToStream(BmpStream);
BmpStream.Position:=0;
LeftSize:=BmpStream.Size;
Application.ProcessMessages;
end;
end.