da pra ver q vc entende mesmo!!!
é isso q mata o pascal.
veja bem!!
vamos levar a discursão na boa igual ta indo até agora!!! blz?
vc ta confundindo Notification com RemoveComponent
veja esse trecho:
destructor TComponent.Destroy;
begin
Destroying;
if FFreeNotifies <> nil then
begin
while Assigned(FFreeNotifies) and (FFreeNotifies.Count > 0) do
TComponent(FFreeNotifies[FFreeNotifies.Count - 1]).Notification(Self, opRemove);
FreeAndNil(FFreeNotifies);
end;
DestroyComponents;
if FOwner <> nil then FOwner.RemoveComponent(Self);
inherited Destroy;
end;
mais necessariamente essa linha:
nessa linha ai o componente q ta sendo destruido. ta avisando pra todo mundo, q tem uma referencia monitorada (twiter, rsrs) a ele, q ele será destruido.
vc pegou um trecho q usou o Notification e fez a sua tese em cima dele. Mas esse era apenas um exemplo se vc tivesse olhado com mais cuidado teria percebido logo.
techo do help da Embarcadero
[quote]Notification is called automatically when the component specified by AComponent is about to be inserted or removed, as specified by Operation. By default, components pass along the notification to their owned components, if any.
A component can, if needed, act on the notification that a component is being inserted or removed. For example, if a component has object fields or properties that contain references to other components, it can check the notifications of component removals and invalidate those references as needed.[/quote]
então:
não sei sua experiencia com objeto, mas pelo visto a minha é melhor q a sua.
teste isso no delphi/lazaus
[code]unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls;
type
TTeste = class (TTimer)
strict private
FBotao : TButton;
procedure SetBotao(const Value: TButton);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure ExecTimer(Sender : TObject);
public
property Botao : TButton read FBotao write SetBotao;
end;
TForm1 = class(TForm)
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
FTeste : TTeste;
procedure BtnClick(Sender: TObject);
procedure BtnMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TTeste }
procedure TTeste.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if (Operation = opRemove) and (AComponent = FBotao) then
begin
FBotao := nil;
if not (csDestroying in ComponentState) then
ShowMessage('Botão foi removido e eu vi');
end;
end;
procedure TTeste.SetBotao(const Value: TButton);
begin
if FBotao <> Value then
begin
if Assigned(FBotao) then
FBotao.RemoveFreeNotification(Self);
Enabled := False;
FBotao := Value;
if Assigned(FBotao) then
begin
FBotao.FreeNotification(Self);
OnTimer := ExecTimer;
Enabled := True;
end;
end;
end;
procedure TTeste.ExecTimer;
var
I : Integer;
F : TCustomForm;
begin
if Botao = nil then
Exit;
F := GetParentForm(Botao);
if F = nil then
Exit;
if Botao.Tag and 1 = 0 then
begin
if Botao.Left + 10 + Botao.Width > F.ClientWidth then
begin
Botao.Tag := Botao.Tag or 1;
Botao.Left := Botao.Left - 10;
end else
Botao.Left := Botao.Left + 10;
end else
begin
if Botao.Left - 10 < 0 then
begin
Botao.Tag := Botao.Tag and not 1;
Botao.Left := Botao.Left + 10;
end else
Botao.Left := Botao.Left - 10;
end;
if Botao.Tag and 2 = 0 then
begin
if Botao.Top + 10 + Botao.Height > F.ClientHeight then
begin
Botao.Tag := Botao.Tag or 2;
Botao.Top := Botao.Top - 10;
end else
Botao.Top := Botao.Top + 10;
end else
begin
if Botao.Top - 10 < 0 then
begin
Botao.Tag := Botao.Tag and not 2;
Botao.Top := Botao.Top + 10;
end else
Botao.Top := Botao.Top - 10;
end;
end;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
FTeste := TTeste.Create(Self); //vai ser destruido qdo esse form for destruido e naõ qdo finalizar a app
FTeste.Interval := 300;
end;
procedure TForm1.BtnClick(Sender: TObject);
begin
Sender.Free;
end;
procedure TForm1.BtnMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbRight then
if Sender is TButton then
FTeste.Botao := TButton(Sender);
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbRight then
begin
Tag := Tag + 1;
with TButton.Create(Self) do
begin
Parent := Self;
Left := X;
Top := Y;
Caption := IntToStr(Self.Tag);
OnClick := BtnClick;
OnMouseDown := BtnMouseDown;
end;
end;
end;
procedure TForm1.FormPaint(Sender: TObject);
begin
Canvas.TextOut(100, 30, ‘Direito cria botao’);
Canvas.TextOut(100, 60, ‘Direito sobre o botao Liga ele’);
Canvas.TextOut(100, 90, ‘Esquerdo sobre o botao destroi ele’);
end;
end.
[/code]