如何识别哪个子窗体在delphi中触发了一个常见的自定义事件 [英] How to identify which child form triggers an common custom event in delphi

查看:82
本文介绍了如何识别哪个子窗体在delphi中触发了一个常见的自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景是:我有一个创建子表单的父表单,为了在它们之间进行通信,在父表单中创建了自定义事件类型的对象。父表单的handel作为参数传递给事件创建过程。



在子表单中,自定义事件类型属性与父表单相同在创建子表单时,从父表单创建,父表单的对象等同于子表达式,例如:propForm.CreateScheduleEvent:= CreateScheduleFormEvent;



在仅使用父表单和相应子表单的场景中,回调可以正常工作。



在创建多个父表单的场景中对于各自的子表单,只有最后创建的父表单才能识别该事件,无论自定义事件执行了哪个子表单。据说,如果每个父表单都有一个激活了侦听器类型的对象(注意自定义事件的返回调用),则在执行自定义事件时,应该触发一个侦听每个父表单的循环,这就是原因使用handle变量,但结果只运行一次。



自定义事件声明是:


$对象的b $ b

The scenario is: I have a parent form that creates a child form, in order to communicate between them, a object of the custom event type is created in the parent. The handel of the parent form is passed as a param to the event create procedure.

In the child form a custom event type property of the same type as the parent form is created, then at the time of creating the child form, from the parent, the parent's object is equated with the child's property, for example: propForm.CreateScheduleEvent:= CreateScheduleFormEvent;

In the scenario that only a parent form and a corresponding child form are used, the call back works without problems.

In the scenario of creating several parent forms with their respective child forms, the event is only recognized by the last created parent form, regardless of which child form the custom event has executed. Supposedly, if each parent form has an object of the listener type activated (attentive to the return calls of the custom event), at the moment of executing the custom event a loop should be triggered that listens for each parent form, that is the reason for using the handle variable but, the result only It runs only once.

The custom event declaration is:

  TCreateScheduleEvent = procedure(source: HWND; GroupSchedule: TGroupSchedule) of object;
  TNewScheduleFormEvent = class
  private
    FCreateScheduleForm: TCreateScheduleEvent;
    FHandle: Int64;
  public
    constructor Create(source: HWND);
    property OnCreateScheduleForm: TCreateScheduleEvent read FCreateScheduleForm write FCreateScheduleForm;
    property Source: Int64 read FHandle write FHandle;
    procedure CreateScheduleForm(GroupSchedule: TGroupSchedule);
  end;

//TNewScheduleFrmEvent
constructor TNewScheduleFormEvent.Create(source: HWND);
begin
  inherited Create;
  FHandle:= source;
end;

procedure TNewScheduleFormEvent.CreateScheduleForm(GroupSchedule: TGroupSchedule);
begin
 { Call the registerd event only if there is a listener }
 if Assigned(FCreateScheduleForm) then FCreateScheduleForm(FHandle, GroupSchedule);
end;





父实现是:





The Parent implementation is:

  TFileListener = class
    procedure CreateScheduleFormEvent(source: HWND; GroupSchedule: TGroupSchedule);
  end;
.........
  private
    { Private declarations }
    FileListener: TFileListener;
    CreateScheduleFormEvent : TNewScheduleFormEvent;
........

procedure TFileListener.CreateScheduleFormEvent(source: HWND; GroupSchedule: TGroupSchedule);
begin
  if FormHandle = source then
    begin
    ...............
    end;
end;

........
//OnCreate
  FileListener := TFileListener.Create();

  FormHandle:= Handle;

  CreateScheduleFormEvent:= TNewScheduleFormEvent.Create(FormHandle);
  CreateScheduleFormEvent.OnCreateScheduleForm:= FileListener.CreateScheduleFormEvent;

//Child form
  propForm:= TfrmPropList.Create(Self);
  propForm.PopupMode:= pmExplicit;
  propForm.Parent:= Parent;
  propForm.CreateScheduleEvent:= CreateScheduleFormEvent;
  propForm.Show;





子表单的实现是:





The Child form implementation is:

  private
    { Private declarations }
    FCreateScheduleEvent : TNewScheduleFormEvent;
