2

我有两种情况。一种有效,一种无效。第一个(有效的)包含一个直接位于表单上的滚动框,当按下按钮时,它会执行以下代码:

procedure TForm1.Button2Click(Sender: TObject); 
begin
 DrawPanel;
end;

procedure TForm1.DrawPanel;
begin
BuildPanel; //Resides on a seperate unit code pasted below

TestPanel.Height := 40;
TestPanel.Width := 100;
TestPanel.Left := Trunc(ScrollBox1.Width / 2) - Trunc(TestPanel.Width / 2);
TestPanel.Top := Trunc(ScrollBox1.Height / 2) - Trunc(TestPanel.Height / 2);
TestPanel.Visible := True;
TestPanel.Parent := ScrollBox1;

end;

unit Unit3;

interface

uses ExtCtrls;

Var
 TestPanel : Tpanel;

Procedure BuildPanel; 

implementation

procedure BuildPanel;
begin
 TestPanel := TPanel.Create(Nil);
end;

end.

代码是相同的,除了在第二种情况下的细微差别。滚动框位于添加到模板调色板的框架上,然后下拉到表单上。按钮点击调用:

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

procedure TTestFrame.DrawPanel;
begin
 BuildPanel; //Still points to the unit3 code above

   TestPanel.Height := 40;
   TestPanel.Width := 100;
   TestPanel.Left := Trunc(ScrollBox1.Width / 2) - Trunc(TestPanel.Width / 2);
   TestPanel.Top := Trunc(ScrollBox1.Height / 2) - Trunc(TestPanel.Height / 2);
   TestPanel.Visible := True;
   TestPanel.Parent := ScrollBox1;
end; 

但是,在运行时触发时,面板不会显示在框架上的滚动框中。我不确定为什么,有人可以帮忙吗?我希望我的问题足够具体,如果有任何不清楚的地方,请告诉我。提前致谢。

这是按顺序排列的所有代码.....希望它更清楚:

//This is the form 
unit Unit1;

interface

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

type
 TForm1 = class(TForm)
 Button1: TButton;
 TTestFrame1: TTestFrame;
 ScrollBox1: TScrollBox;
 Button2: TButton;
 procedure Button1Click(Sender: TObject);
 procedure FormShow(Sender: TObject);
 procedure FormClose(Sender: TObject; var Action: TCloseAction);
 procedure Button2Click(Sender: TObject);
private
 { Private declarations }
 TestFrame: TTestFrame;
 Procedure DrawPanel;
public
 { Public declarations }
end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

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

procedure TForm1.Button2Click(Sender: TObject);
begin
 DrawPanel;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 TestFrame.Free;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
 TestFrame := TTestFrame.Create(Form1);
end;

procedure TForm1.DrawPanel;
begin
 BuildPanel;

 TestPanel.Height := 40;
 TestPanel.Width := 100;
 TestPanel.Left := Trunc(ScrollBox1.Width / 2) - Trunc(TestPanel.Width / 2);
 TestPanel.Top := Trunc(ScrollBox1.Height / 2) - Trunc(TestPanel.Height / 2);
 TestPanel.Visible := True;
 TestPanel.Parent := ScrollBox1;

end;

end.

//This is the frame
unit Unit2;

interface

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

type
 TTestFrame = class(TFrame)
 ScrollBox1: TScrollBox;
private
 { Private declarations }
public
 { Public declarations }
  Procedure DrawPanel;
end;

implementation

{$R *.dfm}

{ TTestFrame }

procedure TTestFrame.DrawPanel;
begin
 BuildPanel;

 TestPanel.Height := 40;
 TestPanel.Width := 100;
 TestPanel.Left := Trunc(ScrollBox1.Width / 2) - Trunc(TestPanel.Width / 2);
 TestPanel.Top := Trunc(ScrollBox1.Height / 2) - Trunc(TestPanel.Height / 2);
 TestPanel.Visible := True;
 TestPanel.Parent := ScrollBox1;

end;
end.

//This is the unit that mocks my data structure
//In reality it creates an Array of Tpanel that is part of a class.                                                                                                         
unit Unit3;

interface

uses ExtCtrls;

Var
 TestPanel : Tpanel;

Procedure BuildPanel;

implementation

procedure BuildPanel;
begin
 TestPanel := TPanel.Create(Nil);
end;

end.
4

1 回答 1

4

您只是忘记将 a 分配parent给您动态创建的 TestFrame。

于 2013-06-04T15:51:45.403 回答