加入控件问题(200分)

  • 主题发起人 主题发起人 xtdragon
  • 开始时间 开始时间
X

xtdragon

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一段代码:
unit WPanel;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls,Menus;

type
TWPanel = class(TPanel)
private
{ Private declarations }
FTPanel:TPanel;
FAPanel:TPanel;
protected
{ Protected declarations }
public
{ Public declarations }
Constructor Create(AOwner:TComponent);override;
Destructor Destroy;override;
procedure FTPanelClick(Sender:TObject);
procedure Change(Sender:TObject);
class procedure mnuPPClear(mnuPP:TPopupMenu);
procedure onPopup(mnuPP:TPopupMenu);
procedure PanelMouseDown(Sender:TObject;Button:TMouseButton;
Shift:TShiftState;X,Y:Integer);
class procedure DeleteAllShapes(ParentControl : TWinControl);
published
{ Published declarations }


end;
Var
mnuPP:TPopupMenu;


implementation



Constructor TWpanel.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
BevelInner:=bvLowered;
BevelWidth:=2;
Height:=235;
Width:=94;
FTPanel:=TPanel.Create(Self);
FTPanel.Parent:=TWpanel(Self);
FTPanel.Height:=25;
FTPanel.Align:=altop;
FTPanel.onClick:=FTPanelClick;
TPanel(Self).onMouseDown:=PanelMouseDown;

end;

Destructor TWPanel.Destroy;
begin
inherited Destroy;
end;


class procedure TWPanel.DeleteAllShapes(ParentControl : TWinControl);
var
i : Integer;
begin {DeleteAllShapes}
// Delete controls from ParentControl
i := 0;

while i < ParentControl.ControlCount do begin
if ParentControl.Controls is TWPanel then begin
ParentControl.Controls.Free;
// Note that there is no need to increment the counter, because the
// next component (if any) will now be at the same position in Controls[]
end else begin
Inc(i);
end;
end;
end; {DeleteAllShapes}

class procedure TWPanel.mnuPPClear(mnuPP:TPopupMenu);
var
i:integer;
begin
for i:=mnuPP.Items.Count-1 DownTo 0 do
mnuPP.Items.Free;
end;

procedure TWPanel.PanelMouseDown(Sender:TObject;Button:TMouseButton;
Shift:TShiftState;X,Y:Integer);

var
p,q:TPoint;
begin
{Selected:=True;}
{ReleaseCapture;
perform(WM_SysCommand,$F012,0);}
if (Button in [mbRight]) then
begin
mnuPPClear(mnuPP);
onPopup(mnuPP);
P.x:=x;
p.y:=y;
q:=ClientToScreen(P);
mnuPP.Popup(q.x,q.y);
exit;
end;
end;


procedure TWPanel.FTPanelClick(Sender:TObject);
var
s:string;
begin
if inputQuery('机架名输入','请输入机架名',s) then
FTPanel.Caption:=s;
end;

procedure TWPanel.onPopup(mnuPP:TPopupMenu);
var
mp:TMenuItem;
begin
mp:=TMenuItem.Create(Self);
mp.Caption:='增加机框';
mp.onClick:=Change;
mnuPP.Items.Add(mp);
end;

procedure TWPanel.Change(Sender:TObject);
begin
{if tmenuitem(sender).Caption='增加机框' then
begin }
FAPanel:=TPanel.Create(self);
FAPanel.Align:=alBottom;
FAPanel.Caption:='仙桃';
FAPanel.Height:=70;
FAPanel.Parent:=TWPanel(self);
{end;}
end;


procedure RegisterStorageClasses;
begin {RegisterStorageClasses}
RegisterClasses([TWPanel]);
end; {RegisterStorageClasses}

initialization
RegisterStorageClasses;
mnuPP:=TPopupMenu.Create(Application);
end.


unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
PopupMenu1: TPopupMenu;
add1: TMenuItem;
ScrollBox1: TScrollBox;
Panel1: TPanel;

procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses wpanel;
{$R *.DFM}

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
with twpanel.Create(self) do
begin
left:=x;
top:=y;
parent:=form1;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.text:=inttostr(form1.componentcount);


end;
end.
在FORM1中动态建立一个TWPANEL后,窗体的控件数增加一个,但是用弹出菜单建立一个
TPANEL后,TPANEL。PARENT:=TWPANEL(SELF),窗体的控件数并不增加。请问这个问题
应该如何解决。
 
创建PARENT的位置,TPANEL是个包容控件,将PARENT 创建在Form上?还是TPANEL上?
告诉电脑
 
若在TPANEL中,你应该用ControlCount来判断数量。你是湖北仙桃的吗?
 
一个 TWinControl 类型控件的 Controls 列表中包含了以该控件为 Parent 的控件,
你用弹出菜单建立的 Panel 的 Parent 是创建它的 WPanel 对象:

FAPanel.Parent:=TWPanel(self);

所以它将出现在这个 WPanel 对象的 Controls 列表中,而不是 Form 的 Controls
列表中,你可以用递归的方法检查一下。
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
686
import
I
I
回复
0
查看
709
import
I
后退
顶部