Results 1 to 14 of 14

Thread: Form TPopupMenu tutorial

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

    Default Form TPopupMenu tutorial

    In this tutorial we will learn how to add and use the TPopupMenu component in SCAR forms.
    We begin with a basic script that shows a form:
    SCAR Code:
    program PopupMenuTest;

    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.


    In the example that we're making we're going to make the popupmenu appear when you press a button.
    So we add a button first:

    Variable declaration:
    SCAR Code:
    btn: TButton;

    Code for the initform procedure:
    SCAR Code:
    btn := TButton.Create(frmDesign);
    btn.Parent := frmDesign;
    btn.Caption := 'Open menu';
    btn.Left := 128;
    btn.Top := 41;
    btn.Width := 75;
    btn.Height := 25;
    btn.TabOrder := 8;

    This results in the script:
    SCAR Code:
    program PopupMenuTest;

    var
      frmDesign: TForm;
      btn: TButton;

    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;

      btn := TButton.Create(frmDesign);
      btn.Parent := frmDesign;
      btn.Caption := 'Open menu';
      btn.Left := 128;
      btn.Top := 41;
      btn.Width := 75;
      btn.Height := 25;
      btn.TabOrder := 8;
    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 we add the popupmenu, first we declare the variable:
    SCAR Code:
    mnu: TPopupMenu;

    Next we also add the variable declarations for the 3 menuitems we're going to add to the menu:
    SCAR Code:
    mnuMenu1, mnuMenu2, mnuMenu3: TMenuItem;

    First we add the code to create the menu in the form to the initform procedure:
    SCAR Code:
    mnu := TPopupMenu.Create(frmDesign);

    Now we do the same for all three menu items:
    [scar]mnuMenu1 := TMenuItem.Create(frmDesign);
    mnuMenu2 := TMenuItem.Create(frmDesign);
    mnuMenu3 := TMenuItem.Create(frmDesign);[scar]

    Of course we need to give our menuitems some text so we add the following code to the initform procedure to add text to the items caption property:
    SCAR Code:
    mnuMenu1.Caption := 'MenuItem1';
    mnuMenu2.Caption := 'MenuItem2';
    mnuMenu3.Caption := 'MenuItem3';

    Now we need to add them to the menu, so we put them in the menu's item list:
    SCAR Code:
    mnu.Items.Add(mnuMenu1);
    mnu.Items.Add(mnuMenu2);
    mnu.Items.Add(mnuMenu3);

    So now we've got a menu, but... where is it? We can't see it yet, it's a popupmenu, so we need to make it pop up, thats why we made the button, we're going add an onclick event to the button with a small procedure that will open our menu.
    To make the menu pop up we use the popup command BUT this requires an x and y coordinate, this is the coordinate where it has to pop up, so we're going to keep it simple and make it pop up at the cursor position.

    First we add x and y to store our mouse coordinates to the variable declarations:
    SCAR Code:
    x, y: Integer;

    We will simply name our procedure ButtonClick, we add a getmousepos procedure to get our mouse position and then the popup command to make our menu pop up when the procedure is ran.
    SCAR Code:
    procedure ButtonClick(sender: TObject);
    begin
      GetMousePos(x, y);
      mnu.Popup(x, y);
    end;

    Now we add an onlick event to our button in initform to make our menu show:
    SCAR Code:
    btn.OnClick := @ButtonClick;

    So now we have the following script:
    SCAR Code:
    program PopupMenuTest;

    var
      frmDesign: TForm;
      mnu: TPopupMenu;
      btn: TButton;
      mnuMenu1, mnuMenu2, mnuMenu3: TMenuItem;
      x, y: Integer;

    procedure ButtonClick(sender: TObject);
    begin
      GetMousePos(x, y);
      mnu.Popup(x, y);
    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;

      btn := TButton.Create(frmDesign);
      btn.Parent := frmDesign;
      btn.Caption := 'Open menu';
      btn.Left := 128;
      btn.Top := 41;
      btn.Width := 75;
      btn.Height := 25;
      btn.TabOrder := 8;
      btn.OnClick := @ButtonClick;

      mnu := TPopupMenu.Create(frmDesign);
      mnuMenu1 := TMenuItem.Create(frmDesign);
      mnuMenu1.Caption := 'MenuItem1';
      mnu.Items.Add(mnuMenu1);
      mnuMenu2 := TMenuItem.Create(frmDesign);
      mnuMenu2.Caption := 'MenuItem2';
      mnu.Items.Add(mnuMenu2);
      mnuMenu3 := TMenuItem.Create(frmDesign);
      mnuMenu3.Caption := 'MenuItem3';
      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.


    We now see our menu pop up when we press the button!

    Finally we're going to add fomr procedures for adding to the onclick events of the menu items to test our menu:
    SCAR Code:
    procedure Mnu1Click(sender: TObject);
    begin
      WriteLn('You clicked mnuMenu1.');
    end;

    procedure Mnu2Click(sender: TObject);
    begin
      WriteLn('You clicked mnuMenu2.');
    end;

    procedure Mnu3Click(sender: TObject);
    begin
      WriteLn('You clicked mnuMenu3.');
    end;

    SCAR Code:
    mnuMenu1.OnClick := @Mnu1Click;
    mnuMenu2.OnClick := @Mnu2Click;
    mnuMenu3.OnClick := @Mnu3Click;

    This leaves us with the following script:
    SCAR Code:
    program PopupMenuTest;

    var
      frmDesign: TForm;
      mnu: TPopupMenu;
      btn: TButton;
      mnuMenu1, mnuMenu2, mnuMenu3: TMenuItem;
      x, y: Integer;

    procedure ButtonClick(sender: TObject);
    begin
      GetMousePos(x, y);
      mnu.Popup(x, y);
    end;

    procedure Mnu1Click(sender: TObject);
    begin
      WriteLn('You clicked mnuMenu1.');
    end;

    procedure Mnu2Click(sender: TObject);
    begin
      WriteLn('You clicked mnuMenu2.');
    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;

      btn := TButton.Create(frmDesign);
      btn.Parent := frmDesign;
      btn.Caption := 'Open menu';
      btn.Left := 128;
      btn.Top := 41;
      btn.Width := 75;
      btn.Height := 25;
      btn.TabOrder := 8;
      btn.OnClick := @ButtonClick;

      mnu := TPopupMenu.Create(frmDesign);
      mnuMenu1 := TMenuItem.Create(frmDesign);
      mnuMenu1.Caption := 'MenuItem1';
      mnuMenu1.OnClick := @Mnu1Click;
      mnu.Items.Add(mnuMenu1);
      mnuMenu2 := TMenuItem.Create(frmDesign);
      mnuMenu2.Caption := 'MenuItem2';
      mnuMenu2.OnClick := @Mnu2Click;
      mnu.Items.Add(mnuMenu2);
      mnuMenu3 := TMenuItem.Create(frmDesign);
      mnuMenu3.Caption := 'MenuItem3';
      mnuMenu3.OnClick := @Mnu3Click;
      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.

    You can also add submenus to these menus, this works with the same concept as with the TMainMenu component, so please refer to my tutorial for those components top learn how to add submenus.

    And that concludes this tutorial, have fun.

  2. #2
    Join Date
    Sep 2006
    Posts
    5,219
    Mentioned
    4 Post(s)
    Quoted
    1 Post(s)

    Default

    Damn Freddy, tutorial diarrhea. Another cool tut.

  3. #3
    Join Date
    Feb 2006
    Location
    Helsinki, Finland
    Posts
    1,395
    Mentioned
    30 Post(s)
    Quoted
    107 Post(s)

    Default

    Freddy is going crazy with tutorials right now. ;d

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

    Default

    Quote Originally Posted by LaBo View Post
    Freddy is going crazy with tutorials right now. ;d
    Tomorrow 1 more and then i'm finished

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

    Default

    what will we be looking forward too tommorrow then ??

    TMenu
    TApplication
    TCanvas

    i was looking through scar and i wondered if you know what a TCMenuItem is

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

    Default

    Hmm, TMenus aren't useful, better to use tmainmenu or tpopupmenu, TCanvas and TApplication aren't even form components and tcmenuitem is a custommenuitem, but just use tmenuitems, they're better for usage

  7. #7
    Join Date
    Feb 2007
    Posts
    3,616
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    GREAT tut dude! best form tut I've found so far. although, for me to understand things 100%, i must read this tut over 5 more times lol. +rep

  8. #8
    Join Date
    Sep 2006
    Location
    New Jersey, USA
    Posts
    5,347
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    I've been wondering how to use tpopup menu's! Thanks a ton!

    Maybe a tut on making two forms in one script? I was trying to do it today, but I finally gave up, and just made a Tgroup thing that would change visibility.
    Interested in C# and Electrical Engineering? This might interest you.

  9. #9
    Join Date
    Oct 2006
    Location
    I'm a figment of your imagination
    Posts
    422
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Wow! Awesome tut. + Rep. Oh wait, I already gave you a few weeks ago. Can't give again .

    Anyways, I really feel I'm actually learning something while reading this.

    It's been a while... but I'm BACK!!!

  10. #10
    Join Date
    Dec 2006
    Location
    utah
    Posts
    1,427
    Mentioned
    2 Post(s)
    Quoted
    7 Post(s)

    Default

    Freddy1990
    i am having a hard time with TBITBUT
    but i a have figured out most of it. if you know how to stick a
    canvas on TBITBUTit please tell me!!!!

  11. #11
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Wow i learned it!
    Something for clarion v0.7

  12. #12
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Quoted
    4 Post(s)

    Default Very nice tutorial

    I got troubles with the string, i made a form to autologin, what do i do to declare username and password for it to autologin?
    i just get Line 65: [Error] (65:21): Unknown identifier 'ButtonClick' and other error messages and im tired of looking and looking without figure it out, what do i type for it to run a certain procedure whenever the button is clicked? in my case i want autologin.

  13. #13
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    Quote Originally Posted by tom99 View Post
    I got troubles with the string, i made a form to autologin, what do i do to declare username and password for it to autologin?
    i just get Line 65: [Error] (65:21): Unknown identifier 'ButtonClick' and other error messages and im tired of looking and looking without figure it out, what do i type for it to run a certain procedure whenever the button is clicked? in my case i want autologin.
    Please take a look at which date the thread was posted in / what date it last modification date is and what date the last post were posted, this thread is over 2 years old.

    To answer your question: http://www.villavu.com/forum/showthread.php?t=41418

  14. #14
    Join Date
    Apr 2007
    Posts
    3,152
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    tutorials like this never go out of date. Its not like anything has changed.
    SCAR Tutorials: The Form Tutorial | Types, Arrays, and Classes
    Programming Projects: NotePad | Tetris | Chess


Thread Information

Users Browsing this Thread

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

Similar Threads

  1. THE big form tutorial ~ by MK
    By MasterKill in forum OSR Advanced Scripting Tutorials
    Replies: 125
    Last Post: 08-04-2013, 07:32 PM
  2. Complete Form Tutorial
    By BobboHobbo in forum OSR Advanced Scripting Tutorials
    Replies: 10
    Last Post: 05-29-2012, 01:36 PM
  3. The Form Tutorial
    By Dan Cardin in forum OSR Advanced Scripting Tutorials
    Replies: 28
    Last Post: 03-03-2011, 04:42 AM
  4. Newbie Form Tutorial
    By Da 0wner in forum OSR Advanced Scripting Tutorials
    Replies: 45
    Last Post: 12-15-2009, 05:48 AM
  5. Ultimate form tutorial
    By jhildy in forum OSR Advanced Scripting Tutorials
    Replies: 9
    Last Post: 02-21-2008, 05:07 PM

Posting Permissions

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