两个单元,一个类,TSocket 和第二个单元必须更改 Form 上的对象颜色

Wel*_*nha 1 sockets delphi events class

我必须准备一个单独的单元来通过 TCP/IP 与另一个系统进行通信。

我创建了两个单元:Unit1带有TForm,和Unit2带有通信。

单元1:

uses ..., Unit2;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Circle1: TCircle;
...

var
  Form1: TForm1;
  Communication: TCommunication; 

implementation

procedure TForm1.Button1Click(Sender: TObject);
begin
  Communication.ClientSocket1.Active := True;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Communication := TCommunication.Create;
end;
Run Code Online (Sandbox Code Playgroud)

单元2:

uses SysUtils, ScktComp;

type
  TCommunication = class(TObject)
    ClientSocket1 : TClientSocket;
    procedure ClientOnRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure ClientOnConnect(Sender: TObject; Socket: TCustomWinSocket);
  public
    constructor Create;
    destructor Destroy; override;
  end;

procedure TCommunication.ClientOnConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  // Change circle to Green
end;

procedure TCommunication.ClientOnRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  s : String;
begin
  s := ClientSocket1.Socket.ReceiveText;
end;

constructor TCommunication.Create;
begin
  ClientSocket1 := TClientSocket.Create(nil);
  with ClientSocket1 do
  begin
    Address      := '127.0.0.1';
    Port         := 4545;
    OnConnect    := ClientOnConnect;
    OnRead       := ClientOnRead;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

我不知道如何更改Circle的颜色,也不知道如何在内部创建一个事件Unit1以允许OnConnectOnRead采取新的行动。

OnRead 在 TForm 上创建一些步骤非常重要。

Rem*_*eau 5

您应该TCommunication公开TForm可以为其分配事件处理程序的自己的事件。就像为的事件TCommunication分配处理程序一样TClientSocket。例如:

单元1:

uses ..., Unit2;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Circle1: TCircle;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    procedure CommunicationOnConnect(Sender: TObject);
    procedure CommunicationOnRead();
  ...
  end;

var
  Form1: TForm1;
  Communication: TCommunication; 

implementation

procedure TForm1.Button1Click(Sender: TObject);
begin
  Communication.Connect;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Communication := TCommunication.Create;
  Communication.OnConnect := CommunicationOnConnect;
  Communication.OnRead := CommunicationOnRead;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Communication.Free;
end;

procedure TForm1.CommunicationOnConnect(Sender: TObject);
begin
  // Change circle to Green
end;

procedure TForm1.CommunicationOnRead(Sender: TObject;
  const Data: String);
begin
  // do something with Data...
end;

end.
Run Code Online (Sandbox Code Playgroud)

单元2:

uses Classes, SysUtils, ScktComp;

type
  TCommunicationReadEvent = procedure(Sender: TObject; const Data: string) of object;

  TCommunication = class(TObject)
    ClientSocket1 : TClientSocket;
    procedure ClientOnRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure ClientOnConnect(Sender: TObject; Socket: TCustomWinSocket);
  public
    constructor Create;
    destructor Destroy; override;
    procedure Connect;
    procedure Disconnect;
    OnConnect: TNotifyEvent;
    OnRead: TCommunicationReadEvent;
  end;

implementation

procedure TCommunication.ClientOnConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  if Assigned(OnConnect) then OnConnect(Self);
end;

procedure TCommunication.ClientOnRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  s : String;
begin
  s := Socket.ReceiveText;
  if Assigned(OnRead) then OnRead(Self, s);
end;

constructor TCommunication.Create;
begin
  ClientSocket1 := TClientSocket.Create(nil);
  with ClientSocket1 do
  begin
    Address      := '127.0.0.1';
    Port         := 4545;
    OnConnect    := ClientOnConnect;
    OnRead       := ClientOnRead;
  end;
end;


procedure TCommunication.Connect;
begin
  ClientSocket1.Active := True;
end;

procedure TCommunication.Disconnect;
begin
  ClientSocket1.Active := False;
end;

end.
Run Code Online (Sandbox Code Playgroud)

  • “*有没有办法在 Unit2 中处理 OnRead 并在 Unit1 的 MEMO 中显示它*” - 我已经向您展示了如何做到这一点。`TCommunication.ClientOnRead()` 可以在调用 `OnRead(Self, s)` 之前根据需要操作 `s`,然后您可以将所需的 Memo 代码放在 `TForm1.CommunicationOnRead()` 中以将 `Data` 显示为需要。 (3认同)