为什么会创建窗体的时候提示找不到类呢? ( 积分: 50 )

  • 主题发起人 主题发起人 HongYuan
  • 开始时间 开始时间
H

HongYuan

Unregistered / Unconfirmed
GUEST, unregistred user!
错误:Resource TMyTest Not found
不知道是不是需要注册一下类?

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

TMyTest=class(TForm)
public
procedure abc;
end;
var
Form1: TForm1;
form2:TMyTest;
procedure test;
implementation

{$R *.dfm}
procedure test;
begin
form2:=TMyTest.Create(nil);
form2.abc;
end;

{ TMyTest }

procedure TMyTest.abc;
begin
ShowMessage('a');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
test
end;

end.
 
{$R *.dfm},和它有关?

不应该呀,只是加载单元同名的DFM文件呀。[:(!]
 
initialization
RegisterClass(TMyTest);
finalization
UnRegisterClass(TMyTest);

和它有关?不应该呀,加了好像也报错呢。
 
一个单元里怎么可能有两个窗体类呢。
两个窗体也不能共用一个资源文件呀{$R *.dfm}
 
是的,我查看了Forms.pas文件,发现CREATE时需要从DFM文件中读入定义 GlobalNameSpace.BeginWrite;
try
CreateNew(AOwner);
if (ClassType <> TForm) and not (csDesigning in ComponentState) then
begin
Include(FFormState, fsCreating);
try
if not InitInheritedComponent(Self, TForm) then
raise EResNotFound.CreateFmt(SResNotFound, [ClassName]);
finally
Exclude(FFormState, fsCreating);
end;
if OldCreateOrder then DoCreate;
end;
finally
GlobalNameSpace.EndWrite;
end;
 
function InitInheritedComponent(Instance: TComponent; RootAncestor: TClass): Boolean;

function InitComponent(ClassType: TClass): Boolean;
begin
Result := False;
if (ClassType = TComponent) or (ClassType = RootAncestor) then Exit;
Result := InitComponent(ClassType.ClassParent);
Result := InternalReadComponentRes(ClassType.ClassName, FindResourceHInstance(
FindClassHInstance(ClassType)), Instance) or Result;
end;

var
LocalizeLoading: Boolean;
begin
GlobalNameSpace.BeginWrite; // hold lock across all ancestor loads (performance)
try
LocalizeLoading := (Instance.ComponentState * [csInline, csLoading]) = [];
if LocalizeLoading then BeginGlobalLoading; // push new loadlist onto stack
try
Result := InitComponent(Instance.ClassType);
if LocalizeLoading then NotifyGlobalLoading; // call Loaded
finally
if LocalizeLoading then EndGlobalLoading; // pop loadlist off stack
end;
finally
GlobalNameSpace.EndWrite;
end;
end;
 
上面有段代码貌似根据类名读取DFM中的定义。
 
窗体的后代类也应该有自己单独的DFM。
 
多人接受答案了。
 
后退
顶部