Implementing remote screen capture using Delphi
Shandong Juli Co., Ltd. CAD Center
Tan Geqin
---- In network management, sometimes it is necessary to understand the usage of online computers by monitoring the remote computer screen. Although there are many software on the market that can achieve this function, and some can even perform remote control, they lack flexibility in use. For example, they cannot specify the size and position of the remote computer screen area, and thus cannot monitor multiple screens on one screen at the same time. . In fact, you can use Delphi to compile a flexible remote screen capture tool, which is briefly described as follows.
---- 1. Software and hardware requirements.
---- Windows95/98 peer-to-peer network, the computer used for monitoring (hereinafter referred to as the master computer) and the computer being monitored (hereinafter referred to as the controlled computer) must be equipped with the TCP/ip protocol and configured correctly. If there is no network, you can also debug on a computer.
---- 2. Implementation method.
---- Prepare two applications, one is VClient.exe, installed on the controlled machine, and the other is VServer.exe, installed on the main control machine. VServer.exe specifies the IP address of the controlled machine to be monitored and the size and position of the area to be captured on the screen of the controlled machine, and issues a screen capture instruction to VClient.exe. After receiving the instruction, VClient.exe Select a designated area on the computer screen, generate a data stream, send it back to the host computer, and display the BMP image of the captured area on the host computer. It can be seen from the above process that there are two keys to this method: one is how to capture the screen on the controlled machine, and the other is how to transmit data between the two computers through the TCP/IP protocol.
---- UDP (User Datagram PROtocol, meaning User Message Protocol) is one of the communication protocols widely used on the Internet. Different from the TCP protocol, it is a non-connection transmission protocol with no confirmation mechanism and is not as reliable as TCP, but it is more efficient than TCP and is more suitable for remote screen monitoring. At the same time, the UDP control does not distinguish between the server and the client, only the sending end and the receiving end. The programming is relatively simple, so the UDP protocol is selected and the TNMUDP control provided by Delphi 4.0 is used.
---- 3. Create a demonstration program.
----The first step is to compile the VClient.exe file. Create a new Delphi project and set the Name property of the default form to "Client". Add the TNMUDP control, set the Name attribute to "CUDP"; set the LocalPort attribute to "1111", let the control CUDP monitor the 1111 port of the controlled machine, and when data is sent to the port, trigger the OnDataReceived event of the control CUDP; set the RemotePort attribute is "2222", when the control CUDP sends data, the data is sent to the 2222 port of the host computer.
---- Add variable definition after implementation
const BufSize=2048;{Buffer size for sending each piece of data}
var
BmpStream:TMemoryStream;
LeftSize:Longint;{The number of bytes remaining after sending each piece of data}
Add code for the Client's OnCreate event:
procedure TClient.FormCreate(Sender: TObject);
begin
BmpStream:=TMemoryStream.Create;
end;
Add code for the Client's OnDestroy event:
procedure TClient.FormDestroy(Sender: TObject);
begin
BmpStream.Free;
end;
Add code for the OnDataReceived event of the control CUDP:
procedure TClient.CUDPDataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String);
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);{Read control code}
if CtrlCode[0]+CtrlCode[1]+CtrlCode[2]+CtrlCode[3]='show' then
begin {The first 4 digits of the control code are "show", indicating that the host computer has issued a screen capture command}
if BmpStream.Size=0 then {There is no data to send, you must take a screenshot to generate the data}
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); {
Take a screenshot}
end;
if LeftSize>BufSize then SendSize:=BufSize
else SendSize:=LeftSize;
BmpStream.ReadBuffer(Buf,SendSize);
LeftSize:=LeftSize-SendSize;
if LeftSize=0 then BmpStream.Clear;{clear stream}
CUDP.RemoteHost:=FromIP; {FromIP is the host IP address}
CUDP.SendBuffer(Buf,SendSize); {Send data to port 2222 of the host computer}
end;
end;
Among them, ScreenCap is a custom function that captures the specified area of the screen.
The code is as follows:
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;
Save as "C:VClientClnUnit.pas" and "C:VClientVClient.dpr",
and compile.
----The second step is to compile the VServer.exe file. Create a new Delphi project and set the form's Name property to "Server". Add the TNMUDP control, set the Name attribute to "SUDP"; set the LocalPort attribute to "2222", let the control SUDP monitor the 2222 port of the host computer, and when data is sent to the port, trigger the OnDataReceived event of the control SUDP; set the RemotePort attribute is "1111", when the control SUDP sends data, the data is sent to the 1111 port of the controlled machine. Add the control Image1, set the Align property to "alClient"; add the control Button1, set the Caption property to "Screenshot"; add the control Label1, set the Caption property to "Left: Top: Right: Bottom"; add Enter control Edit1, and set the Text property to "0:0:100:100"; add control Label2, and set the Caption property to "controlled machine IP address"; add control Edit2, and set the Text property to "127.0.0.1";
Add variable definition after implementation
const BufSize=2048;
var
RsltStream,TmpStream:TMemoryStream;
Add code for Server's OnCreate event:
procedure TServer.FormCreate(Sender: TObject);
begin
RsltStream:=TMemoryStream.Create;
TmpStream:=TMemoryStream.Create;
end;
Add code for the Client's OnDestroy event:
procedure TServer.FormDestroy(Sender: TObject);
begin
RsltStream.Free;
TmpStream.Free;
end;
Add code for the OnClick event of the control Button1:
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;
Add code for the OnDataReceived event of the control SUDP:
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 {data has been read}
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;
Save as "C:VServerSvrUnit.pas" and
"C:VServerVServer.dpr" and compile.
---- 4. Test.
---- 1. Local machine test: Run Vserver.exe and VClient.exe at the same time on the local machine, and use the default settings of the program to take screenshots. Check "Control Panel" - "Network" - "TCP/IP" - "IP Address", set the "Customer IP Address" of the program to this address, and it will run normally.
---- 2. Remote test: Select a controlled machine, run VClient.exe; select another master machine, run VServer.exe, and set the "controlled machine IP address", that is, the content of Edit2, as controlled The IP address of the machine and "take a screenshot". The above briefly introduces the implementation method of remote screen capture. As for monitoring multiple controlled machines at the same time on one screen on the main control machine, readers can improve it by themselves. The above program was debugged and passed under Windows98 peer-to-peer network and Delphi 4.0.