Implement remote screen to capture with Delphi

zhaozj2021-02-11  230

Implement remote screen to capture with Delphi

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. I. Software and software requirements. Windows95 / 98 peer-to-peer, computer (hereinafter referred to as the master) and the monitored computer (hereinafter referred to as a controller) must have a TCP / IP protocol and configure it correctly. If there is no network, you can debug on a single computer. Second, implement the method. To prepare two applications, one is vclient.exe, mounted on a controlled machine, and 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 a user packet 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 program. The first step is to prepare a vclient.exe file. 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 definition const bufsize = 2048 after implementation; {Buffer size of each data} varbmpstream: tmemorystream; Leftsize: longint; {Send Each Data after the remaining byte number} Add code to Client's oncreate event Add code : procedure TClient.FormCreate (Sender: TObject); beginBmpStream: = TMemoryStream.Create; end; OnDestroy add code to the event Client: procedure TClient.FormDestroy (Sender: TObject); beginBmpStream.Free; end; for the control of OnDataReceived event CUDP Add code: procedure tclient.cudpdataareced (sender: tComponent; Numberbytes: integer; fromip: string); vartrlcode: array [0..29] of char; buf: array [0..bufsize-1] of char; tmpstr: string Sendsize, Leftpos, Toppos, Rightpos, Bottompos: Integer; Begincudp.Readbuffler (Ctrlcode, Numberbytes); {Read Control Code} if Ctrlcode [0] Ctrlcode [1] Ctrlcode [2] Ctrlcode [3] = ' SHOW 'Thenbegin {The first 4 digits of the control code indicates that the master issues a grip directive} if bmpstream.size = 0 THEN {No data can be sent, must be displayed in the screen to generate data} begintmpstr: = 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); {screen capture} end; if LeftSize> BufSize then sendSize: = BufSizeelse sendSize: = LeftSize; BmpStream.ReadBuffer (Buf, sendSize); LeftSize: = LeftSize-sendSize; If Leftsize = 0 Then bmpstream.clear; {Clear flow} cudp.remotehost: = fromip; {fromip is the main control IP address} Cudp.sendbuffer (buf, sendsize); {2222 portions of the main controller END;

end; wherein ScreenCap custom function, interception of the screen designated area code is as follows: procedure TClient.ScreenCap (LeftPos, TopPos, RightPos, BottomPos: integer); varRectWidth, RectHeight: integer; SourceDC, DestDC, Bhandle: integer; Bitmap: TBitmap ; beginRectWidth: = 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; exists "c: /vclient/clnunit.pas" and "C: /VCLIENT/VCLIENT.DPR "and compiled. Step 2, prepare a 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 bufsize = 2048 ; varRsltStream, TmpStream: TMemoryStream; add the code for the Server OnCreate event: procedure TServer.FormCreate (Sender: TObject); beginRsltStream: = TMemoryStream.Create; TmpStream: = TMemoryStream.Create; end; OnDestroy add code to the event Client: procedure TServer.FormDestroy (Sender: TObject); beginRsltStream.Free; TmpStream.Free; end; code for the add event control OnClick of Button1: procedure TServer.Button1Click (Sender: TObject); var ReqCode: array [0..29] of char ; ReqCodeStr: string; beginReqCodeStr: = 'show' Edit1.Text; StrpCopy (ReqCode, ReqCodeStr); TmpStream.Clear; RsltStream.Clear; SUDP.RemoteHost: = Edit2.Text; SUDP.SendBuffer (ReqCode, 30); end ; add event control SUDP OnDataReceived code: procedure TServer.SUDPDataReceived (Sender: TComponent; NumberBytes: Integer; FromIP: String); var ReqCode: array [0..29] of char; ReqCodeStr: string; beginReqCodeStr: = 'show ' EDI t1.text; StrpCopy (ReqCode, ReqCodeStr); SUDP.ReadStream (TmpStream); RsltStream.CopyFrom (TmpStream, NumberBytes); if NumberBytes

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

New Post(0)