动态创建窗口?(100分)

  • 主题发起人 主题发起人 wlyft
  • 开始时间 开始时间
W

wlyft

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在已有的窗体情况下动态创建多个与已有窗体一模一样的窗体(包括上面的所有控件)
动态新建的窗口和控件响应事件都共用已有的窗口的,,,如何销毁这些创建出来的窗口,
 
var aNewForm: TYourForm;
begin
aNewForm := TYourForm.Create(...)
// Application.CreateForm...
try
aNewForm.ShowModal
// Show;
finally
FreeAndNil(aNewForm);
end;
end;
 
创建一个 TList , 创建窗口时将 窗口的指针 Add 到 list 中,释放时用 indexof 取
窗口在 list 中的 index, 然后 remove
 
procedure TForm1.Button1Click(Sender: TObject);
var NewForm:TForm1;
begin
NewForm:=TForm1.Create(self);
try
NewForm.Showmodal;//注意位置与大小都与主窗体form1一样
finally
FreeAndNil(NewForm);
end;
end;
 
procedure TMainForm.WindowMinimizeItemClick(Sender: TObject);
var
I: Integer;
begin
{ Must be done backwards through the MDIChildren array }
for I := MDIChildCount - 1 downto 0 do
MDIChildren.close;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var NewForm:TForm1;
begin
NewForm:=TForm1.Create(self);
try
NewForm.Showmodal;//注意位置与大小都与主窗体form1一样
finally
FreeAndNil(NewForm);
end;
end;
这种方式新建的最后一个窗口才能按到,其它之前创建的都点击无效???
怎么办?
 
因为你用的是showmodal,当然不能点以前 的了
这个方法我看还是要改进
以前作过的,忘了怎么弄的了,气死我,我翻箱倒柜也没找到那段代码
 
wdl
谢谢你!
我需要你再帮我找找好吗或问问你的网友,我很需要!
 
这种方式新建的最后一个窗口才能按到,其它之前创建的都点击无效???
怎么办?

你这样就不能用showmodal了,可以试试mdi窗体,创建同样的子窗体是很容易的,父窗体
可以隐掉它

父窗体可以遍历所有子窗体进行销毁
 
form1上放一个button

unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
POP:=Tpop.Create(application);
POP.BITBTN1.caption:='弹出式消息';
POP.BITBTN1.onClick:=pop.popbtnclick;
hnd:=POP.Handle;
POP.BITBTN1.tag:=hnd;
POP.SHOW;
end;
end.
------------------
窗体pop上放一个bitbtn
unit Unit2;

interface

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

type
Tpop = class(TForm)
BitBtn1: TBitBtn;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject
var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure popbtnclick(sender:Tobject);
end;

var
pop: Tpop;
hnd:hwnd;
implementation
{$R *.DFM}

procedure Tpop.popbtnclick(sender:Tobject);
begin
PostMessage((sender as Tbutton).tag,WM_CLOSE,0,0);
end;

procedure Tpop.FormShow(Sender: TObject);
begin
POP.Color:=rgb(88,157,208);
end;

procedure Tpop.FormClose(Sender: TObject
var Action: TCloseAction);
begin
action:=cafree;
end;

procedure Tpop.FormDestroy(Sender: TObject);
begin
sender:=nil;
end;
end.

好不容易翻箱倒柜找出来了,很多地方值得商榷,不过表面上实现了,希望各位指正
我是节选出来地,有些传递地东西或许没用上
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
//申明你要用的Form,这里为5个;
NewForm:array [0..4] of TForm2;
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
//一次性生成并显示5各相同Form
for i:=0 to 4 do
begin
NewForm:=Tform2.Create(Self);
NewForm.Caption:='Form No.'+IntToStr(i);
NewForm.Show;
end;
end;

end.
 
多人接受答案了。
 
后退
顶部