Delphi 事件触发!

事件通知!
1.先写一个事件触发类。
unit UNotifyProcess;

interface

uses Windows, Registry, SysUtils, StrUtils, winsock, Classes, Contnrs, Dialogs;

const
  Restart_Stocket = 0;
  Allow_Work = 1;
  Forbid_Work = 2;
type
  TMyNotifyEvent = procedure(Sender: TObject; TaskSign,chn: integer) of object;
 
  TNotifyProcess = class(TObject)
  private
    FOnPopupMenuNotify: TMyNotifyEvent;
  public

    constructor Create();
    procedure ProcessPopupMenuNotify(TaskSign,chn: integer);

  published
    property OnPopupMenuNotify: TMyNotifyEvent read FOnPopupMenuNotify write FOnPopupMenuNotify;
  end;

var
  NotifyProcess: TNotifyProcess;
implementation
constructor TNotifyProcess.Create();
begin

end;

procedure TNotifyProcess.ProcessPopupMenuNotify(TaskSign,chn: integer);
var
  Sender: TObject;
begin
  Sender := nil;
  FOnPopupMenuNotify(Sender, TaskSign,chn);
end;

initialization
  NotifyProcess := TNotifyProcess.Create;

finalization
  NotifyProcess.Free;

end.
2 调用事件。记得引用UNotifyProcess
procedure TFamTestInfo.N1Click(Sender: TObject);
begin
  NotifyProcess.ProcessPopupMenuNotify(Restart_Stocket,chn);
end;


procedure TFamTestInfo.N2Click(Sender: TObject);
begin
   NotifyProcess.ProcessPopupMenuNotify(Allow_Work,chn);
end;


procedure TFamTestInfo.N3Click(Sender: TObject);
begin
  NotifyProcess.ProcessPopupMenuNotify(Forbid_Work,chn);
end;
3. 执行事件  记得引用UNotifyProcess
  private
    { Private declarations }
    procedure OnPopupMenuNotify(Sender: TObject; TaskSign, chn: integer);
在窗体show的时候加入如下代码。
procedure TFrmMain.FormShow(Sender: TObject);
begin
  NotifyProcess.OnPopupMenuNotify := OnPopupMenuNotify;
end;
 
  
 procedure TFrmMain.OnPopupMenuNotify(Sender: TObject; TaskSign, chn: integer);
begin
  case TaskSign of
    Restart_Stocket: ReConnectNetWork(chn);
    Allow_Work: SocketClient[chn].SocketAskSend('AA', '00010');
    Forbid_Work: SocketClient[chn].SocketAskSend('AA', '00011');
  end;
end;
 
  
 
  
 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(Delphi)