Board index » delphi » D3 menus

D3 menus

Quick question if anyone can help me with this one.

I am trying to find a way to generate menus in a Delphi app at runtime.
I need to be able to create menu options, submenus, etc. I can't find
anything useful in the help - what it tells me to do gives me compile
errors.

I hope I'm not being overly ambitious - if anyone can help I'd be really
really grateful!

Thanks

--
Return address removed for anti-spam purposes.
Email replies to news at maelstrom dot demon dot co dot uk

 

Re:D3 menus


There is nothing hard:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, Menus;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure MenuItemClick(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.MenuItemClick;
begin
  MessageDlg('Hello! I am dynamically created
item!',mtInformation,[mbOk],0);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  MI: TMenuItem;
begin
  MI:= TMenuItem.Create(Application);
  MI.Caption:='Test dynamically creation';
  MI.OnClick:=MenuItemClick;
  File1.Add(MI);
{ each menu item has list of subitems. If this list is empty Windows thinks
that it is a command otherwise it is submenu. So to create submenu(s) of the
item runtime created do something like this:
  MI.Add(New created item) }
end;

end.

Black Hole D??? ???Y?? <48hjCCAbAhN2E...@maelstrom.demon.co.uk> ...

Quote

>Quick question if anyone can help me with this one.

>I am trying to find a way to generate menus in a Delphi app at runtime.
>I need to be able to create menu options, submenus, etc. I can't find
>anything useful in the help - what it tells me to do gives me compile
>errors.

>I hope I'm not being overly ambitious - if anyone can help I'd be really
>really grateful!

>Thanks

>--
>Return address removed for anti-spam purposes.
>Email replies to news at maelstrom dot demon dot co dot uk

Re:D3 menus


In article <48hjCCAbAhN2E...@maelstrom.demon.co.uk>, Black Hole

Quote
<Newsmas...@Black.Hole> writes:
>I am trying to find a way to generate menus in a Delphi app at runtime.
>I need to be able to create menu options, submenus, etc. I can't find
>anything useful in the help - what it tells me to do gives me compile
>errors.

Remember that the TMainMenu is a holding object for the TMenuItems which are
the items in that main menu (ie showing at the top of the form). the sub-items
to the main menu items are also TMenuItems, and so ad infinitum <g>. The menu
item which the TMenuItems attach are nominated as the owner when creating. You
then have to Add or Insert the created TMenuItem into the parent (or main menu)
item. However you use TMainMenu.Items.Add() or TMainMenu.Items.Insert() to add
the main menu items, and TMenuItem.Add or TMenuItem.Insert for the sub menu
items.

Demo program sent to questioner separately.

Alan Lloyd
alangll...@aol.com

Alan Lloyd
alangll...@aol.com

Re:D3 menus


In article <48hjCCAbAhN2E...@maelstrom.demon.co.uk>, Black Hole

Quote
<Newsmas...@Black.Hole> writes:

>I am trying to find a way to generate menus in a Delphi app at runtime.
>I need to be able to create menu options, submenus, etc. I can't find
>anything useful in the help - what it tells me to do gives me compile
>errors

Try this
uses menus.
<snip>
public
var
Menu1:TMainMenu;
<snip>
Procedure Form1.OnCreate(Sender:TObject)
var
MenuList:TStringList;
i,MenuBarCount:integer;
begin
Menu1:=TMainMenu.create(Form1);
MenuList:=TStringList.Create;
{Fill the list with your menubar captions}
For i:=0 to MenuList.items.count-1 do
MakeMenu(i,MenuList.items[i],Menu1.items);
{Fill the list with the captions for the first menu}
MenuBarCount:=20;{Ensures that each item has a unique tag value}
For i:=0 to MenuList.items.count-1 do
MakeMenu(MenuBarCount+i,MenuList.items[i],Menu1.items[0]);
MenuBarCount:=40:{Ensures that each item has a unique tag value}
etc etc
Form1.Menu:=Menu1;
end;

Procedure MakeMenu(CapIndex:integer;CapString:string;MyMenu:TMenuItem);
var
NewItem:TMenuItem;
Begin
 NewItem := TMenuItem.Create(Menu1); {Creates the new menu item}
 NewItem.Caption := CapString;  {Caption for the new menu item}
 NewItem.Tag:=CapIndex;
 NewItem.OnClick:=OwnMenuClick;
 MyMenu.Add(NewItem);   {Adds the new menu item}
end;

Procedure OwnMenuClick(Sender:TObject);
begin
If Sender is TMenuItem then begin
Case (Sender as TMenuItem).Tag of
{procedures for each menu item}

popup menus and nested menus possible in same way, if you need more e-mail me!

regards
John Parsons

Cray Chicken: Crosses faster than any other chicken, but if you don't dip it
in liquid nitrogen first, it arrives on the other side fully cooked!

Other Threads