Results 1 to 7 of 7

Thread: Forms TMainMenu tutorial

  1. #1
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    3,137
    Mentioned
    3 Post(s)
    Quoted
    5 Post(s)

    Default Forms TMainMenu tutorial

    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:
    SCAR Code:
    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:
    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.

  2. #2
    Join Date
    Feb 2006
    Posts
    406
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    NICE!!

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

  3. #3
    Join Date
    Oct 2006
    Posts
    500
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by masquerader View Post
    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?!?

  4. #4
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    3,137
    Mentioned
    3 Post(s)
    Quoted
    5 Post(s)

    Default

    Quote Originally Posted by r!ch!e View Post
    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

  5. #5
    Join Date
    Feb 2006
    Location
    London, England
    Posts
    2,045
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    fuck im gonna have an orgasm...

    FUCKING AMAZING!

    SRL Wiki | SRL Rules | SRL Stats
    Ultimate SCAR Scripting Tutorial | Starblaster100's Auth System | Join the official SRL IRC now!


    Help Keep SRL Alive! Please disable Advert Blockers on SRL! Help Keep SRL Alive!


  6. #6
    Join Date
    Aug 2006
    Location
    London
    Posts
    2,021
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    very good, ill be intrested when i make another scar game

    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
    Join the Official SRL IRC channel. Learn how to Here.

  7. #7
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    3,137
    Mentioned
    3 Post(s)
    Quoted
    5 Post(s)

    Default

    Quote Originally Posted by Yakman View Post
    i bet you got the idea for this when you pressed Ctrl+Space
    No, i didn't actually...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Complete forms tutorial
    By tim46 in forum OSR Advanced Scripting Tutorials
    Replies: 29
    Last Post: 06-12-2009, 11:43 PM
  2. Forms TTimer tutorial
    By Freddy1990 in forum OSR Advanced Scripting Tutorials
    Replies: 21
    Last Post: 01-29-2008, 06:31 AM
  3. Replies: 8
    Last Post: 08-23-2007, 08:13 AM
  4. Canvas on TMainMenu and TPopUp List!!
    By SKy Scripter in forum OSR Advanced Scripting Tutorials
    Replies: 4
    Last Post: 06-27-2007, 01:13 AM
  5. Forms TScrollBar tutorial
    By Freddy1990 in forum OSR Advanced Scripting Tutorials
    Replies: 2
    Last Post: 11-22-2006, 12:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •