Delphi事件处理,如何创建自己的事件 [英] Delphi event handling, how to create own event

查看:144
本文介绍了Delphi事件处理,如何创建自己的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢delphi开发。我必须创建一个事件并传递一些属性作为参数。有人可以分享一些演示程序,显示如何从头开始。我几乎每个站点都搜索到,他们都提供了一段代码,但是我需要的是一个简单易懂的完整的程序。

解决方案

这是一个简短但完整的控制台应用程序,显示如何在Delphi中创建自己的事件。包括从类型声明到调用事件的所有内容。阅读代码中的意见,了解发生了什么。

 程序Project23; 

{$ APPTYPE CONSOLE}

使用
SysUtils;

type
//声明事件类型。它看起来像一个普通的方法声明,除了
//后面加上of对象。那个对象告诉Delphi这个
//类型的变量需要分配一个对象的方法,而不仅仅是任何具有正确签名的全局函数
//。
TMyEventTakingAStringParameter =对象的过程(const aStrParam:string);

//使用实际事件的类
TMyDummyLoggingClass = class
public
OnLogMsg:TMyEventTakingAStringParameter; //这将持有闭包,一个指向
//的方法函数本身+指向
//对象实例的指针。
程序LogMsg(const msg:string);
结束

//提供用作参数的必需字符串方法的类
TMyClassImplementingTheStringMethod = class
public
procedure WriteLine(const Something:string); //有意为
//方法和参数使用不同的名称;名称不重要,只有
//签名很重要。
结束

程序TMyDummyLoggingClass.LogMsg(const msg:string);
begin
如果Assigned(OnLogMsg)然后//测试是否分配事件
OnLogMsg(msg); //调用事件。
结束

procedure TMyClassImplementingTheStringMethod.WriteLine(const Something:string);
begin
//简单的实现,将字符串写入控制台
Writeln(Something);
结束

var日志记录:TMyDummyLoggingClass; //这有OnLogMsg变量
LoggingProvider:TMyClassImplementingTheStringMethod; //这提供了我们将分配给OnLogMsg的方法

begin
try
记录:= TMyDummyLoggingClass.Create;
try

//这样做没有,因为没有分配OnLogMsg。
Logging.LogMsg('Test 1');

LoggingProvider:= TMyClassImplementingTheStringMethod.Create;
try
Logging.OnLogMsg:= LoggingProvider.WriteLine; //分配事件
try

//这将间接调用LoggingProvider.WriteLine,因为这是
//分配给Logging.OnLogMsg
Logging.LogMsg( '测试2');

finally Logging.OnLogMsg:= nil; //由于分配的事件包含指向
//方法本身的指针和LoggingProvider的实例,所以
//需要确保事件不会生存LoggingProvider
结束
finally LoggingProvider.Free;
结束
终于Logging.Free;
结束
除了
在E:Exception do
Writeln(E.ClassName,':',E.Message);
结束
结束。


I am new to delphi development. I have to create an event and pass some properties as parameters. Could someone share some demo program that shows how to do this from scratch. I googled nearly every site, they all gave a piece of code, but what I need is a full fledged program that is simple and understandable.

解决方案

Here's a short-but-complete console application that shows how to create your own event in Delphi. Includes everything from type declaration to calling the event. Read the comments in the code to understand what's going on.

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.

这篇关于Delphi事件处理,如何创建自己的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