很简单的一个测试程序:
一个工程里有两个form,都是自动创建,
在主form上放一button,click事件中是这样写的 (主form中引用idglobal单元) form2.show;
debugoutput(format('%u',[form2.handle]));
form2.Parent := self;
debugoutput(format('%u',[form2.handle]));
form2.Parent := nil;
debugoutput(format('%u',[form2.handle]));
form2.Parent := self;
debugoutput(format('%u',[form2.handle]));在event窗口可以看到执行结果,debug输出的4个值均不相同。
为什么form2的句柄会改变??怎么才能不让它变?
我印象中form创建后,handle不应该变的呀,请高手解释
测试环境: delphi6 + upd1 + upd2 windows xp sp1 / windows 2000 pro sp4
一个工程里有两个form,都是自动创建,
在主form上放一button,click事件中是这样写的 (主form中引用idglobal单元) form2.show;
debugoutput(format('%u',[form2.handle]));
form2.Parent := self;
debugoutput(format('%u',[form2.handle]));
form2.Parent := nil;
debugoutput(format('%u',[form2.handle]));
form2.Parent := self;
debugoutput(format('%u',[form2.handle]));在event窗口可以看到执行结果,debug输出的4个值均不相同。
为什么form2的句柄会改变??怎么才能不让它变?
我印象中form创建后,handle不应该变的呀,请高手解释
测试环境: delphi6 + upd1 + upd2 windows xp sp1 / windows 2000 pro sp4
解决方案 »
- 求助高人,关于如何解决FormResize下控件移动时发生的颤抖
- 进制转换问题(有劳各个老师!!)
- intraweb开发时提到的TIWServerController,TIWUserSession谁解释一下?
- 帮忙,一个DELPHI操纵WORD的问题
- 请问:DELPHI中BDE问题?
- 再问,关于线程的问题,应该有点难度的
- 我的程序用的Access,,运行时出现“无法为更新引集定位:一些值可能已在最后读取后改变”什么原因?
- 为什么单步执行能成功,直接运行就提示无法更新,当前被锁定
- 急!如何实现存折打印?
- 在看《windows 核心编程》一本书,里面的讲解是用vc++,可我不会,难道我非用学vc吗?你们是怎么看这本书的?
- 紧急求助!!!一个Delphi新手到济南找工作这样的条件可以拿到多少钱??必重谢!!!!
- 怎样才能是Panel组件看起来象凹下去的?
from:tfrom;
begin
from:=tfrom.create(self);
from.parent:=self;
from.caption:='自动创建';
from.width:=300;
from.hight:=200;
from.show;
end;
begin
if (Parent <> AParent) and (AParent <> Self) then
begin
if Parent = nil then DestroyHandle;//DestroyHandle;
inherited SetParent(AParent);
if Parent = nil then UpdateControlState;
end;
end;更改了其parent后,会重新创建窗体
procedure TWinControl.DestroyHandle;
var
I: Integer;
begin
if FHandle <> 0 then
begin
if FWinControls <> nil then
for I := 0 to FWinControls.Count - 1 do
TWinControl(FWinControls[I]).DestroyHandle;
DestroyWnd;//这里
end;
end;很明显
begin
if (Parent <> AParent) and (AParent <> Self) then
begin
if Parent = nil then DestroyHandle;//这里最终会进到TWinControl.DestroyWindowHandle;
inherited SetParent(AParent);
if Parent = nil then UpdateControlState;
end;
end;procedure TWinControl.DestroyWindowHandle;
begin
Include(FControlState, csDestroyingHandle);
try
if not Windows.DestroyWindow(FHandle) then
RaiseLastOSError;
finally
Exclude(FControlState, csDestroyingHandle);
end;
FHandle := 0;//更改句柄
end;
begin
Include(FControlState, csDestroyingHandle);
try
if not Windows.DestroyWindow(FHandle) then
RaiseLastOSError;
finally
Exclude(FControlState, csDestroyingHandle);
end;
FHandle := 0;
end;
重新创建form2,
你的那种改变form的父亲的思路是要不得的
是变的。我测试了如下代码procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear ;
form2.Show ;1: Memo1.Lines.Add(format('%u %u', [form2.Handle, FindWindow('TForm2', 'Form2')])); form2.Parent := Self ;
//Sleep(5000) ;
2: Memo1.Lines.Add(format('%u %u', [form2.Handle, FindWindow('TForm2', 'Form2')])); form2.Parent := nil ;
//Sleep(5000) ;
3: Memo1.Lines.Add(format('%u %u', [form2.Handle, FindWindow('TForm2', 'Form2')])); form2.Parent := Self ;
//Sleep(5000) ;
4: Memo1.Lines.Add(format('%u %u', [form2.Handle, FindWindow('TForm2', 'Form2')]));
f9 运行程序,结果如下: form2.handle findwindow parent
1: 2688084 2688084 Application
2: 2753620 2950424 form1
3: 2819156 2819156 nil
4: 2884692 2950424 form1
从上面的数据可以看到:
1,3 中,form2.handle = findwindow ;
1,2,3,4中 ,form2.handle 各不相同;
2,4中,findwindow 相同;
关闭 form2,再次执行上面的代码,结果是:
form2.handle findwindow parent d
1: 1770722 2950424 Application
2: 1770722 2950424 form1
3: 1836258 1836258 nil
4: 1901794 2950424 form1从结果看:
3中 form2.handle = findwindow
1,2中,form2.handle 和 findwindow 分别对应相等;
2,4中, findwindow 对应相等;
再次运行的结果和第二此的结果类似,只是具体数值不同而已。
当改变form2的BoardStyle时,form1的Handle也会变
真的很不爽,delphi为什么会这样做
哪位高人给个理由先