CnPack Forum


 
Subject: 关于TNotifyEvent的问题,请版主解答!谢谢!
jackyxinli
新警察
Rank: 1



UID 130
Digest Posts 0
Credits 1
Posts 1
点点分 1
Reading Access 10
Registered 2003-8-4
Status Offline
Post at 2003-8-4 16:26  Profile | Blog | P.M. 
关于TNotifyEvent的问题,请版主解答!谢谢!

代码如下:
TmxHeaderImages = Class( TPersistent )
  Private
    FHot: TCustomImageList;
    FHotChangeLink: TChangeLink;
    FNormal: TCustomImageList;
    FNormalChangeLink: TChangeLink;
    FOwnerComponent: TComponent;

    FOnChangeParameter: TNotifyEvent;

    Procedure SetImages( Index: Integer; AValue: TCustomImageList );
    Procedure ImageListChange( Sender: TObject );
  Protected
    Procedure Change; Dynamic;
  Public
    Constructor Create( AOwner: TComponent ); Virtual;
    Destructor Destroy; Override;

    Procedure Assign( Source: TPersistent ); Override;
    Procedure AssignTo( Destination: TPersistent ); Override;
  Published
    Property Hot: TCustomImageList Index 2 Read FHot Write SetImages;
    Property Normal: TCustomImageList Index 1 Read FNormal Write SetImages;
    Property OnChangeParameter: TNotifyEvent Read FOnChangeParameter Write FOnChangeParameter;
  End;

implementation

Constructor TmxHeaderImages.Create( AOwner: TComponent );
Begin
  Inherited Create;

  FOwnerComponent := AOwner;

  FNormalChangeLink := TChangeLink.Create;
  FNormalChangeLink.OnChange := ImageListChange;
  FHotChangeLink := TChangeLink.Create;
  FHotChangeLink.OnChange := ImageListChange;
End;

Destructor TmxHeaderImages.Destroy;
Begin
  FNormalChangeLink.Free;
  FHotChangeLink.Free;

  Inherited Destroy;
End;

Procedure TmxHeaderImages.ImageListChange( Sender: TObject );
Begin
  Change;
End;

Procedure TmxHeaderImages.Change;
Begin
  If Assigned( FOnChangeParameter ) Then FOnChangeParameter( Self );
End;

Procedure TmxHeaderImages.SetImages( Index: Integer; AValue: TCustomImageList );
Begin
  Case Index Of
    1: If FNormal <> Nil Then FNormal.UnRegisterChanges( FNormalChangeLink );
    2: If FHot <> Nil Then FHot.UnRegisterChanges( FHotChangeLink );
  End;

  Case Index Of
    1: FNormal := AValue;
    2: FHot := AValue;
  End;

  Case Index Of
    1:
      Begin
        If FNormal <> Nil Then
        Begin
          FNormal.RegisterChanges( FNormalChangeLink );
          FNormal.FreeNotification( FOwnerComponent );
        End;
        ImageListChange( FNormal );
      End;
    2:
      Begin
        If FHot <> Nil Then
        Begin
          FHot.RegisterChanges( FHotChangeLink );
          FHot.FreeNotification( FOwnerComponent );
        End;
        ImageListChange( FHot );
      End;
  End;
End;

Procedure TmxHeaderImages.Assign( Source: TPersistent );
Begin
  If Source Is TmxHeaderImages Then
  Begin
    FOwnerComponent := TmxHeaderImages( Source ).FOwnerComponent;

    Normal := TmxHeaderImages( Source ).Normal;
    Hot := TmxHeaderImages( Source ).Hot;

    Change;
  End
  Else Inherited Assign( Source );
End;

Procedure TmxHeaderImages.AssignTo( Destination: TPersistent );
Begin
  If Destination Is TmxHeaderImages Then
  Begin
    TmxHeaderImages( Destination ).Assign( Self );
  End
  Else Inherited AssignTo( Destination );
End;

程序中定义了一个FOnChangeParameter的类过程变量,还有一个ImageListChange过程,在初始化的时候将它指定给某些对象的OnChange事件,而ImageListChange过程又调用了本类的Change过程,我不明白其中的意思,请版主解答!谢谢!
Top
zjy
管理员
Rank: 9Rank: 9Rank: 9



UID 2
Digest Posts 6
Credits 2385
Posts 1543
点点分 2385
Reading Access 102
Registered 2002-12-16
Location China
Status Offline
Post at 2003-8-4 18:43  Profile | Site | Blog | P.M. 
Delphi在ImageList中设计了更新通知功能,通过RegisterChanges这个方法,可以向ImageList注册一个TChangeLink通知器对象,当ImageList中的图像变更时,通知器对象可以获得通知,这样用户程序就可以执行图像显示刷新等操作。

TmxHeaderImages在构造器中创建了两个TChangeLink对象,并将它们的OnChange事件指定为ImageListChange方法。在Hot和Normal的写方法中,会将相应的TChangeLink对象注册到ImageList中,当ImageList内容更新时,就会调用TmxHeaderImages的ImageListChange方法,该方法把这个通知通过Change发送给OnChangeParameter这个用户事件,该事件表明TmxHeaderImages的内部状态已经发生变化,通常用户可在该事件处理过程中刷新界面显示及执行相应操作。

这样解释,您理解了吗?




Zhou JingYu
CnPack Administrator
http://www.cnpack.org/
Top
 




All times are GMT++8, the time now is 2024-11-22 11:27

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.008768 second(s), 9 queries , Gzip enabled

Clear Cookies - Contact Us - CnPack Website - Archiver - WAP