All About Forms!
Forms are a very important part of your
Windows applications, a Form is a Window.
Your program can have many Forms, such
as Dialog Boxes and your main program Form.
Borlands Delphi encapsulates the concept
of a Form into a class called TForm.
Using a Form may seem a simple task to you but the common and very hard working Form has many different qualities that are very important and you should learn as much as you can about Forms and how they work in your programs and how to use them to your advantage.
A Form has a title bar which can be removed with code. The title bar is helpful to your software users, it helps them know when a Form is active or not. You can also add components to the title bar like a button, but this is done with special code.
Components are put on the Forms client area.
You can resize Forms to suit your needs,
an example is you may want a small Form to use as a dialog box.
But you may decide your main application
Form will fill the entire screen area.
Forms also have a Control menu.
Adding some text to a Forms title bar is easy to do, click on the Form and then in the Object Inspector click next to the word Caption then type in some text, this text will now be seen at the top of your Form in the title bar.
Application.Minimize:
procedure TForm1.Button2Click(Sender: TObject);
begin
Application.Minimize;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Form1.WindowState := wsMaximized;
end;
Right-click on the Form then select View As Text and then you will see the Form in text, it should look something like this:
object Form1: TForm1 Left = 190 Top = 107 Width = 544 Height = 375 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 end
You can actually make changes to the Form by editing this text,
but this should only be done if you know what you are doing.
To see your Form as a Form again just right-click again and select View
As Form.
Forms are saved as a binary file with the extension of DFM - Delphi
ForM.
With Delphi 5 you can change your Forms to text.
You can also use a DOS command tool called Convert.exe which is able to translate Forms from the compiled Form to the textual version and visa-versa. Convert.exe comes with Delphi and you should find it in the bin directory (folder).
If you already have a project opened in the Delphi IDE and you click on the New Form button or go to File|New Form, Delphi will automatically create the Form in memory (Including the projects main Form) in the projects source unit like this:
Application.CreateForm(TForm1, Form1);This is what it looks like:
program Project1;uses Forms, Unit1 in 'Unit1.pas' {Form1};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
This is what it looks like after adding a new Form, in this case Form2:
program Project1;uses Forms, Unit1 in 'Unit1.pas' {Form1}, Unit2 in 'Unit2.pas' {Form2};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);
Application.Run;
end.
The two Forms in the above project will both exist in memory from the
moment your program is started until the moment you close (or end) your
program.
If you want to show Form2 you can add a button to Form1 and then add
this code:
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.Show;
end;
When you are using two forms, you can use Form2.Show; to make the second
form visible to the user and bring the form to the front of the other form,
form1.
This also enables the user to click between the two forms, form1 and
form2.
You can also use:
procedure TForm1.Button1Click(Sender: TObject); begin Form2.ShowModal; end;ShowModal will show a form as a modal form.
You don't have to have your programs Forms in memory at the programs
startup. You can have the Main Form auto-created and then have the rest
of your Forms created when your users need them.
An example of when this is a good idea is when you include an About
Box in your program, it makes no sense to have the About Box loaded in
memory when your programs starts because the user may have already seen
it and they may not ever want to see it again.
First you select New Form from the Menu or from the New Form button.
Next go to Project|Options then click on the Forms tab, to the left
you will see the list of Auto-Created Forms, select the Form you want to
create dynamically and then click on the button that has this sign on it
>, next click on the OK button. Now the Form will not be created automatically
when your program runs.
Use this code to create the Form when the program is running:
procedure TForm1.Button1Click(Sender: TObject); begin with TForm2.Create(Self) do try ShowModal; finally Free; end; end;As you can see this is not very hard to do.
You will know when your Form is active because Windows paints the title bar a different color when the Form is active and another color when it is not active. Only one Form in your program is active at any one time.
You can have a Form inside your Main Form. Here is an example of how to insert a Form into a Panel:
Start a new project.
Place a Panel on Form1, resize it so it is reasonably large.
Add a new Form (Form2).
Add this code to Form2.
procedure TForm2.FormCreate(Sender: TObject); begin Form2.Parent := Form1.Panel1; Form2.Visible := True; Form2.WindowState := wsMaximized; end;If you change the color of Form2 it stands out more.
Run the program.
You should now see Form1 with a panel on it and Form2 inside the panel.
You can minimize Form2 and then maximixe it.
It is also possible to draw directly onto a Forms Canvas, in other words
directly onto the Form, here are some simple examples to get you started:
Adding text to a form:
procedure TForm1.Button1Click(Sender: TObject); begin Canvas.TextOut(10, 30, 'Here!'); end;Drawing a box with some text in it:
procedure TForm1.Button1Click(Sender: TObject);
var AreaBox : TRect;
begin
AreaBox := Rect(100, 100, 200, 200);
Form1.Canvas.TextRect(AreaBox,100, 100,'Inserted
text');
end;
Drawing an Ellipse:
procedure TForm1.Button1Click(Sender: TObject); begin Canvas.Ellipse(100, 100, 160, 160); end;To learn more about drawing on Forms go to this web page: Drawing On A Form (Canvas)
You will find the AutoScroll property in the Object Inspector and it
is used to manage the (automatic) appearance of scroll bars on the Form
if the Form is too small to display all the controls on it.
If you set AutoScroll to False then the controls are cropped at the
Forms border, otherwise you can set the AutoScroll property to True this
will ensure that the Forms scroll bars will appear so that the user can
scroll up and down the Form to see and use all the controls on the Form.
The BorderIcons property is used so that you can determine which icons you want to appear on the Forms titlebar, BorderIcons is really a set of Boolean values.
Changing these BorderIcon values can change the way your Form looks
and how it will act in your program.
You should experiment with the BorderIcons values to see what combinations
you want.
biSystemMenu creates an icon (Really a drop-down menu) that is on the left hand side of your Form in the title bar.
biHelp will display the Help icon (A question mark - "Whats this?") on the right hand side of the Forms title bar.
If BorderStyle is bsDialog or biMinimize and biMaximize are excluded, a question mark appears in the form's title bar and if it is clicked the cursor changes to the crHelp cursor; otherwise, no question mark appears.
procedure TForm1.Button1Click(Sender: TObject);
var FormTwo : TForm2;
begin
FormTwo := TForm2.Create (Application);
FormTwo.BorderStyle := bsDialog;
FormTwo.Show;
end;
biMaximize is used to enable or un-enable the Maximize icon (button)
on the Forms title bar.
biMinimize is used to enable or un-enable the Minimize icon (button)
on the Forms title bar.
We haven't (yet) put anything together on our web site about shaped
forms, but we do have some helpful links that will help
you learn more about this subject.
The first one is "Gruhn's Transparent Delphi App" http://www.hwb.com/gruhn/programming/tutorial/
The second link leads to "Transparent window" http://www.greatis.com/tip00001.htm
You will find making shaped/transparent forms is not as hard as it looks.
Transparent Form Anyone?
The following example demonstrates how to create a transparent form.
Example:
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Brush.Style := bsClear;
Form1.BorderStyle := bsNone
end;
{Put an exit button on it.}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Application.Terminate;
end;
This is another example from: Clayton Turner
You use this to determine where you want the Form position on the screen to be when the user starts your program.
In the Object Inspector you can change the values for the Forms position,
the default setting is poDesigned.
The poDesigned value is the position and size that your form is in
at design time.
The poScreenCenter value puts your Form in the middle of the screen area.
Both poDefaultSizeOnly and poDefaultPosOnly are useful in MDI applications.
The poMainFormCenter value should really only be used in secondary Forms.
The Form will remain the size that you left it in at design time, but
it is positioned in the center of the application’s main Form.
If you use poMainFormCenter on a main Form then it will act like poScreenCenter.
With poDesktopCenter your Form remains the size that you left it at design time, but it is positioned in the center of the screen.
And with poDefault Windows will set your Forms Top, Left, Height and
Width automatically. This means you give up all
control over your Forms placement.
And with poOwnerFormCenter the Form remains the size you left it at design time, but is positioned in the center of the form specified by the Owner property. If the Owner property does not specify a Form, then this position acts like poMainFormCenter.
If you would like your Form to open at the top of the screen you would use:
Form1.Top := 0;
If you would like your Form to open as far over to the left then you would use this code:
Form1.Left := 0;
To make your Form as wide as the screen you would use this code:
Form1.Width := Screen.Width;
To make your Form as high as the screen you would use this code:
Form1.Height := Screen.Height;
If you want to resize your Form to the screen height and width you would use this code:
[procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;]
SetBounds(0, 0, Screen.Width, Screen.Height);
SetBounds will set the windowed control's boundary properties all at once.
To change the color of your Form you can use the Object Inspector or use this code:
Form1.Color := clNavy;
This will change the Forms color to Navy.
While you look at the Object Inspector for a Form (or a component) and
you see a property that you are not sure what
it means/does then just click on it once and then press the F1 key
and Help will open on that subject.
For information about Multiple Document Interface (MDI) then go to this
web page: Creating An MDI Application
Sometimes, I am in a situation where I need to be able to move a form
which has no caption bar.
I have seen many examples on the Web which involve handling all sorts
of Window messages, and the code is quite bulky. Just
add this to the onMouseDown event of the form(or a component you might
like to use as a "Caption")
Releasecapture; {Not quite sure what this means, but} {You may have
problems if it is not used}
Form1.Perform(WM_SYSCOMMAND, $F012, 0);
This assumes the form is named Form1 .
The code could also be applied to any other control, such as a button
or panel, e.g.
Releasecapture;
Panel1.Perform(WM_SYSCOMMAND, $F012, 0);
"Jon Nichols"
This is used to set the Forms font.
If you put a TLable component on a form then set the forms font to Ariel
Bold Italic the label wil then inherit the forms properties. In other words
the labels font will be Ariel Bold Italic.
It is the default font for all components on the form.
The Forms Icon Property
The forms in your Delphi applications can have a different icons (.ico) in the forms Title bar and you can use the forms icon property to do this. The icon is stored in the .dfm file so you do not need to include it with your software when you distrbute it.