PDA

View Full Version : Adding a PopUp Menu



Da 0wner
03-25-2008, 11:53 PM
Today we are going to learn how to make popup menus.

Too see an example run this


program PopupMenuTest;

var
frmDesign: TForm;
MainMenu: TPopupMenu;
Button1: TButton;
Menu1, Menu2, Menu3: TMenuItem;

Procedure OpenMenu(sender: TObject);
Var
x, y: Integer;

Begin
GetMousePos(x, y);
MainMenu.Popup(x, y);
End;

procedure Click1(sender: TObject);
Begin
WriteLn('You clicked the first menu item.');
End;

procedure Click2(sender: TObject);
begin
WriteLn('You clicked the second menu item.');
end;

procedure Click3(sender: TObject);
begin
WriteLn('You clicked the third menu item.');
end;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 156;
frmDesign.Height := 150;
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;

Button1 := TButton.Create(frmDesign);
Button1.Parent := frmDesign;
Button1.Caption := 'Open Popup Menu';
Button1.Left := 15;
Button1.Top := 41;
Button1.Width := 115;
Button1.Height := 25;
Button1.TabOrder := 8;
Button1.OnClick := @OpenMenu;

MainMenu := TPopupMenu.Create(frmDesign);

Menu1 := TMenuItem.Create(frmDesign);
Menu1.Caption := 'Da';
Menu1.OnClick := @Click1;
MainMenu.Items.Add(Menu1);

Menu2 := TMenuItem.Create(frmDesign);
Menu2.Caption := '0wner';
Menu2.OnClick := @Click2;
MainMenu.Items.Add(Menu2);

Menu3 := TMenuItem.Create(frmDesign);
Menu3.Caption := 'Pwnz';
Menu3.OnClick := @Click3;
MainMenu.Items.Add(Menu3);
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 look in the var section near top you see this


MainMenu: TPopupMenu;
Menu1, Menu2, Menu3: TMenuItem;


MainMenu is the actual menu that pops up. Menu1, 2, and 3 are the MenuItems that are in the actual menu ans options. I guess they can be arrays but i don't know, haven't tried (h)

If you look in the initform you see this


MainMenu := TPopupMenu.Create(frmDesign);

Menu1 := TMenuItem.Create(frmDesign);
Menu1.Caption := 'Da';
Menu1.OnClick := @Click1;
MainMenu.Items.Add(Menu1);

Menu2 := TMenuItem.Create(frmDesign);
Menu2.Caption := '0wner';
Menu2.OnClick := @Click2;
MainMenu.Items.Add(Menu2);

Menu3 := TMenuItem.Create(frmDesign);
Menu3.Caption := 'Pwnz';
Menu3.OnClick := @Click3;
MainMenu.Items.Add(Menu3);
end;


All this stuff


Menux := TMenuItem.Create(frmDesign);
Menux.Caption := 'Pwnz';
Menux.OnClick := @Clickx;
MainMenu.Items.Add(Menux);


adds the Menux to the popup menu. It also sets the caption to the Menux.Caption. And when user clicks it it does the Clickx procedure (see top)


MainMenu := TPopupMenu.Create(frmDesign);


Creates the mainmenu which is the actual menu that pops up.

When you look in the OpenMenu procedure you see this

GetMousePos(x, y);
MainMenu.Popup(x, y);


Get mouse pos just gets the coordinates wher the mouse is and stores it in x and y. MainMenu.Popup makes MainMenu (the actual menu) pop up where the mouse is.

That's it. I hope you liked my tutorial. Post any additional questions or comments and i will answer them.

And as always, don't forget to rep me and rate this excellent :D.

skilld u
03-26-2008, 12:05 AM
w00t!! another amazing form tut by you! keep it up.

HayHay89
03-29-2008, 08:42 AM
hmm,
looked over and its the thing i wanted for my new script
for my yew cutter i just release i might add some more stuff to it

-hayhay89

Richard
03-29-2008, 09:56 PM
Wow, you're gonna get the tut writers cup soon. You are probably one of the best people to learn forms off.

Rep++ again.

mixster
03-29-2008, 10:03 PM
Is it just me or are most of his form tut's covering more advanced things very similar to other form tuts in the advanced section...
Edit: Go here (http://www.villavu.com/forum/showthread.php?t=3688) to see what I mean. It's the same tutorial only with different Var names and pop up text and order. Not to mention you do have a few other form tuts that are the same tuts as in the advanced section with only bits changed. Maybe it's just me be paranoid, but I think you've read this tuts and now re-releasing them after changing them to try to get fame. Of course it could be that you've just learnt Delphi and haven't seen those tuts, either way I don't see why we need multiple version of tuts covering the same thing. Beginner tuts etc. all have different methods and stuff like that, but when you get to forms, it's pretty much the same way every time with new things.

Da 0wner
03-29-2008, 10:05 PM
...:p. I like to make tuts that are already made but some people seem to say they are to advanced so i try to make them more easy to understand.. And i cant post in intermediate/advanced though because i have to be srl member or something.

Cazax
03-29-2008, 10:06 PM
@Da Owner: Why dont you just make one tut instead of spliting them away?

@mixter: Truth...

Richard
03-29-2008, 10:09 PM
@Mixter

He can't post anywhere other than beginners and down, so he would have to ask a mod/admin to move.

@Cazax

Think of this like maths, you wouldn't want to learn everything about it at once, you have to learn the different bits seperately

mixster
03-29-2008, 10:13 PM
DR: I meant that his tutorials are very similar in content to tutorials already available, not that they seem to advanced for the beginner section. Checkout my link to Freddies tut on popup menu's for forms to decide yourself.

Richard
03-29-2008, 10:17 PM
Oh, sorry. I'm not very clever when it comes to having a brain...

Da 0wner
03-29-2008, 10:19 PM
Whatever mixster, whereever you go you try to critizize me. :rolleyes: :p. jk, but you have sometimes. I have had no idea of that tutorial and i don't really have my own technique and I just like forms and was learning about many things and so now im just telling public about it. So please don't say i copied it and changed a few things. I wrote this and made all the stuff my self...

mixster
03-29-2008, 10:37 PM
I only said you may have in my opinion. It just happens that you've brought out a lot of form tuts in a couple of days and a few of them resemble other tutorials, so I'm just being my paranoid self. I'm sorry if you feel that I'm criticizing you, it's just that when it comes to tutorials, I've read of most them and when reading yours it reminded me of one I previously read, so I followed the breadcrumbs that led me to Freddies tut (and I also noticed both of you used 3 menu vars) and I just posted the link to get a few other opinions on it and also just to let you know that there are other tut's similar to yours so that you could maybe expand a bit to try to cover what they didn't to make it more useful to the community.

Da 0wner
03-29-2008, 10:39 PM
Alright, sorry before i publish a tutorial i'll look for other similiar ones. truce?

mixster
03-29-2008, 10:46 PM
I didn't know we had a war :)
Though as long as you cover something they don't, I don't mind it being similar.

Da 0wner
03-29-2008, 10:53 PM
k, we didn't just wanted us to be more friendly then we were. :)