我该如何处理Delphi中的事件? [英] How can i handle events in Delphi?

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

问题描述

例如:在单击Button1后,我有执行某项操作的过程。没有Button1Click中的代码,如何处理按钮上的onclick事件?
我需要为Button1动态添加事件吗?

For example: I have procedure, that doing something, after click on Button1. How can i handle onclick event from button, without code in Button1Click? I need dynamically add event for Button1?

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure MyTest;
    procedure OutData(Sender: TObject);
    procedure FormClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}



procedure TForm1.FormClick(Sender: TObject);
begin
  MyTest; // Start up Main
end;

procedure TForm1.MyTest; // Main

begin
  Button1.OnClick := OutData;
end;

procedure TForm1.OutData(Sender: TObject);
begin
  ShowMessage('Button clicked!');
end;

end.

好,它适用于一个事件,但是如果我需要处理,两个,三个事件或事件带有OnMouseDown之类的参数?

Ok, it work for one event, but if i need handle, two, three event, or event with parameters like OnMouseDown?

推荐答案

事件是对对象方法的引用。每个事件都使用某些参数显式键入。最常见的类型 TNotifyEvent 具有参数(发件人:TObject),例如您在<$ c $上看到的c> OnClick 事件。

Events are references to an object's method. Each event is explicitly typed with certain parameters. The most common type TNotifyEvent has the parameters (Sender: TObject) such as what you see on the OnClick event.

其他事件具有其他参数集。例如, OnMouseDown TMouseEvent ,其参数为(发件人:TObject;按钮:TMouseButton ;
Shift:TShiftState; X,Y:Integer)
。您必须确保过程的参数与事件类型的参数匹配。

Other events however have other sets of parameters. OnMouseDown for example is a TMouseEvent which has the parameters (Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer). You have to be sure the parameters of your procedure match that of the event type.

本质上,这是所有在后台进行设置的方式。

Here's essentially how everything is set up behind the scenes...

type
  TNotifyEvent = procedure(Sender: TObject) of object;

  TMouseEvent = procedure(Sender: TObject; Button: TMouseButton;
    Shift: TShiftState; X, Y: Integer) of object;

  TControl = class(TComponent)
  ...
    property OnClick: TNotifyEvent read FOnClick write FOnCLick;
    property OnMouseDown: TMouseEvent read FOnMouseDown write FOnMouseDown;
  ...
  end;






在这种情况下,您也可以分配相同的值事件处理程序来处理具有相同事件类型的多个不同事件 。例如,带有 OnClick 事件的5个不同的按钮指向同一处理程序。


In this case, you can also assign the same event handler to multiple different events of the same event type. For example, 5 different buttons with their OnClick event pointed to the same handler.

procedure TForm1.MyButtonClick(Sender: TObject);
begin
  //Do Something...
end;

procedure TForm1.MyButtonMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  //Do Something...
end;

Button1.OnClick := MyButtonClick;
Button2.OnClick := MyButtonClick;
Button3.OnClick := MyButtonClick;
Button4.OnClick := MyButtonClick;
Button5.OnClick := MyButtonClick;
Button1.OnMouseDown := MyButtonMouseDown;
Button2.OnMouseDown := MyButtonMouseDown;
Button3.OnMouseDown := MyButtonMouseDown;
Button4.OnMouseDown := MyButtonMouseDown;
Button5.OnMouseDown := MyButtonMouseDown;






如果您希望这两个事件都可以同样, 您不能将同一事件处理程序分配给不同类型的事件 ,因为它们具有不同的参数。在这种情况下,您将需要使两个事件处理程序都重定向到同一事物。使用上面的示例,您将在两个地方都看到 // Do Something ... 。但是,不要简单地复制代码。只需创建第三个过程,并使两个事件处理程序都调用该过程。


If you want both of these events to do the same thing, you cannot assign the same event handler to events of different types, because they have different parameters. In that case, you will need to make both event handlers redirect to the same thing. Using the example, above, where you see //Do Something... in both places you would do the same exact thing. However, don't simply copy the code. Just make yet a third procedure, and make both event handlers call that one procedure.

procedure TForm1.MyButtonClick(Sender: TObject);
begin
  DoSomething;
end;

procedure TForm1.MyButtonMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  DoSomething;
end;

procedure TForm1.DoSomething;
begin
  //Do Something...
end;

另一方面,上面的示例在现实世界中毫无意义,因为那样会导致每次单击按钮都会两次调用同一过程。仅仅是为了演示如何使用问题中的示例来完成您要完成的工作。

On the other hand, the above example would make no sense in the real world though, because that would result in the same procedure being called twice for every button click. It's merely to demonstrate how to accomplish what you're trying to do using your example in your question.

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

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