Delphi窗體的建立與銷毀基礎教程

2021-08-07 01:29:45 字數 1275 閱讀 6734

delphi中的窗體分為模式窗體和無模式窗體。二者的區別在於,使用者可以在無模式窗體和其他窗體之間切換。這樣,使用者就可以同時工作於乙個應用程式的幾個部分。delphi中窗體的初始化有兩種情況,動態建立,和自動建立。通過show顯示乙個無模式窗體,showmodal顯示乙個模式窗體。窗體有建立對應的也要考慮釋放問題。當關閉乙個窗體時,窗體並沒有真正從記憶體中釋放掉,它仍然存在於記憶體中,除非關閉了主窗體。因為模式窗體於無模式窗體的不同,所以二者的釋放處理也有不同。

因為模式窗體可以判斷什麼時候關閉窗體,可以用一下**來建立和釋放窗體。

begin

trymodalform.showmodal;//顯示模式窗體

finally

modalform.free; //釋放窗體例項

//showmessage(booltostr(modalform = nil));

modalform := nil; //把窗體變數設為nil

//showmessage(booltostr(modalform = nil));

end;

end;

begin

form1.show;

end;

用上述**會導致每次都建立窗體例項,從而消耗了大量的記憶體。

我們可以考慮讓窗體只建立一次,同時在窗體關閉的時候將釋放掉。

begin

if not assigned(modallessform) then //確保只建立乙個視窗

begin

end;

modallessform.show;

end;

以上**就是乙個單例模式,確保只建立一次。當關閉窗體後,如果希望在記憶體中釋放掉,就要處理器onclose事件,吧action設為cafree,同時還要在ondestroy事件中將窗體變數指向nil。

procedure tmodallessform.formclose(sender: tobject;

var action: tcloseaction);

begin

action := cafree; //當關閉視窗時釋放窗體

end;

procedure tmodallessform.fomdestroy(sender: tobject);

begin

modallessform := nil; //在視窗銷毀時,把modallessform變數設為nil

end;

Delphi中的窗體建立與銷毀

delphi中的窗體,有模式窗體與非模式窗體兩種。兩種窗體的呼叫方式不同,模式窗體使用showmodal顯示,非模式窗體使用show顯示。當顯示模式窗體的時候你是不能操作本程式的其他窗體的,你不能把焦點從模式窗體轉到其他窗體上。而非模式窗體則沒有這種限制,你可以從乙個非模式窗體切換到另外乙個非模式窗...

Delphi 窗體建立過程

來自大富翁.1 tcustomform.create 在 tcustomform.create 中呼叫 tcustomform.createnew 2 tcustomform.createnew 呼叫 fcanvas tcontrolcanvas.create 觸發 tcontrolcanvas.c...

DELPHI動態建立窗體

第一種方式 procedure tform1.btn1click sender tobject begin try showmodal finally free end end 第二種方式 procedure tform1.btn2click sender tobject begin if not ...