Delphi implements remote screen capture

xiaoxiao2021-03-06  141

---- 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;

Add code to Client's OnDestroy Event: Procedure Tclient.formDestroy (Sender: TOBJECT); Begin Bmpstream.Free; End;

Adding the control code for the event CUDP OnDataReceived of: 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; {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, the screen is required to generate 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: = start (Copy (Tmpstr, POS (':', tmpstr) 1, Length (Tmpstr) -pos (':', tmpstr)); Screencap (Leftpos, Toppos, Rightpos, Bottompos); {Intercept Screen} End; if Leftsize> BufSize TH en SendSize: = BufSize else SendSize: = LeftSize; BmpStream.ReadBuffer (Buf, SendSize); LeftSize: = LeftSize-SendSize; if LeftSize = 0 then BmpStream.Clear; {clear stream} CUDP.RemoteHost: = FromIP; {FromIP of Master machine IP address} cudp.sendbuffer (buf, sendsize); {2222 portions of the data to the master;

Wherein ScreenCap custom function, interception of the screen designated area 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 (deletedc); releasedc (bhandle, sourcec); end; store "c: vclientclnunit.pas" and "c: vclientvclient.dpr "And compile. ---- 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;

Add code to the Client OnDestroy event: procedure TServer.FormDestroy (Sender: TObject); begin RsltStream.Free; TmpStream.Free; end; add the code for the OnClick event of the control of 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);

SUDP OnDataReceived add event control code: 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

Save "C: Vserversvrunit.PAS" and "C: vservervserver.dpr" and compile.

---- 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-126988.html

New Post(0)