你的概念错了,参数只能是一个实例或指针,但你试途把一个类型做为参数。而且FFrame是在哪定义的?

解决方案 »

  1.   

    可以
    type TClass = Class of TObject;
    ?处填写TClass;
    可以给分了,呵呵
      

  2.   

    用类指针,请看帮助:
    A class-reference type, sometimes called a metaclass, is denoted by a construction of the formclass of typewhere type is any class type. The identifier type itself denotes a value whose type is class of type. If type
    1 is an ancestor of type2, then class of type2 is assignment-compatible with class of type1. Thustype TClass = class of TObject;var AnyObj: TClass;declares a variable called AnyObj that can hold a reference to any class. (The definition of a class-reference type cannot occur directly in a variable declaration or parameter list.) You can assign the value nil to a variable of any class-reference type.
    To see how class-reference types are used, look at the declaration of the constructor for TCollection (in the Classes unit):type TCollectionItemClass = class of TCollectionItem;  ...
    constructor Create(ItemClass: TCollectionItemClass);This declaration says that to create a TCollection instance object, you must pass to the constructor the name of a class descending from TCollectionItem.
    Class-reference types are useful when you want to invoke a class method or virtual constructor on a class or object whose actual type is unknown at compile time.
      

  3.   

    To:Tompage
    你的方法我在提问以前,就式过了,不行FFrame在函数外已经定义:
    var
      FFrame:TFrame;
      

  4.   

    的确,这样是存在问题,因为你的程序中使用的构造函数时Create(Component:TComponet);
    在TObject类中没法找到这个构造函数,所以会出错。你应该把定义改成:
    type TComponetClass = class of TComponent;
    ok!
      

  5.   

    还有点问题,其实应该是
    type TFrameClass = class of TFrame;
    才对,因为不能将TComponent类赋值给它的子类的变量。
      

  6.   

    我是想把一个Frame建立在以个Panel上
    原程序是:
    procedure TForm1.ButtonClick(Sender:Tobject);
    begin
      if FFrame<>nil then
        FFrame.free;
      FFrame := Tlei.Create(Panel1);
      FFrame.Parent := Panel1;
    end;
    但Frame不止一个所以我想写成一个过程,希望各位能说详细一点,你们说的我都不大明白
      

  7.   

    在Delphi中可以将一个类看作是一个实例,它的类型就使用class of来定义,
    在使用时,class of TObject 可以表示任何类,class of TFrame则只能表示TFrame类或其子类,将TFrame类的一个子类赋值给这个变量后,调用构造函数,构造出的类是子类的类型。也就达到了传递类型的作用。
      

  8.   

    sender:Tobject 是为了增加代码的可重复利用率,将调用该方法的调用者作为参数传递进来,在方法内可以通过 sender as ... 将sender转化为相应的对象,来使用他。
      

  9.   

    完全测试通过:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ExtCtrls;type
      TClass=Class of TFrame;
      TObjClass=Class of TObject;
      TForm1 = class(TForm)
        Button1: TButton;
        Panel1: TPanel;
        Button2: TButton;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
        FFrame:TFrame;
        procedure CreateObj(ObjectCls:TClass);
        procedure CreateObjEx(ObjectCls:TObjClass);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit2, Unit3, Unit4;{$R *.DFM}
    procedure TForm1.CreateObj(ObjectCls:TClass);
    begin
      if Assigned(FFrame) then
      begin
        FFrame.Free();
        FFrame:=nil;
      End;
      FFrame:=ObjectCls.Create(Self);
      FFrame.Parent:=Panel1;
    end;procedure TForm1.CreateObjEx(ObjectCls:TObjClass);
    Var
      FrameClass:TClass;
    Begin
      if Assigned(FFrame) then
      begin
        FFrame.Free();
        FFrame:=nil;
      End;
      FrameClass:=TClass(ObjectCls);
      FFrame:=FrameClass.Create(Self);
      FFrame.Parent:=Panel1;
    End;procedure TForm1.Button1Click(Sender: TObject);
    begin
      CreateObj(TFrame2);
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      FFrame:=Nil;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      CreateObj(TFrame3);
    end;procedure TForm1.Button3Click(Sender: TObject);
    begin
      CreateObjEx(TFrame4);end;End.
      

  10.   

    这段代码最好这样:
    procedure TForm1.CreateObj(ObjectCls:TClass);
    begin
      if Not Assigned(FFrame) then
      begin
        FFrame:=ObjectCls.Create(Self);
        FFrame.Parent:=Panel1;
      End;
      
    end;
      

  11.   

    to lance2000();
    你这么写虽然没有错,但我想,一般要怎么动态创建TFrame一般是为了实现在各Frame之间切换,你这么写,就实现不了该功能了!当然程序中的“FFrame:=nil;”是多余的,只是写程序习惯了,Free后紧接着设为nil,(在delphi5以后还可以用FreeAndNil函数)。
      

  12.   

    我的确是要创建许多的Frame,并且在各Frame间切换,有点象Windows优化大师