要不你贴出你试验的原码。我这里模拟一个简单没这个问题,在D7下通过。
form1上有一个BUTTON和一个EDIT,表示你以后增的窗体,
CommUnit单元,就是你的通讯类
form2上有一个BUTTON,form2只是用来表示远程的客户端 发送数据过来
远行后,点击FORM1上的BUTTON,显示FORM2,在点FORM2的BUTTON,就模拟远程主机发送数据到通讯类,通讯类根据数据类型(协议判断),调用相应的FORM处理函数.
//项目单元
program Project2;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
CommUnit in 'CommUnit.pas',
Unit2 in 'Unit2.pas' {Form2};
{$R *.res}
begin
Application.Initialize;
aMyComm:=TMyComm.Create;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);
Application.Run;
end.
//form1
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,CommUnit, StdCtrls;
type
pProRevData=function (Datas:string):integer;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//处理数据的函数,提供给通讯类使用
function ProRevData(Datas:string):integer;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
{ TForm1 }
function ProRevData(Datas: string): integer;
begin
Form1.Edit1.Text:=Datas;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
pfun
ProRevData;
begin
//通过调用TMYCOMM提供的函数,告诉TMYCOMM类型为1的数据,由我处理
pfun:=ProRevData;
aMyComm.RegForm(Handle,1,Pointer(@pfun));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.Show;
end;
end.
//CommUnit单元
unit CommUnit;
interface
type
pProRevData=function (Datas:string):integer;
type
TMyComm = class
private
pfun
ointer;
RdataStyle:integer;
public
//接收数据
procedure RevDatas(data:string);
//给各FORM用的函数,用来登记要显示数据FORM的句柄、要接收那种数据、及处理数据的函数
//这就算一个回调函数,他是属于TMYCOMM,但设计的意图是给其人调用的,在这里是给那些FORM用的
function RegForm(FormHandle:THandle;RevDataStyle:integer;PProcRevDataFun
ointer):integer;
end;
var
aMyComm:TMyComm;
implementation
uses SysUtils;
{ TMyComm }
function TMyComm.RegForm(FormHandle: THandle; RevDataStyle: integer;
PProcRevDataFun: Pointer): integer;
begin
//
RdataStyle:=RevDataStyle;
pfun:=PProcRevDataFun;
end;
procedure TMyComm.RevDatas(data:string);
var
style:integer;
profun
ProRevData;
begin
style:=StrToInt(copy(data,1,1));
if style=RdataStyle then
begin
profun:=pProRevData(pfun);
profun(data);
end;
end;
end.
//form2
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses CommUnit;
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
aMyComm.RevDatas('1aaab');
end;
end.