-1

问题:

如何Form1在表单中加载框架或样本容器?

FindClass或者GetClass只是主要形式 appl-n 的位置

我需要(也许)字符串全局元素TFrameClass,下一个代码:

unit Unit1;

interface

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

type
  TFrameClass = class of TFrame;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);    
  private
    FFrame: TFrame;    
    function StrShowFrame(FrameClassName: string;
      ParentPanel: TWinControl): Boolean;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses Base1Frame, Base2Frame, Base3Frame;

function TForm1.StrShowFrame(FrameClassName: string;
  ParentPanel: TWinControl): Boolean;
var
  FrameClass: TClass;
  // Current Frame  (FrameName)
  FrameName: string;    
begin
  Result := False;

  ???   GetClass is only locality for main form in appl-n

  FrameClass := GetClass(FrameClassName);

  if FrameClass = nil then
  begin    
    ShowMessageFmt('Class %s not registered', [FrameClassName]);
    Result := False;
    Exit;
  end;

  try
    begin
      LockWindowUpdate(ParentPanel.Handle);

      if Assigned(FFrame) then
        if FFrame.ClassType = FrameClass then
        begin
          Result := True;
          Exit;  
        end
        else
          FFrame.Destroy; // del previus FrameClass

      try
        FFrame := TFrameClass(FrameClass).Create(nil);
      except
        on E:Exception do
        begin
          Result := True;
          E.Create(E.Message);
          FFrame := nil;
          Exit;
        end;
      end;

      FrameName:= FrameClassName;
      Delete(FrameName, 1, 1); // T-...    
      FFrame.Name := Concat(FrameName, '1');
      FFrame.Parent := ParentPanel;
      FFrame.Align := alClient;    
    end;    
  finally
    LockWindowUpdate(0); 
  end;

  Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  StrShowFrame('TFr_Base1', Panel1);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  try
    if FFrame <> nil then
      FFrame.Free
    else
      ShowMessage('Class not activ');    
  except

  end;    
end;

end.

如何Form1在表单中加载框架或样本容器?

FindClass或者GetClass只是主要形式 appl-n 的位置

我需要(也许)字符串全局元素TFrameClass

4

1 回答 1

3

GetClass()并且FindClass()不是 MainForm 本地的,它们对整个 RTL 是全局的。任何单元都可以调用RegisterClass()并让共享相同RTL 实例的任何其他单元访问该类。最后一部分很重要。DLL 不能注册 EXE 使用的类(反之亦然),除非两个项目都在启用运行时包的情况下编译,因此它们共享一个 RTL 实例。

于 2013-09-17T03:38:55.927 回答