我是delphi开发的新手.我必须创建一个事件并传递一些属性作为参数.有人可以分享一些演示程序,显示如何从头开始.我几乎每个站点都搜索到,他们都提供了一段代码,但是我需要的是一个简单易懂的完整的程序.
解决方法
这是一个简短但完整的控制台应用程序,显示如何在Delphi中创建自己的事件.包括从类型声明到调用事件的所有内容.阅读代码中的注释,了解发生了什么.
program Project23; {$APPTYPE CONSOLE} uses SysUtils; type // Declare an event type. It looks allot like a normal method declaration except // it suffixed by "of object". That "of object" tells Delphi the variable of this // type needs to be assigned a method of an object,not just any global function // with the correct signature. TMyEventTakingAStringParameter = procedure(const aStrParam:string) of object; // A class that uses the actual event TMyDummyLoggingClass = class public OnLogMsg: TMyEventTakingAStringParameter; // This will hold the "closure",a pointer to // the method function itself + a pointer to the // object instance it's supposed to work on. procedure LogMsg(const msg:string); end; // A class that provides the required string method to be used as a parameter TMyClassImplementingTheStringMethod = class public procedure WriteLine(const Something:string); // Intentionally using different names for // method and params; Names don't matter,only the // signature matters. end; procedure TMyDummyLoggingClass.LogMsg(const msg: string); begin if Assigned(OnLogMsg) then // tests if the event is assigned OnLogMsg(msg); // calls the event. end; procedure TMyClassImplementingTheStringMethod.WriteLine(const Something: string); begin // Simple implementation,writing the string to console Writeln(Something); end; var Logging: TMyDummyLoggingClass; // This has the OnLogMsg variable LoggingProvider: TMyClassImplementingTheStringMethod; // This provides the method we'll assign to OnLogMsg begin try Logging := TMyDummyLoggingClass.Create; try // This does nothing,because there's no OnLogMsg assigned. Logging.LogMsg('Test 1'); LoggingProvider := TMyClassImplementingTheStringMethod.Create; try Logging.OnLogMsg := LoggingProvider.WriteLine; // Assign the event try // This will indirectly call LoggingProvider.WriteLine,because that's what's // assigned to Logging.OnLogMsg Logging.LogMsg('Test 2'); finally Logging.OnLogMsg := nil; // Since the assigned event includes a pointer to both // the method itself and to the instance of LoggingProvider,// need to make sure the event doesn't out-live the LoggingProvider end; finally LoggingProvider.Free; end; finally Logging.Free; end; except on E: Exception do Writeln(E.ClassName,': ',E.Message); end; end.