Da 0wner
2008-03-25 19:53:28
Today we are going to learn how to make popup menus.
Too see an example run this
If you look in the var section near top you see this
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
All this stuff
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)
Creates the mainmenu which is the actual menu that pops up.
When you look in the OpenMenu procedure you see this
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.
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.