判断一个窗体是否已经创建?(50分)

B

Btic

Unregistered / Unconfirmed
GUEST, unregistred user!

其实这个问题我上次已经问过,可是还不能解决,麻烦各位了。

我用 Assigned 函数求一个已打开的窗体,得到值-1,
可是当这个窗体执行Free方法后,得到的值还是-1。
 
不会吧
就用这个函数
在释放后加一句:
form1:=nil;
 
ASSIGNED是判断指针是否为null值
而对象free的时候并不会把对象的实例的指针赋为null
所有你需要在free之后手动的把指针赋为null
 
判断application.FindComponent('form1')=nil就可以了啊
 
unit MY_BASEFORM;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TBaseForm = class(TForm)
///.实例是否存在。免去了一些复杂的操作比如要释放后置空对象否则判断不准确
class function HasInstance(Instance: TBaseForm): Boolean;
private
{ Private declarations }

public
{ Public declarations }

constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
end;
var
BaseForm : TBaseForm;
implementation

{$R *.dfm}
var
BasaeForm_Instance: TList;

constructor TBaseForm.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
BasaeForm_Instance.Add(Self);
end;

destructor TBaseForm.Destroy;
var
i : Integer;
begin
for i := (BasaeForm_Instance.Count - 1) downto 0 do
begin
if TBaseForm(BasaeForm_Instance) = Self then
begin
BasaeForm_Instance.Delete(i);
end;
end;
inherited Destroy;
end;

class function TBaseForm.HasInstance(Instance: TBaseForm): Boolean;
var
i : Integer;
begin
Result := False;
for i := (BasaeForm_Instance.Count - 1) downto 0 do
begin
if TBaseForm(BasaeForm_Instance) = Instance then
begin
Result := True;
end;
end;
end;

initialization
BasaeForm_Instance := TList.Create;
finalization
BasaeForm_Instance.Free;
BasaeForm_Instance := nil;
end.
添加到仓库。以后所有的窗体都从这个窗体上继承。
判断时这样调用:
if TBaseForm.HasInstance(xxx) then
begin
showmessage('已经创建');
end;
 
自己定义一个开关信号量吧!
在窗体创建时把它置为开,窗体释放时把它置为关就行了。
我也碰过一样的问题,这差不多可以算得上一个Bug 了。
Delphi 的控件在没创建时 Assigned(component)=nil
创建后 Assigned(component)<>nil
释放后 Assigned(component)<>nil
所以依靠 Assigned 来作判断不安全
 
谢谢各位,理解了。
 
顶部