PDA

View Full Version : Forms TMainMenu tutorial



Freddy1990
11-19-2006, 06:40 PM
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:
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.
http://stuart.existhost.com/freddy/tutimages/menu1.PNG

Now the menu we are going to build is going to look like this:

(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:
mnu: TMainMenu;
Now we add this to our form by adding the following code to the InitForm procedure which will add the menubar to the form:
mnu := TMainMenu.Create(frmDesign);
So far we have:
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:
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:
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:
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:
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:
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.
http://stuart.existhost.com/freddy/tutimages/menu2.PNG

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:
mnuSub1, mnuSub2, mnuSub3: TMenuItem;

And we add the following code to the initform procedure to create the items and give them some text:
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:
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:
mnu.Items.Items[0].Items[0].Add(mnuSub2);
Finally we add the last subitem to the second menuitem:
mnu.Items.Items[2].Add(mnuSub3);

This results in the following script:
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.
http://stuart.existhost.com/freddy/tutimages/menu3.PNG

For adding events after clicking an item we just use the onclick property like for buttons or other components:
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.

masquerader
11-19-2006, 06:47 PM
NICE!!

i had no idea that you could make those menus!
they seem to be lacking from the form editor

Rick
11-19-2006, 07:01 PM
NICE!!

i had no idea that you could make those menus!
they seem to be lacking from the form editor


Same.

AWESOME!! Ill be using this knowledge. Thanks for sharing this.

How did you find this?!?

Freddy1990
11-19-2006, 07:11 PM
How did you find this?!?

I know ALOT about scar ;) With a bit of experimenting and my delphi knowledge i was able to compile this tut :)

Starblaster100
11-19-2006, 07:21 PM
fuck im gonna have an orgasm...

FUCKING AMAZING!

:D :D :D :D :D :D

Yakman
11-19-2006, 07:39 PM
very good, ill be intrested when i make another scar game :D

i bet you got the idea for this when you pressed Ctrl+Space
thanks for sharing, it looks very good

also starblaster, calm down, we dont want any sticky keys around here, it might make your keyboard short-circuit

Freddy1990
11-19-2006, 08:09 PM
i bet you got the idea for this when you pressed Ctrl+Space

No, i didn't actually...