您的位置:首页 > 编程语言 > Delphi

Delphi - 窗体创建过程

2010-01-08 17:42 555 查看
来自大富翁.

1、TCustomForm.Create
在 TCustomForm.Create 中调用 TCustomForm.CreateNew;

2、TCustomForm.CreateNew;
调用 FCanvas := TControlCanvas.Create;
触发 TControlCanvas.Create;
触发 TControlCanvas.CreateHandle;

3、TControlCanvas.CreateHandle;
调用 FControl.GetDeviceContext(FWindowHandle);
即 TWinControl.GetDeviceContext(FWindowHandle);

4、TWinControl.GetDeviceContext(FWindowHandle);
调用 TWinControl.GetDC(Handle);

此处说明一下:
对 TWinControl 的 Handle 属性的读取会触发 TWinControl.GetHandle;
可以察看 Property Handle; 的声明。

5、第四步中对 Handle 进行读取,触发下述序列:(TWinControl)
Handle->GetHandle->HandleNeeded

6、TWinControl.HandleNeeded 检查 FHandle 的值:
if FHandle = 0 then
begin
if Parent <> nil then Parent.HandleNeeded;
CreateHandle; // 调用 CreateHandle;
end;

7、TWinControl.CreateHandle
调用 CreateWnd;
if FHandle = 0 then // 此时 FHandle 仍然为零
begin
CreateWnd;
...
end;

8、TWinControl.CreateWnd
调用 CreateParams(Params);
// 让用户有机会加入新的特征参数
CreateParams(Params);
with Params do
begin
...
// 标准的 API 使用,注册窗口类,CreateWindowEx ...
if Windows.RegisterClass(WindowClass) = 0 then RaiseLastWin32Error;
...
CreateWindowHandle(Params);
...
end;

9、CreateWindowHandle(Params);
FHandle :=  CreateWindowEx(ExStyle, WinClassName, Caption, Style,
X, Y, Width, Height, WndParent, 0, WindowClass.hInstance, Param);
完成真正的窗口创建,并赋予 FHandle 窗口句柄。

10、回到第一步
CreateNew 之后调用 DoCreate
try
CreateNew(AOwner);
...
if OldCreateOrder then DoCreate;
finally
...
end;

11、DoCreate
调用用户的 OnCreate 事件:
if Assigned(FOnCreate) then
try
FOnCreate(Self); // 调用 OnCreate;
except
Application.HandleException(Self);
end;
if fsVisible in FFormState then Visible := True;

我们OnCreate事件最后才触发的.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: