(For 9CBS use, other users contact the author) BCB provides a large number of VCL components in the BCB, sometimes it is necessary to dynamically create components in the program, VCL is written with Object Pascal, she is still with C languages, some different Point, to master the right way, do not prevent us to see the relationship between stack and heap (HEAP).
Stack (stack) is a memory of all dynamic local variables and functions of the storage function and the returned information. The stack of memory management strictly follows the advanced order, which is desirable to implement function calls. The allocation of memory is particularly high from the stack. The data object uses the memory in the stack (such as dynamic local variable) than the memory in use, makes the program faster.
Heap (HEAP) is a single memory for Malloc (), Calloc (), Realloc (), and New. Get memory from the heap than much slower than from the stack, but the memory management of the stack is much flexible than the stack, you can get (or release) memory from the heap, we can do it in any order. The memory used to store recursive data structures is almost all from the heap. The memory used to store strings is usually acquired from the heap, especially for a long string that may appear when running.
The memory obtained from the heap is released with free (), delete, it is not automatically released.
C Compiled Program produces such high-quality code, the rapid use of the program run is related to the correct use of the stack, but all objects in Object Pascal can only be constructed in the heap, can not be like C , can be in the stack (in The object created in the function), the data segment (objects created outside the function), the stack (using a function such as New "to create a class of objects) build an object, so the object of the VCL class can only be in the heap create.
If you create a button object, we can create this:
TButton * btNmy = new TButton (from1);
You can write as follows: Class name * Object name = new class name (...);
Note: () inside can be the parent class name, engine name, null or this. But it is best to be the parent class name of the object.
Example: Dynamic Generating Button
Let's first put a button button1 on the form (Form1), and write as follows in his clicking event:
Void __fastcall tform1 :: button1click (Tobject * Sender)
{
TButton * my = new TButton (Form1);
MY-> Parent = form1; // The most critical sentence, otherwise you will not see anything, but compile is correct
MY-> TOP = 200;
MY-> left = 200;
MY-> height = 25;
MY-> width = 75;
MY-> CAPTION = "I'm Button!";
// my-> visible = true; this statement can be available, because his parent class usually defaults to him.
}
Through this example we should clearly see several important steps of dynamically create components:
1) To a space (memory); // tbutton * my = new TButton (Form1);
2) Specify its parent components, saying that it is the object we want to create to be placed on that container; // my-> parent = Form1;
3) The specified component should appear in the position of the parent class; // my-> TOP = 200; my-> left = 200; my-> height = 25; my-> width = 75; so this property You must set up Good location; 4) Other important properties. // my-> caption = "I'm button!";
And its order cannot be reversed, otherwise your program will joke.
Add the corresponding header file when dynamically generating the original components of the non-Baolian VCL. For example, we must dynamically generate report components must join:
#include "qrctrls.hpp" // If there is a problem, you have to join:
#include "quickrpt.hpp"
In addition, due to the conflict of BCB's conflict of memory, your dynamic creation program may not be a little wrong, but it is compiled; sometimes it is the first time, the second time is not passed, there is this Such a prompt, the easiest way is to cancel the system, try again, mostly can solve it.