オーナーとしてnilを指定すると、親アイテムは自分のアイテムを解放します。
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
mi: TMenuItem;
protected
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
end;
..
procedure TForm1.Button1Click(Sender: TObject);
begin
mi := TMenuItem.Create(nil);
mi.FreeNotification(Self);
PopupMenu1.Items.Add(mi);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
PopupMenu1.Free;
end;
procedure TForm1.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if (AComponent = mi) and (Operation = opRemove) then
ShowMessage('mi freed');
end;
:それはあなたが通知メカニズムを使用することができますアクションでそれを見るために、十分ではない場合
destructor TMenuItem.Destroy;
begin
..
while Count > 0 do Items[0].Free;
..
end;
:検証するよう
は、最も簡単にはTMenuItem.Destroy
内のコードを確認することです最初にポップアップメニューに項目を追加するには、Button1を押します。 Button2を押してポップアップを解放します。アイテムは、破棄されたときにフォームに通知します。
しかし、Form1そのもののような所有者をI * do *使用すると問題はありますか? Form1もTMenuItemを解放しないでしょうか? (最初の質問でこれを言及してくれないのは申し訳ありません) – Vlad
@Vlad - いいえ、問題はありません。アイテム(フォームまたは親アイテム)を解放した後、そのアイテムはその親アイテムから削除されます。 –
非常に良い答えをありがとう! – Vlad