RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TComponent.FreeNotification Method

Ensures that AComponent is notified that the component is going to be destroyed.

Pascal
procedure FreeNotification(AComponent: TComponent);
C++
__fastcall FreeNotification(TComponent * AComponent);

Use FreeNotification to register AComponent as a component that should be notified when the component is about to be destroyed. It is only necessary to register components this way when they are in a different form or have a different owner. For example, if AComponent is in another form and uses the component to implement a property, it must call FreeNotification so that its Notification method is called when the component is destroyed. 

For components with the same owner, the Notification method is called automatically when an application explicitly frees the component. This notification is not sent out when components are freed implicitly, because the Owner is already being freed.  

C++ Examples: 

 

/*
The following code registers several components, some on the
MySystem palette page that already exist, and some on a new
palette page that is created when the components are
installed.
*/

void __fastcall PACKAGE Register()
{
  TComponentClass classes[1] = {__classid(TDemoLabel)};
  RegisterComponents("Samples", classes, 0);
  RegisterComponents("MySystem", classes, 0);
}

__fastcall TDemoLabel::TDemoLabel(TComponent *AOwner):
    TGraphicControl(AOwner)
{
  FComponentStyle >> csInheritable;
  Width=64;
  Height=13;
}

void __fastcall TDemoLabel::Notification(TComponent *AComponent, TOperation Operation)
{
  TGraphicControl::Notification(AComponent, Operation);
  if ((Operation == opRemove)  && (AComponent == FFocusControl))
    FFocusControl = 0;
}

void __fastcall TDemoLabel::SetFocusControl(TWinControl *Value)
{
  FFocusControl = Value;

  // Calling FreeNotification ensures that this component will receive an
  // opRemove when Value is either removed from its owner or when it is
  // destroyed.

  Value->FreeNotification(Value);
}

void __fastcall TDemoLabel::Paint()
{
  TRect Rect = ClientRect;
  Canvas->Font = Font;
  Canvas->Brush->Color = Color;
  Canvas->FillRect(Rect);
  DrawText(
    Canvas->Handle,
    Caption.t_str(),
    Caption.Length(),
    &Rect,
    DT_EXPANDTABS | DT_WORDBREAK | DT_LEFT);
}

void __fastcall TDemoLabel::CMDialogChar(TCMDialogKey &Message)
{
  if (FFocusControl != NULL &&
      Enabled == true &&
      IsAccel(Message.CharCode, Caption))
      if (FFocusControl->CanFocus()){
        FFocusControl->SetFocus();
        Message.Result = 1;
      }
}

void __fastcall TDemoLabel::CMTextChanged(TMessage &Message)
{
  Invalidate();
}
/*
This is the defs file for the RegisterComponents project.
*/
class PACKAGE TDemoLabel : public TGraphicControl
{
private:
    TWinControl* FFocusControl;
    void __fastcall SetFocusControl(TWinControl* Value);
    void __fastcall CMDialogChar(TWMKey& Message);
    void __fastcall CMTextChanged(TMessage& Message);

protected:
    virtual void __fastcall Notification(TComponent* AComponent, TOperation Operation);
    virtual void __fastcall Paint(void);

public:
    __fastcall virtual TDemoLabel(TComponent* AOwner);

__published:
    __property Caption ;
    __property Color ;
    __property TWinControl* FocusControl = {read=FFocusControl, write=SetFocusControl, nodefault};
    __property Font ;
    __property ParentColor ;
    __property ParentFont ;
public:
    __fastcall virtual ~TDemoLabel(void) { }

BEGIN_MESSAGE_MAP
  MESSAGE_HANDLER(CM_DIALOGCHAR, TWMKey, CMDialogChar);
  MESSAGE_HANDLER(CM_TEXTCHANGED, TMessage, CMTextChanged);
END_MESSAGE_MAP(TGraphicControl);
};

 

Delphi Examples: 

{
The following code registers several components, some on the
MySystem palette page that already exist, and some on a new
palette page that is created when the components are
installed.
} 
type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TDemoLabel = class(TGraphicControl)
  private
    FFocusControl: TWinControl;
    procedure SetFocusControl(Value: TWinControl);
    procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
    procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
  protected
    procedure Notification(AComponent: TComponent;
      Operation: TOperation); override;
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property Caption;
    property Color;
    property FocusControl: TWinControl read FFocusControl write SetFocusControl;
    property Font;
    property ParentColor;
    property ParentFont;
  end;

procedure Register;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TDemoLabel }

constructor TDemoLabel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FComponentStyle := FComponentStyle - [csInheritable];
end;

procedure TDemoLabel.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) and (AComponent = FFocusControl) then
    FFocusControl := nil;
end;

procedure TDemoLabel.SetFocusControl(Value: TWinControl);
begin
  FFocusControl := Value;

  { Calling FreeNotification ensures that this component will receive an
    opRemove when Value is either removed from its owner or when it is
    destroyed. }

  Value.FreeNotification(Self);
end;

procedure TDemoLabel.Paint;
var
  Rect: TRect;
begiN
  Rect := ClientRect;
  Canvas.Font := Font;
  Canvas.Brush.Color := Color;
  Canvas.FillRect(Rect);
  DrawText(Canvas.Handle, PChar(Caption), Length(Caption), Rect,
    DT_EXPANDTABS or DT_WORDBREAK or DT_LEFT);
end;

procedure TDemoLabel.CMDialogChar(var Message: TCMDialogChar);
begin
  if (FFocusControl <> nil) and Enabled and
    IsAccel(Message.CharCode, Caption) then
    with FFocusControl do
      if CanFocus then
      begin
        SetFocus;
        Message.Result := 1;
      end;
end;

procedure TDemoLabel.CMTextChanged(var Message: TMessage);
begin
  inherited;
  Invalidate;
end;

procedure Register;
begin
  Classes.RegisterComponents('MySystem', [TDemoLabel]);
  Classes.RegisterComponents('Samples', [TDemoLabel]);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!