看看我的程序虽然是用CB写的,但改成delphi的也很容易。(类pcanywhere)
服务器端:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "jpeg.hpp"
#include "server.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Trayicon"
#pragma resource "*.dfm"
const BufSize=2048;
TForm1 *Form1;
long LeftSize;
TMemoryStream *jpegStream;
DWORD (__stdcall *RegisterServiceProcess)(DWORD dwProcessId,DWORD dwType);
bool clo,yk;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
HINSTANCE hinst=LoadLibrary("KERNEL32.DLL");
DWORD a=GetCurrentProcessId();
(FARPROC)RegisterServiceProcess=GetProcAddress(hinst,"RegisterServiceProcess");
RegisterServiceProcess(a,1);
jpegStream=new TMemoryStream();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
jpegStream->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode=0;
jpegStream->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
AnsiString rstr;
rstr=Socket->ReceiveText();
if(rstr.SubString(1,5)=="mouse")//&&yk
{
int x,y;
x=StrToInt(rstr.SubString(9,4));
y=StrToInt(rstr.SubString(13,4));
SetCursorPos(x,y);
if(rstr.SubString(6,3)=="dbl")
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 );
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 );
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );
}
if(rstr.SubString(6,3)=="lef"){
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 );
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );
}
if(rstr.SubString(6,3)=="rig"){
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0 );
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 );
}
}
if (rstr =="show")sendscreen();
if (rstr=="stop")jpegStream->Clear();
if(rstr=="close")
{
clo=true;
Close();
}
}
//---------------------------------------------------------------------------
void TForm1::sendscreen()
{
long sendsize;
char buf[BufSize-1];
if (jpegStream->Size==0)snapscreen();
if (LeftSize>BufSize)
sendsize=BufSize;
else
sendsize=LeftSize;
jpegStream->ReadBuffer(buf,sendsize);
LeftSize=LeftSize-sendsize;
if (LeftSize==0)jpegStream->Clear();
try
{
ServerSocket1->Socket->Connections[0]->SendBuf(buf,sendsize);
}
catch(...)
{
jpegStream->Clear();
}
}
void TForm1::snapscreen()
{
Graphics::TBitmap *bmpscreen= new Graphics::TBitmap();
TJPEGImage *jpegscreen= new TJPEGImage();
HDC ScreenDC;
ScreenDC=GetDC(0);
bmpscreen->Width=Screen->Width;
bmpscreen->Height=Screen->Height;
BitBlt(bmpscreen->Canvas->Handle, 0, 0,Screen->Width,Screen->Height, ScreenDC,0,0, SRCCOPY);
ReleaseDC(0, ScreenDC);
jpegscreen->Assign (bmpscreen);
jpegscreen->CompressionQuality=99;
jpegscreen->SaveToStream (jpegStream);
jpegStream->Position =0;
LeftSize=jpegStream->Size;
bmpscreen->Free();
jpegscreen->Free();
}
void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
{
CanClose=clo;
}
//---------------------------------------------------------------------------
客户端:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "jpeg.hpp"
#include "Unit1.h"
#include "screen.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
const bufsize=2048;
TForm1 *Form1;
TMemoryStream *stream;
TJPEGImage *jpegscreen;
bool isthieve;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::bt_connectClick(TObject *Sender)
{
AnsiString ip=InputBox("建立连接","请输入被察看的机器的IP地址或名称:","172.16.2");
if(ip!="")
{
ClientSocket1->Active=false;
ClientSocket1->Port=15333;
ClientSocket1->Host=ip;
try{
ClientSocket1->Active=true;
}
catch(...)
{
ShowMessage("连接失败!");
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::bt_disconnectClick(TObject *Sender)
{
Form2->Close();
isthieve=false;
ClientSocket1->Active=false;
stream->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::bt_stopClick(TObject *Sender)
{
isthieve=false;
ClientSocket1->Socket->SendText("stop");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::bt_thieveClick(TObject *Sender)
{
isthieve=true;
Form2=new TForm2(this);
Form2->Show();
stream->Clear();
ClientSocket1->Socket->SendText("show");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
ShowMessage("连接成功!");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode=0;
ShowMessage("不能连接!");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
char buf[bufsize];
int numberbytes;
if (isthieve)
{
numberbytes=Socket->ReceiveLength();
Socket->ReceiveBuf(buf,numberbytes);
Socket->SendText("show");
stream->Write(buf,numberbytes);
if(numberbytes<2048){
stream->Position=0;
try{
jpegscreen->LoadFromStream(stream);
Form2->Image1->Picture->Bitmap->Assign(jpegscreen);
}
catch(...){}
stream->Clear();
}
}
if (!isthieve)
Socket->SendText("stop");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
stream=new TMemoryStream();
stream->Clear();
jpegscreen=new TJPEGImage();
isthieve=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::bt_closeClick(TObject *Sender)
{
ClientSocket1->Active=false;
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
ClientSocket1->Socket->SendText("complete");
ClientSocket1->Active=false;
stream->Free();
jpegscreen->Free();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
ClientSocket1->Socket->SendText("close");
if (isthieve)ShowMessage("OK");
}
//---------------------------------------------------------------------------
显示对方机器图像的:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "screen.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm2::sendkey(TMessage msg)
{
}
void __fastcall TForm2::Image1DblClick(TObject *Sender)
{
tagPOINT xy;
GetCursorPos(&xy);
AnsiString sx,sy;
sx=(IntToStr(xy.x)).SetLength(4);
sy=(IntToStr(xy.y)).SetLength(4);
AnsiString s="mousedbl"+sx+sy;
Form1->ClientSocket1->Socket->SendText(s);
Form1->ClientSocket1->Socket->SendText("show");
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if(Shift.Contains(ssLeft))
{
tagPOINT xy;
GetCursorPos(&xy);
AnsiString sx,sy;
sx=(IntToStr(xy.x)).SetLength(4);
sy=(IntToStr(xy.y)).SetLength(4);
AnsiString s="mouselef"+sx+sy;
Form1->ClientSocket1->Socket->SendText(s);
}
else
{
tagPOINT xy;
GetCursorPos(&xy);
AnsiString sx,sy;
sx=(IntToStr(xy.x)).SetLength(4);
sy=(IntToStr(xy.y)).SetLength(4);
AnsiString s="mouserig"+sx+sy;
Form1->ClientSocket1->Socket->SendText(s);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormClose(TObject *Sender, TCloseAction &Action)
{
this->Free();
}
//---------------------------------------------------------------------------