Implement remote screen to capture with Delphi

zhaozj2021-02-08  224

Implement remote screen to capture with Delphi

Shandong Juli Co., Ltd. CAD Center

Talian

---- In network management, sometimes you need to understand the usage of online microcomputers by monitoring the remote computer screen. Although there are many software on the market to implement this feature, some can even perform remote control, but lack flexibility in use, if you cannot specify the size and position of the remote computer screen area, thereby can't monitor multiple screens simultaneously on one screen. . In fact, it is possible to prepare a flexible remote screen crawler tool with Delphi.

---- First, hardware and software requirements.

---- Windows95 / 98 peer-to-peer network, computer (hereinafter referred to as the master) and the monitored computer (hereinafter referred to as controlled machine) must have a TCP / IP protocol and configure it correctly. If there is no network, you can debug on a single computer.

---- Second, implementation method.

---- Develop two applications, one for vclient.exe, mounted on a controlled machine, the other is VSERVER.EXE, mounted on the master. VServer.exe Specifies the IP address of the controlled machine to be monitored and the size and location of the area to be captured on the controlled machine screen, and issue the screen capture instruction to vclient.exe, Vclient.exe gets the instruction, in controlled The specified area is selected on the screen, generate a data stream, send it back to the master, and display the BMP image of the capture area on the main control. As can be seen from the above process, the key to the method is two: one, how is the screen capture on a controlled machine, and the second is how to transfer data in two computers through the TCP / IP protocol.

---- UDP (User DataGram Protocol) is one of the widely used communication protocols on the Internet. Unlike TCP protocols, it is a non-connected transport protocol, no confirmation mechanism, reliability is not as good as TCP, but its efficiency is higher than TCP, which is used for remote screen monitoring or more suitable. At the same time, the UDP control does not distinguish between server-side and clients, only distinguishes transmitting ends and receiving ends, which is more simple, so select UDP protocol, use the TNMUDP control provided by Delphi 4.0.

---- Third, create a demo.

---- First step, prepare vclient.exe files. New Delphi project, set the Name property of the default form to "Client". Add TNMUDP control, the Name property is set to "CUDP"; set to "1111", allow the control CUDP to monitor the 1111 port of the controlled machine, when the data is sent to the port, the control CUDP's OnDataReceiveD event; RemotePort property To "2222", when the control CUDP sends data, the data is sent to 2222 ports of the master.

---- Add variable definitions after IMPLEMentation

Const buffsize = 2048; {Buffer size of each data}

VAR

Bmpstream: TMemoryStream;

Leftsize: longint; {Send every byte after each data}

Add code to Client's oncreate event:

Procedure Tclient.formCreate (Sender: TOBJECT);

Begin

Bmpstream: = TMEMORYSTREAM.CREATE;

END;

Add code to Client's OnDestroy event:

Procedure Tclient.formDestroy (Sender: TOBJECT);

Begin

Bmpstream.free;

END;

Add code to the control CUDP's OnDataReceiveD event: Procedure Tclient.cudpdataarement (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 is "show" indicates that the master has issued a grip command}

If bmpstream.size = 0 THEN {No data can be sent, you must screen the screen generation 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); {

Intercept screen}

END;

IF LEFTSIZE> BUFSIZE THEN Sendsize: = bufsize

Else Sendsize: = Leftsize;

Bmpstream.readbuffer (buf, senendsize);

Leftsize: = leftsize-sendsize;

IF leftsize = 0 Then bmpstream.clear; {empty flow}

Cudp.remotehost: = fromip; {fromip is the main control IP address}

Cudp.sendbuffer (buf, sendsize); {2222 portions that will be sent to the master machine}

END;

END;

Sth, ScreenCap is a custom function, intercepting the screen designated area,

code show as below:

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: = CREATECOMPALEDC (SOURCEDC);

Bhandle: = CREATECOMPATIBLEBITMAP (SOURCEDC,

RectWidth, RectHeight;

SelectObject (destdc, bhandle);

Bitblt (destdc, 0,0, rectwidth, recctheight, sourcedc,

LEFTPOS, TOPPOS, SRCCOPY;

Bitmap: = Tbitmap.create;

Bitmap.handle: = bhandle;

Bitmap.savetostream (BMPStream);

Bmpstream.position: = 0;

Leftsize: = bmpstream.size;

Bitmap.free;

Deletedc (destdc);

ReleaseDC (Bhaldle, Sourcedc);

END;

Save "c: /vclient/clnunit.pas" and "c: /vclient/vclient.dpr",

And compiled.

---- Step 2, prepare the vServer.exe file. Create a new Delphi project, set the Name property of the form to "Server". Add TNMUDP control, the Name property is set to "SUDP"; the localport property is set to "2222", allows the control SUDP to monitor the 2222 port of the master, and trigger the control SUDP's OnDataReceIVed event; RemotePort property For "1111", when the control SUDP sends data, the data is sent to the 1111 port of the controlled machine. Add the control image1, the align property is set to "AlClient"; add the control button1, the CAPTION property is set to "Screen capture"; add the control Label1, the CAPTION property is set to "Left: upper: Right: Next"; join the control Edit1, the TEXT property is set to "0: 0: 100: 100"; join the control Label2, the CAPTION property is set to "Controller IP Address"; add the control EDIT2, and the Text property is set to "127.0.0.1";

Add variable definitions after IMPLEMentation

Const buffs = 2048;

VAR

RSLTSTREAM, TMPSTREAM: TMEMORYSTREAM;

Add code to Server's oncreate event:

Procedure TSERVER.FORMCREATE (Sender: TOBJECT);

Begin

RsltStream: = TMEMORYSTREAM.CREATE;

TmpStream: = TMEMORYSTREAM.CREATE;

END;

Add code to Client's OnDestroy event:

Procedure TSERVER.FORMDESTROY (Sender: TOBJECT);

Begin

RsltStream.free;

TmpStream.free;

END;

Add code to the control Button1 onclick event:

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 to the control SUDP's OnDataReceiveD event:

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

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 "C: /VServer/svrunit.pas" and

"C: /vserver/vserver.dpr" and compiled.

---- Fourth, test.

---- 1, Local Test: The screen capture can be implemented using the user's default settings at the same time. View "Control Panel" - "Network" - "TCP / IP" - "IP Address", set the "Customer IP Address" of the program to this address, which is equally normal.

---- 2, Remote Test: Choose a controlled machine, run vclient.exe; choose one master, run VServer.exe, set "controlled machine IP address", EDIT2, set to controlled The IP address of the machine, "screen capture". The above is briefly introduced the implementation method of the remote screen, as for monitoring multiple controlled machines at the same time on the main control machine, readers can improve themselves. The above procedures, in Windows98 peer, Delphi 4.0 debugging.

转载请注明原文地址:https://www.9cbs.com/read-1279.html

New Post(0)