Board index » delphi » Sub menu items in popup

Sub menu items in popup

I am trying to iterate through a list of lines and create a popup menu
structure that has submenus in it. But, I cannot figure out how to
create submenus and add items to those submenus.
If anyone can show me an example or where to find the information I
would be thankful.
To help better explain what I mean, below is a list of what I am trying
to do. The '.' is discriptive of a submenu.

Item1                       - Item
Item2                       - Item
Menu1.                      - Menu
Menu1.SubItem1              - Item in Menu
Menu1.SubItem2              - Item in Menu
Menu1.SubMenu1.             - SubMenu in Menu
Menu1.SubMenu1.SubMenuItem1 - Item in the SubMenu
Menu2.                      - Menu
Menu2.SubItem1              - Item in Menu
Item3                       - Item

Thanks in advance.

 

Re:Sub menu items in popup


Quote
On Mon, 16 Feb 1998 18:15:37 -0600, zolic <zo...@flash.net> wrote:
> But, I cannot figure out how to create submenus and add items
> to those submenus.

Use the following helper functions which are declared in the Menus
unit:

function NewMenu(Owner: TComponent; const AName: string; Items: array
of TMenuItem): TMainMenu;

function NewPopupMenu(Owner: TComponent; const AName: string;
  Alignment: TPopupAlignment; AutoPopup: Boolean;
  Items: array of TMenuitem): TPopupMenu;

function NewSubMenu(const ACaption: string; hCtx: Word;
  const AName: string;  Items: array of TMenuItem): TMenuItem;

function NewItem(const ACaption: string; AShortCut: TShortCut;
  AChecked, AEnabled: Boolean; AOnClick: TNotifyEvent; hCtx: Word;
  const AName: string): TMenuItem;

function NewLine: TMenuItem;

Here's an example of how to use these functions:

unit Unit1;

interface

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

type

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    procedure ClickHandler(Sender: TObject);
    procedure OtherClickHandler(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  Popup : TPopupMenu;
  SubMenu: TMenuItem;
  Item1 : TMenuItem;
  Item2 : TMenuItem;
begin
  Item1 := NewItem('Caption1', 0, False, True, ClickHandler, 0,
'Item1');
  Item2 := NewItem('Caption2', 0, False, True, OtherClickHandler, 0,
'Item2');
  SubMenu := NewSubMenu('SubItem1', 0, 'SubItem1', [Item2]);
  Popup := NewPopupMenu(Self, 'PopupMenu1', paLeft, True, [Item1,
SubMenu]);
  Self.PopupMenu := Popup;
end;

procedure TForm1.ClickHandler(Sender: TObject);
begin
  ShowMessage('Clicked');
end;

procedure TForm1.OtherClickHandler(Sender: TObject);
begin
  ShowMessage('Ouch. Stop clicking me.');
end;

end.

--
Rick Rogers (TeamB) | Fenestra Technologies

Re:Sub menu items in popup


Quote
Rick Rogers (TeamB) wrote:

> On Mon, 16 Feb 1998 18:15:37 -0600, zolic <zo...@flash.net> wrote:

> > But, I cannot figure out how to create submenus and add items
> > to those submenus.

> Use the following helper functions which are declared in the Menus
> unit:

> function NewMenu(Owner: TComponent; const AName: string; Items: array
> of TMenuItem): TMainMenu;

> function NewPopupMenu(Owner: TComponent; const AName: string;
>   Alignment: TPopupAlignment; AutoPopup: Boolean;
>   Items: array of TMenuitem): TPopupMenu;

> function NewSubMenu(const ACaption: string; hCtx: Word;
>   const AName: string;  Items: array of TMenuItem): TMenuItem;

> function NewItem(const ACaption: string; AShortCut: TShortCut;
>   AChecked, AEnabled: Boolean; AOnClick: TNotifyEvent; hCtx: Word;
>   const AName: string): TMenuItem;

> function NewLine: TMenuItem;

> Here's an example of how to use these functions:

> unit Unit1;

> interface

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

> type

>   TForm1 = class(TForm)
>     procedure FormCreate(Sender: TObject);
>   private
>     { Private declarations }
>   public
>     procedure ClickHandler(Sender: TObject);
>     procedure OtherClickHandler(Sender: TObject);
>   end;

> var
>   Form1: TForm1;

> implementation

> {$R *.DFM}

> procedure TForm1.FormCreate(Sender: TObject);
> var
>   Popup : TPopupMenu;
>   SubMenu: TMenuItem;
>   Item1 : TMenuItem;
>   Item2 : TMenuItem;
> begin
>   Item1 := NewItem('Caption1', 0, False, True, ClickHandler, 0,
> 'Item1');
>   Item2 := NewItem('Caption2', 0, False, True, OtherClickHandler, 0,
> 'Item2');
>   SubMenu := NewSubMenu('SubItem1', 0, 'SubItem1', [Item2]);
>   Popup := NewPopupMenu(Self, 'PopupMenu1', paLeft, True, [Item1,
> SubMenu]);
>   Self.PopupMenu := Popup;
> end;

> procedure TForm1.ClickHandler(Sender: TObject);
> begin
>   ShowMessage('Clicked');
> end;

> procedure TForm1.OtherClickHandler(Sender: TObject);
> begin
>   ShowMessage('Ouch. Stop clicking me.');
> end;

> end.

> --
> Rick Rogers (TeamB) | Fenestra Technologies

One of these days, I may actually read the source code files. Thanks.

Other Threads