好吧,下面是一个例子:
1、完成基类
TMyForm = class(TForm)
private
FID: Longint;
public
constructor Create(AOwner: TComponent
ID: Longint)
reintroduce;
...
public
property ID: Longint read FID;
end;
constructor TMyForm.Create(AOwner: TComponent
ID: Longint);
begin
inheirted Create(AOwner);
FID := ID;
end;
-----------------------------
2、创建窗体Form1,Delphi 默认的窗体父类是 TForm,改为 TMyForm,直接手工改就是了。
TForm1 = class(TMyForm)
...
end;
3、使用窗体
var MyForm: TMyForm;
MyForm := TMyForm.Create(Application, 1);
4、查找窗体:写一个函数:
bool __fastcall IsInheritObject(TObject* AObject, String AClassName)
{
if (AObject == Null) return False;
TClass AClass;
String ClassName;
bool result = false;
AClass = AObject->ClassType()
while (AClass->ClassParent() != NULL)
{
AClass = AClass->ClassParent()
ClassName = AClass->ClassName();
if (stricmp(ClassName.c_str(), AClassName.c_str()) == 0)
{
result = TRUE;
break;
}
}
return result;
}
然后遍历窗口,先判断该窗体是否 TMyForm 类型,如果是,继续判断ID是否是
你想要的 ID 就行了。
var
F: TMyForm;
if IsInheritObject(AnyForm, 'TMyForm') then
begin
F := AnyForm as TMyForm;
if F.ID = your_id then
begin
...
end;
end;
--------------------------------------------------------------
给你个我程序中用到的现成例子吧:
class TCommandForm : public TForm
{
typedef Forms::TForm inherited;
protected:
DYNAMIC void __fastcall DoClose(TCloseAction &Action);
public:
__fastcall TCommandForm(TComponent*);
virtual void __fastcall DoUpdateCommand(int, int&
{};
virtual void __fastcall DoExecuteCommand(int, int){};
};
class TDocumentForm : public TCommandForm
{
typedef TCommandForm inherited;
private:
int FAttachHandle;
int FDocumentID;
bool FReadOnly;
bool FModified;
public:
__fastcall TDocumentForm(int, int);
public:
__property int AttachHandle = {read = FAttachHandle};
__property int DocumentID = {read = FDocumentID};
__property bool ReadOnly = {read = FReadOnly, write = FReadOnly};
__property bool Modified = {read = FModified, write = FModified};
};
TDocumentForm 就是你要看的东西。
__fastcall TCommandForm::TCommandForm(TComponent* AOwner) : TForm(AOwner)
{
}
void __fastcall TCommandForm:
oClose(TCloseAction &Action)
{
Action = caFree;
inherited:
oClose(Action);
};
//---------------------------------------------------------------------------
__fastcall TDocumentForm::TDocumentForm(int dwAttachHandle, int dwDocumentID)
: TCommandForm(Application)
{
FAttachHandle = dwAttachHandle;
FDocumentID = dwDocumentID;
FReadOnly = false;
FModified = false
};
//---------------------------------------------------------------------------