两个单元,一个 Class,TSocket 和第二个单元必须更改 Form 上的对象颜色
Two Units, one Class, TSocket and second Unit has to change object color on Form
我必须准备一个单独的单元以通过 TCP/IP 与另一个系统通信。
我创建了两个单元:Unit1
带有 TForm
,Unit2
带有通信。
Unit1:
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;
第 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;
我不知道如何更改 Circle
的颜色,或者如何在 Unit1
中创建一个事件让 OnConnect
和 OnRead
接受新的动作。
OnRead
在TForm上创建一些步骤非常重要。
您应该 TCommunication
公开它自己的事件,TForm
可以将事件处理程序分配给这些事件。正如 TCommunication
将处理程序分配给 TClientSocket
的事件一样。例如:
Unit1:
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.
第 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.
我必须准备一个单独的单元以通过 TCP/IP 与另一个系统通信。
我创建了两个单元:Unit1
带有 TForm
,Unit2
带有通信。
Unit1:
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;
第 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;
我不知道如何更改 Circle
的颜色,或者如何在 Unit1
中创建一个事件让 OnConnect
和 OnRead
接受新的动作。
OnRead
在TForm上创建一些步骤非常重要。
您应该 TCommunication
公开它自己的事件,TForm
可以将事件处理程序分配给这些事件。正如 TCommunication
将处理程序分配给 TClientSocket
的事件一样。例如:
Unit1:
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.
第 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.