PDA

View Full Version : Form TPopupMenu tutorial



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

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.
http://stuart.existhost.com/freddy/tutimages/menu5.PNG

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.
http://stuart.existhost.com/freddy/tutimages/menu4.PNG

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
11-20-2006, 10:13 PM
Damn Freddy, tutorial diarrhea. Another cool tut.

Janilabo
11-20-2006, 11:14 PM
Freddy is going crazy with tutorials right now. ;d

Freddy1990
11-20-2006, 11:34 PM
Freddy is going crazy with tutorials right now. ;d

Tomorrow 1 more and then i'm finished :)

Rick
11-22-2006, 12:42 AM
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
11-22-2006, 01:41 PM
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
02-23-2007, 11:23 PM
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
02-24-2007, 12:10 AM
: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
02-24-2007, 12:19 AM
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
03-27-2007, 11:56 PM
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
04-02-2007, 06:07 AM
Wow i learned it!
Something for clarion v0.7 :)

tom99
12-14-2009, 12:08 PM
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.

Zyt3x
12-14-2009, 12:59 PM
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
12-14-2009, 07:25 PM
tutorials like this never go out of date. Its not like anything has changed.