Effective BCB FORM Program (2) - Form Event Drive Chain Starter Processor Error Analysis

zhaozj2021-02-11  186

Second, the list event drive chain created by the programmer with new ways to see the following code in the program: tfrmConstant * PForm = new tfrmconstant (this); // Create a form PFORM-> show () ...... delete pform; we often need to dynamically create a form, then display it, then destroy it in the right place. Note that the THIS parameters in the above code, which is transmitted to the TFRMConstant form constructor, which has a unique set of ginseng --owner. This parameter determines who is responsible for destroying it. The description of the Owner parameter in the BCB help file is as follows: Owner describes who is responsible for destroying this component.

Use the Owner parameter to access the interface of the Component owner. When a component has another component, the memory resources occupied by the subcaps are also released when the parent component memory is released. That is to say, when a form is destroyed, all components on the form will be deleted.

By default, a form has all components placed on it, similarly, Application objects have all FORM objects. Thus, when the program ends, the Application object will release all FORMs occupied resources.

Below is the experiment record experiment of my Owner parameter 1: Main Fact (Form1) New A normal form (Form2), Owner = this form1 code example: P2 = New TFORM2 (this); p2-> show () Create: After creating the main form, only the constructor of the ordinary form FORM2 is called, regardless of the display, the event drive chain is the same as the previous experiment. Close: Main Finde: OnClosequeryàonCloseàonhide () àdestory () à Main Window Structure Function àform2 Destructor Close order is the first master form again, if you have created multiple normal forms, according to the order of creation The principle that is first destroyed is destroyed one by one:

The normal form is displayed or hidden to the event drive chain has no effect. The main form (Form1) New A common form form2, Owner = Application creation: Turn off: First is Form1: onCloseQuery () àonclose () àform2 Destructor (if there are multiple, according to the order of the first order) àform1: onhide () à opeStory () àform1 destructor.

Experiment 3: Main Form (Form1) New One Inheritance Form (Form2 Inherited Since Form1), Owner = This creation: Create a master form Form1, the creation of the event chain is created; then starting in the code New Form2: Form2 Oncreate () àform1 constructor àform2 constructor à created end

We see that the constructor of the parent class is turned off: 1. Form2 not displayed: form1: onclosequery () àonclose () àOnHid () Eàdestory () àform1 destructor àform2 destructor à form1 destructor à Form2 OnDestory () Note: There is no Form1 onDestory, the same: The destructor of Form1 is executed twice 2. Form2 Show :: form1: onclosequery () àonclose () àmonhide () àondestory () àform1 destructor form2 onhide () à form2 destructor à form1 destructor à Form2 OnDestory only one FORM2 OnHide () Process Experiment 4: Main Factory New One Inheritance Form (Form2 Inherits Since Form1), Owner = Application: Creation (Form2 does not display): form2 oncreate () àform1 constructor à Form2 constructor is close (Form2 is not Display case) Form1 oncloseQuery () àonclose () àform2 destructor à form1 destructor à Form2 OnDestory () àmainform hideàmainform Destructor à mainform destructory function creation (Form2 display): form2 oncreate () àform1 Constructor à form2 onshow () à ... à Form2: onResize () A displayed event shutdown (Form2 display) Form1: onclosequery () àonclose () à form2: onhide () àform2 Destructor à form1 destructor à form2 onDestory () àform1 onhide () àform1 onDestoryà mainform1 destructor

Third, the MDI-type application event generates the primary form for mainform, the subsystem creates the mainform constructor àoncreate () à ... àoncreate () à ... àoncreate () When newing a MDI child form, Owner = Application or MainForm Mdichild constructor when àOnCreateàshowàActivate Close Owner = Application: MDIChild OnCloseQuery () (there are several subforms executed several times) àMainForm OnCloseQuery () àMainForm OnClose () à MDIChild onDestory () à MDIChild destructor àMainForm onDestory () àMainForm destructor

Owner = mainform: mdichild closeQuery () (several child forms) àmainform onclosequery () àmainform ondestory () àmainform ondestory () à mainform destructory function à msingr3ild onDestory () à mdichild destructor

Obviously, the Owner parameter determines the sequence of the main form and MDICHILD.

Useful conclusions From experiments we can draw some useful conclusions: About Oncreate and OnDestory events: 1. The Form's onCreate event is unreliable, not like the name is executed when the form is created, but when the form is a project's main form or a sub-form inherited other forms, So, in an initialization variable and creating objects in oncreate () are inappropriate; 2. In the case of inheritance, the oncreate event will occur before the constructor, and the corresponding ONDESTROY event occurs after the destructor. About the form of the structure and the destructive function 1. For any form, constructor and destructor are reliable because it will be performed every time you create and destroy the form. So we should perform the initialization operation of the program in the constructor, clean up resources in the destructive function; do not process in the OnCreate and the OnDestory event; 2. In the case of inheritance, the construction process of the BCB subform is first constructed by the parent form, and then construct the subform, which is exactly the same as C , and the destruction process is reverse; 3. In the case of inheritance, the constructor and the destructuring function of the parent class form will be executed multiple times, so do not new and delete objects in these two places, but should put this type of code in the specific sub-window. Body class. If this is not done, it may cause an error due to multiple deletion of the same object, unless the parent class form is declared as static, guarantees that all subclauses share the unique object of this NEW! 4. The destructor is always called, and the constructor is always the first call.

About Owner Parameters: When Owner = Application, first destroy the form out of New, then destroy the main form; and when Owner = this (refer to the main form), first destroy the main form, then sell the ruined form; The principle is also applicable to dynamically generating VCL controls inside the form. In particular, pay more attention when the data module is used in the program, or when it is necessary to dynamically generate a DataSet and other objects. Because a DataSet may provide data to multiple forms (should avoid doing this, the appropriate method is NEW TDATASET when the Owner parameter is not set to Application). Other: 1. A Visible = True form must first hide to be destroyed. 2. The source of the program event chain is the constructor of the main form of the project.

3. In the case of inheritance, the sub-form instance must first hide can be destroyed.

4. Multi-form management BCB uses a stack method, pressing the order created, first destroyed first, 10,000 to avoid the form that created first contains a pointer to the form (or control thereon there)).

5. Onclosequery events will occur when it is closed, if it is MDI, this event is also propagated in the MDICHILD form, so that the user's change is appropriate in this place.

6. In some cases, it will certainly follow a series of events, the order of these events is constant, for example, as long as the onshow event occurs, it will then take the following events:

àonactivate () àoncanresize () àonconstrainedresize () àonresize () onclosequery is ONCLOSE ()

转载请注明原文地址:https://www.9cbs.com/read-4366.html

New Post(0)