In this tutorial we will learn how to add and use the TMainMenu component in SCAR forms.
We begin with a basic script that shows a form:
SCAR Code:
program MenuTest;
var
frmDesign: TForm;
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'Menu Testing Form';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
end;
procedure SafeInitForm;
var
v: TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('InitForm', v);
end;
procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;
procedure SafeShowFormModal;
var
v: TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('ShowFormModal', v);
end;
begin
SafeInitForm;
SafeShowFormModal;
end.
Now the menu we are going to build is going to look like this:
Code:
(Menu1)(Menu2)(Menu3)
We're going to give menu1 a submenu and that we give another submenu, menu2 we elso give a submenu.
First we are going to add out 3 menus, to do that we need a menu bar first, so we add the following to the variable declaration:
Now we add this to our form by adding the following code to the InitForm procedure which will add the menubar to the form:
SCAR Code:
mnu := TMainMenu.Create(frmDesign);
So far we have:
SCAR Code:
program MenuTest;
var
frmDesign: TForm;
mnu: TMainMenu;
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'Menu Testing Form';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
mnu := TMainMenu.Create(frmDesign);
end;
procedure SafeInitForm;
var
v: TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('InitForm', v);
end;
procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;
procedure SafeShowFormModal;
var
v: TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('ShowFormModal', v);
end;
begin
SafeInitForm;
SafeShowFormModal;
end.
If you wonder why you don't see anything special when running this, thats because there aren't any items on the bar so it won't display.
So we'll add some!
First we add the items to the variable declaration:
SCAR Code:
mnuMenu1, mnuMenu2, mnuMenu3: TMenuItem;
Before we can add these items to the menubar we have to create them, we do by adding the following lines to the initform procedure:
SCAR Code:
mnuMenu1 := TMenuItem.Create(frmDesign);
mnuMenu2 := TMenuItem.Create(frmDesign);
mnuMenu3 := TMenuItem.Create(frmDesign);
This will create the items inside the form.
Of course we have to add some text to the items before adding them, we do this by putting text into their Caption property:
SCAR Code:
mnuMenu1.Caption := 'Menu1';
mnuMenu2.Caption := 'Menu2';
mnuMenu3.Caption := 'Menu3';
Now we add them to the menubar by putting the following code in the initform procedure:
SCAR Code:
mnu.Items.Add(mnuMenu1);
mnu.Items.Add(mnuMenu2);
mnu.Items.Add(mnuMenu3);
As you can propably see, this adds the menuitems to the itemslist of the menubar.
We now have the following script:
SCAR Code:
program MenuTest;
var
frmDesign: TForm;
mnu: TMainMenu;
mnuMenu1, mnuMenu2, mnuMenu3: TMenuItem;
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'Menu Testing Form';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
mnu := TMainMenu.Create(frmDesign);
mnuMenu1 := TMenuItem.Create(frmDesign);
mnuMenu1.Caption := 'Menu1';
mnu.Items.Add(mnuMenu1);
mnuMenu2 := TMenuItem.Create(frmDesign);
mnuMenu2.Caption := 'Menu2';
mnu.Items.Add(mnuMenu2);
mnuMenu3 := TMenuItem.Create(frmDesign);
mnuMenu3.Caption := 'Menu3';
mnu.Items.Add(mnuMenu3);
end;
procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('InitForm', v);
end;
procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;
procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('ShowFormModal', v);
end;
begin
SafeInitForm;
SafeShowFormModal;
FreeForm(frmDesign);
end.
Now we run our script and we see that a menu bar with our 3 items has appeared at the top of the form.
We were going to add some subitems, we create them like our other menuitems with the variabledeclarations:
SCAR Code:
mnuSub1, mnuSub2, mnuSub3: TMenuItem;
And we add the following code to the initform procedure to create the items and give them some text:
SCAR Code:
mnuSub1 := TMenuItem.Create(frmDesign);
mnuSub1.Caption := 'Sub1';
mnuSub2 := TMenuItem.Create(frmDesign);
mnuSub2.Caption := 'Sub2';
mnuSub3 := TMenuItem.Create(frmDesign);
mnuSub3.Caption := 'Sub3';
Adding the subitems to the menu is a bit different than adding the main items. Each main item is given an indox in the items list, like an array, item 1 would have index 0, item 2,index 1 and item 3,index 2.
to add a subitem to an item we use the item's property 'items' with the index we want to add an item to using the add property, for the first item we do this as followed:
SCAR Code:
mnu.Items.Items[0].Add(mnuSub1);
The subitem has been given index 0 in the itemslist of the first menuitem, so we do the same with the subitem for adding the 2nd subitem as it's subitem with the following code:
SCAR Code:
mnu.Items.Items[0].Items[0].Add(mnuSub2);
Finally we add the last subitem to the second menuitem:
SCAR Code:
mnu.Items.Items[2].Add(mnuSub3);
This results in the following script:
SCAR Code:
program MenuTest;
var
frmDesign: TForm;
mnu: TMainMenu;
mnuMenu1, mnuMenu2, mnuMenu3: TMenuItem;
mnuSub1, mnuSub2, mnuSub3: TMenuItem;
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'Menu Testing Form';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
mnu := TMainMenu.Create(frmDesign);
mnuMenu1 := TMenuItem.Create(frmDesign);
mnuMenu1.Caption := 'Menu1';
mnu.Items.Add(mnuMenu1);
mnuMenu2 := TMenuItem.Create(frmDesign);
mnuMenu2.Caption := 'Menu2';
mnu.Items.Add(mnuMenu2);
mnuMenu3 := TMenuItem.Create(frmDesign);
mnuMenu3.Caption := 'Menu3';
mnu.Items.Add(mnuMenu3);
mnuSub1 := TMenuItem.Create(frmDesign);
mnuSub1.Caption := 'Sub1';
mnu.Items.Items[0].Add(mnuSub1);
mnuSub2 := TMenuItem.Create(frmDesign);
mnuSub2.Caption := 'Sub2';
mnu.Items.Items[0].Items[0].Add(mnuSub2);
mnuSub3 := TMenuItem.Create(frmDesign);
mnuSub3.Caption := 'Sub3';
mnu.Items.Items[1].Add(mnuSub3);
end;
procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('InitForm', v);
end;
procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;
procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('ShowFormModal', v);
end;
begin
SafeInitForm;
SafeShowFormModal;
FreeForm(frmDesign);
end.
For adding events after clicking an item we just use the onclick property like for buttons or other components:
SCAR Code:
program MenuTest;
var
frmDesign: TForm;
mnu: TMainMenu;
mnuMenu1, mnuMenu2, mnuMenu3: TMenuItem;
mnuSub1, mnuSub2, mnuSub3: TMenuItem;
procedure Sub2Click(sender: TObject);
begin
WriteLn('You clicked mnuSub2.');
end;
procedure Sub3Click(sender: TObject);
begin
WriteLn('You clicked mnuSub3.');
end;
procedure Mnu3Click(sender: TObject);
begin
WriteLn('You clicked mnuMenu3.');
end;
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'Menu Testing Form';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
mnu := TMainMenu.Create(frmDesign);
mnuMenu1 := TMenuItem.Create(frmDesign);
mnuMenu1.Caption := 'Menu1';
mnu.Items.Add(mnuMenu1);
mnuMenu2 := TMenuItem.Create(frmDesign);
mnuMenu2.Caption := 'Menu2';
mnu.Items.Add(mnuMenu2);
mnuMenu3 := TMenuItem.Create(frmDesign);
mnuMenu3.Caption := 'Menu3';
mnuMenu3.OnClick := @Mnu3Click;
mnu.Items.Add(mnuMenu3);
mnuSub1 := TMenuItem.Create(frmDesign);
mnuSub1.Caption := 'Sub1';
mnu.Items.Items[0].Add(mnuSub1);
mnuSub2 := TMenuItem.Create(frmDesign);
mnuSub2.Caption := 'Sub2';
mnuSub2.OnClick := @Sub2Click;
mnu.Items.Items[0].Items[0].Add(mnuSub2);
mnuSub3 := TMenuItem.Create(frmDesign);
mnuSub3.Caption := 'Sub3';
mnuSub3.OnClick := @Sub3Click;
mnu.Items.Items[1].Add(mnuSub3);
end;
procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('InitForm', v);
end;
procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;
procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('ShowFormModal', v);
end;
begin
SafeInitForm;
SafeShowFormModal;
FreeForm(frmDesign);
end.
And that concludes this tutorial, have fun.