深入VCL源碼研究DELPHI表單的建立和關閉

來源:互聯網
上載者:User

在DELPHI中,我們通常使用Application.CreateForm(TForm2, Form2)和TForm.create來建立表單,我們幾乎無法區別這兩種方法差異,更何況,我們更多的時候都是在使用TForm.create來產生子表單。

       不過,仔細觀察VCL源碼,你會發現,其實兩者區別很大。

procedure TApplication.CreateForm(InstanceClass: TComponentClass; var Reference);var   Instance: TComponent;begin   Instance := TComponent(InstanceClass.NewInstance);   TComponent(Reference) := Instance;  try     Instance.Create(Self);   except     TComponent(Reference) := nil;     raise;   end;  if (FMainForm = nil) and (Instance is TForm) then   begin     TForm(Instance).HandleNeeded;     FMainForm := TForm(Instance);   end;end;constructor TCustomForm.Create(AOwner: TComponent);begin   GlobalNameSpace.BeginWrite;  try     CreateNew(AOwner);    if (ClassType <> TForm) and not (csDesigning in ComponentState) then     begin       Include(FFormState, fsCreating);      try        if not InitInheritedComponent(Self, TForm) then           raise EResNotFound.CreateFmt(SResNotFound, [ClassName]);      finally         Exclude(FFormState, fsCreating);       end;      if OldCreateOrder then DoCreate;     end;  finally     GlobalNameSpace.EndWrite;   end;end; 

       Form1 := TForm1.Create(Application); 是先調用TForm1的Create方法, 然後賦值 給Form1變數。而Application.CreateForm(TForm1, Form1); 他會先得到一個Instance的指標, 把這個指標賦值給Form1, 然後是Form1.Create(Application). 這Tform1.create的區別在於, 在TForm1的OnCreate事件中, 我們可以使用Form1這個變數。

        千萬不要小瞧這點區別。例如你的程式有多個表單,各個子表單都是在需要的時候通過Tform1.create動態產生的,你想在FormOnCreate 事件中對表單上的edit1賦值text屬性,那麼你不能直接使用Form1.edit1.text := 'wudi_1982',你可以使用self.edit1.text 或者直接使用edit1.text。此時,你可能會想,可以直接用edit1.text,我為什麼要多寫form1.edit1.text呢?這裡除了了 解兩者的區別,更重要的在於,如果你的程式中有一個函數,函數並非寫在表單類中,此函數調用了form上的資訊,而在初始化的時候,你又必須調用它,如果 不明白此中道理,可能就這個問題,就要讓你調試好長時間,關於這方面的例子我就不寫了。在DELPHI的DEMO程式中,又一個關於ListView的, 其中就有類似的情況,只不過那個DEMO程式只有一個表單,用不到Tform.create,如果有興趣,你可以把那個常式添加到一個已存在的工程中,然 後用兩種不同的方法產生,你就會發現問題了。

二、表單的關閉

       通常情況下,我們對於程式中子表單的關閉,大多是使用close方法或者直接點擊表單右上方的關閉按鈕。那麼對於VCL的表單,它真的“關閉”了嗎?在預設情況下,答案是否定的。觀察VCL源碼,你會發現,那個關閉只能算做隱藏。至於怎麼測試,我想你知道。

       要徹底關閉表單並釋放資源,就要調用他的free方法(模式表單的常用辦法),或者在onclose事件中,設定Action := caFree(無模式表單的常用辦法),如果表單還要通過並且將自身賦值為nil。關於為什麼手動做form1 := nil的操作,我這裡就不多說了,

TCloseAction = (caNone, caHide, caFree, caMinimize);procedure TCustomForm.Close;var   CloseAction: TCloseAction;begin  if fsModal in FFormState then     ModalResult := mrCancel  else    if CloseQuery then     begin      if FormStyle = fsMDIChild then        if biMinimize in BorderIcons then           CloseAction := caMinimize else           CloseAction := caNone      else         CloseAction := caHide;       DoClose(CloseAction);      if CloseAction <> caNone then        if Application.MainForm = Self then Application.Terminate        else if CloseAction = caHide then Hide        else if CloseAction = caMinimize then WindowState := wsMinimized        else Release;     end;end;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.