6. Fundamental properties of the forms
6.1. Derived TCustomForm
Activate < br / > < br / > specify when the form has the focus.
__property bool Active = {read = FActive, nodefault};
< br / > If Active is true, the form has focus; if enable is false, the form does not have focus.< br / > < br / > ActiveControl < br / > < br / > This property serves to establish what control has the focus when you open the form.
__property Controls:TWinControl ** ActiveControl = {read = FActiveControl, write = SetActiveControl, stored = IsForm;}
< br / > The following event handler responds to timer events by moving the active control one pixel to the right:< br / > void __fastcall TForm1:Timer1Timer(TObject_*Sender)
{ActiveControl - > Left = ActiveControl - > Left + 1;}
< br / > ActivateMDIChild < br / > < br / > Specifies that child MDI form has the focus.< br / > when its value, this property returns a pointer to the window of query MDI active daughter. It's a read-only property. Returns a NULL value if no window MDI active daughter or if the application is not MDI. < br / > < br / > __property TForm ** ActiveMDIChild = {read = GetActiveMDIChild;} < br / > < br / > < em > This code uses a button on an MDI application. When the user clicks the button, the active MDI child form is Visual.
void __fastcall TForm1:Button1Click(TObject_*Sender) {TForm ** TheForm;}
TheForm = Form1 - > ActiveMDIChild;
If (TheForm) TheForm - > WindowState = wsMinimized;
}
BorderIcons < br / > < br / > specify which icons appear in the toolbar of the title of the form.
enum TBorderIcon {biSystemMenu, biMinimize, biMaximize, biHelp}; typedef Set TBorderIcons; __property TBorderIcons BorderIcons = {read = FBorderIcons, write = SetBorderIcons, stored = IsForm, default = 7}
Value | Meaning |
biSystemMenu | The form has a Control menu (also known as a System menu). |
biMinimize | The form has a Minimize button |
biMaximize | The form has a Maximize button |
biHelp | If BorderStyle is bsDialog or biMinimize and biMaximize are excluded, a question mark appears in the form's title bar and when clicked, the cursor changes to crHelp; otherwise, no question mark appears. |
The following code removes a form's Maximize button when the user clicks per button:
void __fastcall TForm1:Button1Click(TObject_*Sender) {BorderIcons = BorderIcons-(TBorderIcons() < < biMaximize);}
BorderStyle < br / > < br / > indicates what kind of edge should have the form. The default value is bsSizable (which the window can change size).
enum TFormBorderStyle {bsNone, bsSingle, bsSizeable, bsDialog, bsToolWindow, bsSizeToolWin}; __property TFormBorderStyle BorderStyle = {read = FBorderStyle write = SetBorderStyle stored = IsForm, default = 2};
value | meaning |
BsDialog | not resizable; standard edge of dialog boxes. |
BsSingle | not resizable; edge with a simple line. |
BsNone | resizable not; the non-visible edge line. |
BsSizeable | standard resizable edge. |
BsToolWindow | like bsSingle but with the small caption. |
BsSizeToolWin | like bsSizeable but with the small caption. |
< br / > Canvas < br / >
provides access to the client area of the form to draw. < br / > Camvas property gives access to the surface of the form to draw bitmaps, lines, contours and text at runtime. The Label component is usually used to write text, the Image to display graphics and Shape components to draw outlines or silhouettes. But sometimes it is necessary to draw in time of execution. also serves to save disk image of the form.
__property Graphics:TCanvas ** Canvas = {read = GetCanvas;} void __fastcall TForm1:Button1Click(TObject_*Sender) {Graphics:TBitmap ** pBitmap = new Graphics:TBitmap;}
try {pBitmap - > LoadFromFile("C:ProgramFilesBorlandCBuilderImagesSplash256colorfactory.bmp_");}
pBitmap - > Transparent = true;
pBitmap - > TransparentColor = pBitmap - > Canvas - > Pixels [50,50];
Form1 - > Canvas - > Draw (0,0,pBitmap);
pBitmap - > TransparentMode = tmAuto;
/ / Transparent color now is clDefault = 0x20000000;
Form1 - > Canvas - > Draw (50,50,pBitmap);
} catch (...)
{ShowMessage("Could_not_load_or_display_bitmap");}
} delete pBitmap;
} The following example paints to Rectangle on the form when the user clicks on Button1.
void __fastcall TForm1:Button1Click(TObject_*Sender) {Canvas - > Rectangle (0, 0, 40, 40);}
ClientHeight < br / > < br / > specifies the height (in pixels) of the client area (inside the form area).
__property ClientHeight = {write = SetClientHeight, stored = IsClientSizeStored;}
This example reduce the height of the form's client area by half when the user clicks the button on the form:
void __fastcall TForm1:Button1Click(TObject_*Sender) {Form1 - > ClientHeight = Form1 - > ClientHeight/2;}
}
< br / > ClientRect < br / > < br / > This property contains the coordinates of the limits top, bottom, left and right of the client area of the form. < br / > specify the dimensions of the client area.
ClientRec equals a Rectangu (0,0,ClientWidth,ClientHeight) rectangle.
__property Windows:TRect ClientRect = {read = GetClientRect;}
< br / > ClientWidth < br / > < br / > specify the width (in pixels) of the client area (inside the form area).
__property ClientWidth = {write = SetClientWidth, stored = IsClientSizeStored;}
This example starts the form in a position so that the right hand edge shows: scrolled
void __fastcall TForm1:FormCreate(TObject_*Sender)
{/ ** set the width of the form window to display 300 pixels in the client area ** / ClientWidth = 300;}
/ ** set the range to the client width twice now.
This means that the form has a logical size that is twice as big as the physical window.
Note that Range must always be at least as big as ClientWidth! ** / HorzScrollBar - > Range = 600;
/ ** start the form fully scrolled ** / HorzScrollBar - > Position = HorzScrollBar - > Range - ClientWidth;
/ ** clicking the scroll arrows moves the form 10 pixels ** / HorzScrollBar - > Increment = 10;
HorzScrollBar - > Visible: = true; {/ / Show the scrollbar}
< br / > FormStyle < br / > < br / > determines the style of the form.
enum TFormStyle {fsNormal, fsMDIChild, fsMDIForm, fsStayOnTop}; __property TFormStyle FormStyle = {read = FFormStyle, write = SetFormStyle, stored = IsForm, default = 0};
value | meaning |
FsNormal | normal form (default). |
FsMDIChild | indicator of form secondary MDI. |
FsMDIForm | main MDI form. |
FsStayOnTop | the form is always in the foreground. |
This example ensures the main form of the application is an MDI parent form:
__fastcall TForm1:TForm1(TComponent*_Owner)
: TForm (Owner) {if(FormStyle_!=_fsMDIForm) FormStyle = fsMDIForm; if(FormStyle_==_fsMDIForm) Edit1 - > Text = "MDI form"; else //This line never runs Edit1 - > Text = "Not an MDI form";}
HelpFile < br / > < br / > specifies the name of the help file used in the application.
__property System:AnsiString HelpFile = {read = FHelpFile, write = FHelpFile;}
Icon < br / > < br / > indicates the icon that you can see in the title bar when the form is displayed at runtime or minimized. If link icon is not specified, Windows assigns one by default.
__property Graphics:TIcon ** Icon = {read = FIcon, write = SetIcon, stored = IsIconStored;}
This code assigns an icon to a form when the form is created:
__fastcall TForm1:TForm1(TComponent*_Owner): TForm (Owner) {Icon - > LoadFromFile ("C:\\PROGRAM FILES\\BORLAND\\CBUILDER\\IMAGES\\ICONS\\EARTH.")}("ICO");
}
KeyPreview < br / > < br / > specify when the form can receive events from the keyboard before active control.
- Si KeyPreview is true, keyboard events occur on the form, and then in the active control.
- If KeyPreview is false, the keyboard events only occur on active control.
__property bool KeyPreview = {read = FKeyPreview, write = FKeyPreview, stored = IsForm, default = 0};
This example changes a form's color to aqua when the user presses a key, even when a control on the form has the focus. When the user releases the key, the form returns to its original color.
color FormColor; void __fastcall TForm1:FormCreate(TObject_*Sender) {KeyPreview = true};
void __fastcall TForm1:FormKeyDown(TObject_*Sender,_WORD_&Key,_TShiftState_Shift) {FormColor = Form1 - > Color;}
Form1 - > Color = clAqua;
} void __fastcall TForm1:FormKeyUp(TObject_*Sender,_WORD_&Key,_TShiftState_Shift) {Form1 - > Color = FormColor;}
< br / > MDIChildCount
Specifies the number of forms open children.
__property int MDIChildCount = {read = GetMDIChildCount, nodefault};
The following code closes Form1 if there are no MDI children open:
void __fastcall TForm1:Button1Click(TObject_*Sender) {Close() if(MDIChildCount_==_0)};
MDIChildren
Provides an index to access the forms an MDI children.
__property TForm ** MDIChildren [int I] = {read = GetMDIChildren;}
The following code closes all the MDI children of Form1.
void __fastcall TForm1:Button1Click(TObject_*Sender) {for(int_i_=_0;_i_<_MDIChildCount;_i++) MDIChildren [i] - > Close();}
Menu < br / > < br / > the Menu property is used to load and activate the menu in the form at runtime. The TmainMenu component must be loaded in the form.
__property Menus:TMainMenu ** Menu = {read = FMenu, write = SetMenu, stored = IsForm;}
This code displays a new menu named MyMenu when the user clicks the button.
TMenuItem ** Item1;
TMainMenu ** MyMenu;
/ /-void __fastcall TForm1:Button1Click(TObject_*Sender) {Item1 = new TMenuItem (this)};
Item1 - > Caption = "New item";
MyMenu = new TMainMenu (this);
MyMenu - > Items - > Add (Item1);
Menu = MyMenu;
} / /-void __fastcall TForm1:FormDestroy(TObject_*Sender) {MyMenu for(int_i_=_0;_i_<_MyMenu->Items->Count;_i++) - > Items [i].}Remove (Item1);
delete Item1;
delete MyMenu;
}
ModalResult < br / > ObjectMenuItem < br / > OLE < br / > OleFormObject < br / > OLE < br / > Parent < br / > PixelsPerInch < br / >
Position
determines the size and position of the form when it opens for the first time.
enum TPosition {poDesigned, poDefault, poDefaultPosOnly, poDefaultSizeOnly, poScreenCenter}; __property TPosition Position = {read = FPosition, write = SetPosition, stored = IsForm, default = 0};
value | meaning |
PoDesigned | same position in the design phase. |
PoDefault | Windows sets the size and position according to the Z-ordering algorithm. |
PoDefaultPosOnly | same position in the phase of design. Windows select the position on the screen. |
PoDefaultSizeOnly | same position in the phase of design. Windows selects the size. |
PoScreenCenter | the form is always situated in the center of the screen. |
PrintScale
represents the proportions of the form in the printer.
enum TPrintScale {poNone, poProportional, poPrintToFit}; __property TPrintScale PrintScale = {read = FPrintScale, write = FPrintScale, stored = IsForm, default = 1};
value | meaning |
PoNone | does not apply any scale. The output of the form varies between a printer and another printer. |
PoProportional | the proportional option is intended to print the form as approximate to what looks on the screen as possible. |
PoPrintToFit | increases or reduces the size of the image to match the printer settings. |
The following code maintains the proportions of the form when it is printed.
Form1 - > PrintScale = poProportional;
Form1 - > Print();
Scaled < br / > TileMode < br / > MDI < br / >
Visible
Indicates if the form is initially visible or not. This property is very useful at runtime, establishes whether or not a form is visible.
__property Visible = {write = instantiates, default = 0;}
The following code uses two forms. Form1 has a button on it. The second form is used as a tool palette. This code makes the palette visible form, and ensures it is the top form by bringing it to the front. < br / > To run this example, you must include Unit2.h in the Unit1 source file.
void __fastcall TForm1:ShowPaletteButtonClick(TObject_*Sender) {if (!)}Form2-> Visible) {Form2 - > Visible = true;}
Form2 - > BringToFront();
}
}
WindowMenu
Uses the WindowsMenu property to load and activate the menu of the form MDI parent at runtime. The TmainMenu component must be loaded in the form.
__property Menus:TMenuItem ** WindowMenu = {read = FWindowMenu, write = SetWindowMenu, stored = IsForm;}
< br / > < em > For this code to run, to menu item called MyWindows must exist on an MDI parent form form. This line of code designates the MyWindows menu to be the Window menu, the menu that lists all open child windows in an MDI application:
WindowMenu = MyWindows;
WindowState
This property is available to determine the current status of the form (minimized, maximized, or normal). It can also be used to indicate how the form should show initially. The options are wsMinimized, wsMaximized and wsNormal.
enum TWindowState {wsNormal, wsMinimized, wsMaximized}; __property TWindowState WindowState = {read = FWindowState, write = SetWindowState, stored = IsForm, default = 0}
Value | Meaning |
WsNormal | The form appears in its normal state (that is, its non-Visual, non-maximized state). |
WsMinimized | The form appears in its visual state. |
WsMaximized | The form appears in its maximized state. |
The following code responds to the user clicking to button named Shrink by minimizing the form:
void __fastcall TForm1:ShrinkClick(TObject_*Sender) {WindowState = wsMinimized};