........
  public
    { Public declarations }
    property CreateScheduleEvent: TNewScheduleFormEvent read FCreateScheduleEvent write FCreateScheduleEvent;
......
//Event Execution
  CreateScheduleEvent.OnCreateScheduleForm(CreateScheduleEvent.Source, GroupSchedule);





问题是:自定义事件的正确或更好配置是什么这样就可以有几个父表单及其各自的子表单,并且当按下按钮从子表单执行自定义事件时,即使父表单没有正确的父表单也会听到此事件。焦点激活。



我尝试过:



澄清问题:



子表单是从父表单创建的



在子表单中有一个表单必须能够向父表单发送消息的按钮,以便可以执行任何操作或方法。



创建事件类型类以建立通信补间父表单和子表单。为了帮助通过子表单识别父表单,为了创建事件类,父句柄作为create事件的参数传递。

创建一个监听器事件类型对象在父表单中,并与事件类关联以侦听子表单。



CreateScheduleFormEvent:= TNewScheduleFormEvent.Create(FormHandle); CreateScheduleFormEvent.OnCreateScheduleForm:= FileListener.CreateScheduleFormEvent; propForm.CreateScheduleEvent:= CreateScheduleFormEvent;



如果只有一个带有子表单的父表单,子表单中的事件将被其父表单正确听到。 br />


如果有多个相同类型的父表单,每个表单都有相应的子表单,则相应的父表单不会听到该事件,即,如果来自child1事件被执行,它应该由parent1听到,如果它是从child2执行的,这应该从parent2听到。



如果是多个父母与他们相应的孩子一起,由任何一个孩子执行的事件,child1 ... child10,只听到上一个父母创建的父母10 ..



什么是正确的在父表格和子表单之间的个性化事件的配置,当父母是相同类型且子女是相同类型时,两者都是两个基本形式的重复。在子表单中执行事件时,它应该只由其相应的父表单监听,而不是由同一父表单的另一个实例监听。



The question is: What would be the correct or better configuration of the customized event so that it is possible to have several parent forms with their respective child forms and that when a button is pressed to execute the custom event from a child form, this event will be heard by the correct parent even if the parent form does not have the focus activated.

What I have tried:

Clarification of the problem:

A child form is created from a parent form

In the child form there is a button that must be able to send a message to the parent form so that any action or method can be executed.

An event type class is created to establish communication between the parent form and the child form. As an aid to the recognition of the parent form by the child form, to the creation of the event class a parent handle is passed as a parameter of the create event.
A listener event type object is created in the parent form and is associated with the event class to listen to the child form.

CreateScheduleFormEvent:= TNewScheduleFormEvent.Create(FormHandle); CreateScheduleFormEvent.OnCreateScheduleForm:= FileListener.CreateScheduleFormEvent; propForm.CreateScheduleEvent:= CreateScheduleFormEvent;

If there is only one parent form with a child form, the event from the child form is heard correctly by its parent form.

If there are several parent forms, of the same type, each with its corresponding child, the event is not heard by the corresponding parents, that is, if from the child1 the event is executed, it should be heard by the parent1 and if it is executed from the child2 this should be heard from the parent2.

In the case of multiple parents with their corresponding children, the event executed by any of the children, child1 ... child10, only heard from the last parent created parent10 ..

What is the correct configuration of personalized events between parent forms and child forms, when the parents are of the same type and the children are of the same type, both are duplicates of two basic forms. When executing an event in the child form, it should only be listened to by its corresponding parent and not by another instance of the same parent form.

推荐答案

解决方案有一直是从多个实例共享的事件改变技术,从使用TMethod和TExecute从子表单到父母的技术的使用。



随着这种技术可以传递父窗体的句柄和要执行的方法的名称,无论应用程序中存在多少个相同窗体的实例,它总是指向正确父级中的方法。
The solution has been to change the technique from events shared by multiple instances in the same way to the use of the technique with TMethod and TExecute from the child forms to the parents.

With this technique you can pass the handle of the parent form and the name of the method to be executed and regardless of how many instances of the same form exist in the application it always points to the method in the correct parent.


这篇关于如何识别哪个子窗体在delphi中触发了一个常见的自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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