gpt4 book ai didi

delphi - 破坏形状关闭上的形状

转载 作者:行者123 更新时间:2023-12-03 15:45:17 24 4
gpt4 key购买 nike

目前,当我单击按钮时,它将在新表单上创建一些形状。一旦我关闭新表格,我怎样才能破坏它所形成的形状。

如果需要,我可以添加更多信息,但希望有一种简单的方法可以在表单关闭时销毁所有 TMachine 实例。

TMachine 是一个 TShape 类

procedure TFLayout1.GetClick(Sender: TObject);
var
azone: string;
adept: string;
machine : TMachine;
begin
fdb.count := 0; //keeps track of number of machines in zone
azone := MyDataModule.fDB.GetZone(Name); //gets name of zone
adept := TButton(Sender).Name; //gets name of dept
fdeptlayout.ListBox1.Clear;

fdeptlayout.show;
with fdeptlayout.ADOQuery1 do
begin
sql.Clear;
sql.BeginUpdate;
sql.Add('SELECT');
sql.Add(' *');
sql.Add('FROM');
sql.Add(' `MList`');
sql.Add('WHERE `Zone` = :myzone ');
sql.Add(' AND `Dept` = :mydept');
sql.EndUpdate;

parameters.ParamByName('myzone').Value := azone;
parameters.ParamByName('mydept').Value := adept;
open;
end;

//gets number of machines in total
while not fdeptlayout.ADOQuery1.Eof do
begin
fdb.count := fdb.count+1;
fdeptlayout.ADOQuery1.Next;
end;

//restarts back at first query
fdeptlayout.ADOQuery1.First;

//clears the last x value
fdb.LastX :=0;

//creates the shape
while not fdeptlayout.ADOQuery1.Eof do
begin
machine := MachineShape.TMachine.Create(self);
machine.Parent := fdeptlayout;
machine.PlaceShape(44,44,'CM402','first','123/33/123');
fdeptlayout.ListBox1.Items.Add(fdeptlayout.ADOQuery1.FieldByName('Name').AsString);
fdeptlayout.ADOQuery1.Next;
end;
end;

TM机器类

unit MachineShape;


interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, extctrls,myDataModule,Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type

TMachine = class(TShape)
private
{ Private declarations }
public
{ Public declarations }
procedure PlaceShape(sizeW,sizeH :integer; name, order,asset : string);
end;
implementation



Procedure TMachine.PlaceShape(sizeW,sizeH :integer; name, order,asset : string);
begin
self.width := sizeW;
self.height := sizeH;
self.top := 136;
self.left := MyDataModule.fDB.LastX +2;//set left
MyDataModule.fDB.lastx := left + sizeW;
showmessage(inttostr(mydatamodule.fDB.LastX));
end;

end.

FDept布局

unit DeptLayout;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls,mydatamodule, Vcl.Forms, Vcl.Dialogs, Data.DB, Data.Win.ADODB, Vcl.StdCtrls,
Vcl.ExtCtrls;

type
TfDeptLayout = class(TForm)
ADOQuery1: TADOQuery;
ListBox1: TListBox;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
fDeptLayout: TfDeptLayout;

implementation

{$R *.dfm}

procedure TfDeptLayout.FormClose(Sender: TObject; var Action: TCloseAction);
begin

end;

end.

最佳答案

显示的代码利用 VCL ownership model并且表单将为您释放它,因为您只需在创建表单时将表单本身作为组件的所有者传递即可:

machine := MachineShape.TMachine.Create(self);

由于这是从 TFLayout1 类调用的,因此当表单的特定实例自行销毁时,它将释放所有拥有的组件。

有关更多信息,您可以阅读文章:Owner vs. Parent in Delphi .

编辑

根据注释,它导致您在与显示它的表单不同的类上创建 TMachine 实例,并且在关闭表单实例时不会销毁该实例,因此,您进行此更改可以达到您想要的效果:

  • 创建形状向所有者显示的表单,更改代码以创建它们:

    //don't use self, now the parent is the instance referenced by fdeptlayout
    machine := MachineShape.TMachine.Create(fdeptlayout);
  • 在 Tfdeptlayout 类上,使用以下代码添加 OnClose 处理程序:

    begin
    for I := ComponentCount - 1 downto 0 do
    if Components[I] is TMachine then
    Components[I].Free;
    end;

也就是说,您确实必须阅读文档和引用文章才能了解 Delphi 应用程序幕后发生的情况。

关于delphi - 破坏形状关闭上的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14556051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com