Delphi - 窗体创建过程

来自大富翁.

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事件最后才触发的.

program Hackdiy; uses Windows, Messages; var TheMessage: TMsg; const ClassName = 'MainForm_FOrm1'; // 窗体过程回调函数 function FormProc(hForm, MsgID, WParam, LParam: LongWord): LongWord; stdcall; const {$J+} TempFont: DWORD = 0; {$J-} ControlID1 = 1; ControlID2 = 2; ControlID3 = 3; var EditText: array[0..30] of Char; begin Result := DefWindowProc(hForm, MsgID, WParam, LParam); // 标准处理 case MsgID of WM_CREATE: begin TempFont := CreateFont(12, 6, 0, 0, FW_EXTRALIGHT, Byte(FALSE), Byte(FALSE), Byte(FALSE), GB2312_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, '宋体'); CreateWindowEx(0, 'BUTTON', '退出程序', WS_CHILD or WS_VISIBLE, 60, 20, 70, 25, hForm, ControlID2, HInstance, nil); SendMessage(GetDlgItem(hForm, ControlID2), WM_SETFONT, TempFont, 1); end; WM_COMMAND: begin if (HIWORD(wParam) = BN_CLICKED) then // 单击按钮 begin case LOWORD(wParam) of // 控件ID ControlID1: begin end; ControlID2: SendMessage(hForm, WM_CLOSE, 0, 0); end; end; end; WM_DESTROY: begin PostQuitMessage(0); DeleteObject(TempFont); end; end; end; // 注册窗体类 procedure Register_MainForm; var FormClass: TWndClass; begin FormClass.Style := CS_HREDRAW or CS_VREDRAW; FormClass.lpfnWndProc := @FormProc; FormClass.cbClsExtra := 0; FormClass.cbWndExtra := 0; FormClass.hInstance := SysInit.HInstance; FormClass.hIcon := LoadIcon(HInstance, 'Cool'); FormClass.hCursor := LoadCursor(0, IDC_ARROW); FormClass.hbrBackground := COLOR_WINDOW; FormClass.lpszMenuName := nil; FormClass.lpszClassName := ClassName; RegisterClass(FormClass); end; // 注销窗体类 procedure Unregister_MainForm; begin UnregisterClass(ClassName, HInstance); end; // 建立主窗体 procedure Create_MainForm; begin CreateWindowEx(WS_EX_TOPMOST, ClassName, 'Form1', WS_VISIBLE or WS_TILED or WS_SYSMENU or WS_MINIMIZEBOX or WS_SIZEBOX, 200, 200, 200, 100, 0, 0, HInstance, nil); end; begin Register_MainForm; Create_MainForm; while GetMessage(TheMessage, 0, 0, 0) do begin TranslateMessage(TheMessage); DisPatchMessage(TheMessage); end; end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值