SRL Archive - OSR Advanced Scripting Tutorials

Form TPopupMenu tutorial


Freddy1990

2006-11-20 16:42:49

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:
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:
btn: TButton;

Code for the initform procedure:
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:
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:
mnu: TPopupMenu;

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

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

Now we do the same for all three menu items:
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]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:
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:
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.
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:
btn.OnClick := @ButtonClick;

So now we have the following script:
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:
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;

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

This leaves us with the following script:
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.

Boreas

2006-11-20 17:13:44

Damn Freddy, tutorial diarrhea. Another cool tut.

Janilabo

2006-11-20 18:14:19

Freddy is going crazy with tutorials right now. ;d

Freddy1990

2006-11-20 18:34:57

From: LaBo
Freddy is going crazy with tutorials right now. ;d


Tomorrow 1 more and then i'm finished :)

Rick

2006-11-21 19:42:18

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

Freddy1990

2006-11-22 08:41:51

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

JAD

2007-02-23 18:23:04

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 :)

Smartzkid

2007-02-23 19:10:49

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

Maybe a tut on making two forms in one script? :D I was trying to do it today, but I finally gave up, and just made a Tgroup thing that would change visibility.

botmaster

2007-02-23 19:19:12

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

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

SKy Scripter

2007-03-27 19:56:58

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!!!!

n3ss3s

2007-04-02 02:07:52

Wow i learned it!
Something for clarion v0.7 :)

tom99

2009-12-14 07:08:56

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.

Gu3st...

2009-12-14 07:59:04

From: tom99
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

Dan Cardin

2009-12-14 14:25:32

tutorials like this never go out of date. Its not like anything has changed.