动态地创建菜单需要做以下工作:
• 创建子菜单。
• 为每个子菜单创建菜单项。
• 设置菜单项的属性。
• 定义菜单命令的事件处理过程。
例程S4_2 该例程不仅对以上内容进行了说明,而且还包含了以下内容:
• 在菜单中设置竖向分隔条。
• 通过按钮激活弹出式菜单。
具体实现过程如下:
1) 通过菜单File | New Application 创建一个新的工程。
2) 在窗体中添加三个B u t t o n 组件,C a p t i o n 属性分别设置为“创建主菜单( & M )”、“创建弹
出式菜单( & P )”和“显示弹出式菜单( & S )”。
3) 在U s e s 语句中增加下面的代码:
. . .
u s e s
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls,Menus;
. . .
4) 在U n i t 1 . p a s 文件中定义下面的变量:
. . .
v a r
Form1: TForm1;
MyMainMenu: TMainMenu;
MyPopUpMenu: TPopUpMenu;
MySubItems: array[0..3] of TMenuItem;
MyPopUpItems: array[0..3] of TMenuItem;
i: Integer;
i m p l e m e n t a t i o n
. . .
5) 定义菜单命令的事件处理过程。
首先在U n i t 1 . p a s 文件的T F o r m 1 的定义代码中添加如下代码:
t y p e
TForm1 = class(TForm)
. . .
// 定义菜单命令的事件处理过程
procedure MyPopupHandler(Sender: TObject);
p r i v a t e
. . .
然后手工在U n i t 1 . p a s 文件的最后一条e n d 语句的前面定义过程体如下:
// 菜单命令的事件处理过程
procedure TForm1.MyPopupHandler(Sender: TO b j e c t ) ;
b e g i n
with Sender as TMenuItem do
b e g i n
// 在信息对话框中显示选中菜单项的C a p t i o n 属性
ShowMessage(Caption);
e n d ;
e n d ;
6) 添加B u t t o n 1 组件、B u t t o n 2 组件和B u t t o n 3 组件的O n C l i c k 事件的处理过程如下:
procedure TForm1.Button1Click(Sender: TO b j e c t ) ;
v a r
MyItem: array[0..2] of TMenuItem;
b e g i n
MyMainMenu:= TMainMenu.Create(Self);
// 创建三个子菜单
for i := 0 to 2 do begin
MyItem[i] := TMenuItem.Create(Self);
MyItem[i].Caption := '子菜单' + I n t To S t r ( i ) + ' ( & ' + I n t To S t r ( i ) + ' ) ' ;
M y M a i n M e n u . I t e m s . A d d ( M y I t e m [ i ] ) ;
e n d ;
for i := 0 to 3 do begin
MySubItems[i] := TMenuItem.Create(Self);
MySubItems[i].Caption := '主菜单项' + I n t To S t r ( i ) + ' ( & ' + I n t To S t r ( i + 1 ) + ' ) ' ;
M y M a i n M e n u . I t e m s [ 0 ] . A d d ( M y S u b I t e m s [ i ] ) ;
// 指定菜单项的O n C l i c k 事件的处理过程
MySubItems[i].OnClick := MyPopUpHandler;
e n d ;
// 将第2 个菜单项设置为分隔条
MySubItems[1].Caption :='-';
// 设置竖向分隔条
MySubItems[3].Break := mbBarBreak;
e n d ;
procedure TForm1.Button2Click(Sender: TO b j e c t ) ;
b e g i n
MyPopUpMenu := TPopUpMenu.Create(self);
for i := 0 to 3 do
b e g i n
MyPopUpItems[i] := TMenuItem.Create(Self);
// 设置菜单项的C a p t i o n 属性
MyPopUpItems[i].Caption := '弹出菜单项' + I n t ToStr(i)+ '(&'+ IntToStr(i+1)+ ')';
M y P o p U p M e n u . I t e m s . A d d ( M y P o p U p I t e m s [ i ] ) ;
// 指定菜单项的O n C l i c k 事件的处理过程
MyPopUpItems[i].OnClick := MyPopupHandler;
e n d ;
e n d ;
procedure TForm1.Button3Click(Sender: TO b j e c t ) ;
b e g i n
// 通过按钮激活弹出式菜单
MyPopUpMenu.Popup(Form1.Left + 60, Form1.Top + 60);
e n d ;抄的,看一看吧,
• 创建子菜单。
• 为每个子菜单创建菜单项。
• 设置菜单项的属性。
• 定义菜单命令的事件处理过程。
例程S4_2 该例程不仅对以上内容进行了说明,而且还包含了以下内容:
• 在菜单中设置竖向分隔条。
• 通过按钮激活弹出式菜单。
具体实现过程如下:
1) 通过菜单File | New Application 创建一个新的工程。
2) 在窗体中添加三个B u t t o n 组件,C a p t i o n 属性分别设置为“创建主菜单( & M )”、“创建弹
出式菜单( & P )”和“显示弹出式菜单( & S )”。
3) 在U s e s 语句中增加下面的代码:
. . .
u s e s
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls,Menus;
. . .
4) 在U n i t 1 . p a s 文件中定义下面的变量:
. . .
v a r
Form1: TForm1;
MyMainMenu: TMainMenu;
MyPopUpMenu: TPopUpMenu;
MySubItems: array[0..3] of TMenuItem;
MyPopUpItems: array[0..3] of TMenuItem;
i: Integer;
i m p l e m e n t a t i o n
. . .
5) 定义菜单命令的事件处理过程。
首先在U n i t 1 . p a s 文件的T F o r m 1 的定义代码中添加如下代码:
t y p e
TForm1 = class(TForm)
. . .
// 定义菜单命令的事件处理过程
procedure MyPopupHandler(Sender: TObject);
p r i v a t e
. . .
然后手工在U n i t 1 . p a s 文件的最后一条e n d 语句的前面定义过程体如下:
// 菜单命令的事件处理过程
procedure TForm1.MyPopupHandler(Sender: TO b j e c t ) ;
b e g i n
with Sender as TMenuItem do
b e g i n
// 在信息对话框中显示选中菜单项的C a p t i o n 属性
ShowMessage(Caption);
e n d ;
e n d ;
6) 添加B u t t o n 1 组件、B u t t o n 2 组件和B u t t o n 3 组件的O n C l i c k 事件的处理过程如下:
procedure TForm1.Button1Click(Sender: TO b j e c t ) ;
v a r
MyItem: array[0..2] of TMenuItem;
b e g i n
MyMainMenu:= TMainMenu.Create(Self);
// 创建三个子菜单
for i := 0 to 2 do begin
MyItem[i] := TMenuItem.Create(Self);
MyItem[i].Caption := '子菜单' + I n t To S t r ( i ) + ' ( & ' + I n t To S t r ( i ) + ' ) ' ;
M y M a i n M e n u . I t e m s . A d d ( M y I t e m [ i ] ) ;
e n d ;
for i := 0 to 3 do begin
MySubItems[i] := TMenuItem.Create(Self);
MySubItems[i].Caption := '主菜单项' + I n t To S t r ( i ) + ' ( & ' + I n t To S t r ( i + 1 ) + ' ) ' ;
M y M a i n M e n u . I t e m s [ 0 ] . A d d ( M y S u b I t e m s [ i ] ) ;
// 指定菜单项的O n C l i c k 事件的处理过程
MySubItems[i].OnClick := MyPopUpHandler;
e n d ;
// 将第2 个菜单项设置为分隔条
MySubItems[1].Caption :='-';
// 设置竖向分隔条
MySubItems[3].Break := mbBarBreak;
e n d ;
procedure TForm1.Button2Click(Sender: TO b j e c t ) ;
b e g i n
MyPopUpMenu := TPopUpMenu.Create(self);
for i := 0 to 3 do
b e g i n
MyPopUpItems[i] := TMenuItem.Create(Self);
// 设置菜单项的C a p t i o n 属性
MyPopUpItems[i].Caption := '弹出菜单项' + I n t ToStr(i)+ '(&'+ IntToStr(i+1)+ ')';
M y P o p U p M e n u . I t e m s . A d d ( M y P o p U p I t e m s [ i ] ) ;
// 指定菜单项的O n C l i c k 事件的处理过程
MyPopUpItems[i].OnClick := MyPopupHandler;
e n d ;
e n d ;
procedure TForm1.Button3Click(Sender: TO b j e c t ) ;
b e g i n
// 通过按钮激活弹出式菜单
MyPopUpMenu.Popup(Form1.Left + 60, Form1.Top + 60);
e n d ;抄的,看一看吧,
解决方案 »
- 怎样处理空值
- 有關CXGRID新增記錄的問題
- 在XP下如何隐藏进程
- 提示no active mdi form curuntly actived 什么意思呀?
- 在线等!能否告知一个能真正下载Delphi2005的网址!!!!
- 我想用realplay 10 做一个网络电视,但是在DELPHI里引用不了~~(在线等~分不够可以加)
- 关于ExpressBar中dxSideBar的使用问题
- 非常感激WnEunfn,请来领昨天晚上的800分!-2
- 关于使用控件的问题?
- Delphi6的一些消息
- 请问分页控件是什么名字,假如我需要分5页,这个5怎么设置?谢谢!!!
- 今天刚看delphi,请问procedure TForm1.Button1Click(Sender: TObject);这个是传值还是传参数的???
作 者:zzd
所属论坛:Delphi
问题点数:20
回复次数:5
发表时间:2001-11-30 23:05:48
我在程序中使用动态菜单,即根据运行状态改变菜单的内容和结构,但我不会编写菜单项的处理程序,如何才能使某一过程作为菜单的处理程序?谢了。
回复贴子:
回复人: liang_z(千山一刀之忍者神龟) (2001-11-30 23:14:33) 得10分
procedure TFMain.BuildPopMenu;
begin
MainPopMenu.Items.Clear;
AddPopMenu(pShareData.aPopedomMenu,0,MainPopMenu.Items);
end;procedure TFMain.AddPopMenu(aMenu:array of ST_POPEDOM;ParentID:Integer;PMenu:TMenuItem);
var
i : Integer;
myMenu : TMenuItem;
begin
Application.ProcessMessages;
for i:=low(aMenu) to High(aMenu) do
begin
if (aMenu[i].iParentID=ParentID)and(aMenu[i].bShow) then
begin
myMenu := TMenuItem.Create(self);
myMenu.Caption := aMenu[i].sName;
myMenu.Tag := aMenu[i].iPopedomID;
myMenu.Hint := aMenu[i].sMemo;
myMenu.OnClick := PopMenuClick;//你的关键是这一句!!!!!!!!!!!
PMenu.Insert(myMenu.Count,myMenu);
AddPopMenu(aMenu,aMenu[i].iPopedomID,myMenu);
end;
end;
end;
//主功能菜单事件
procedure TFMain.PopMenuClick(Sender: TObject);
begin
Case TMenuItem(Sender).Tag of
1999 :
begin
pPrivateData.pConnect :=MainData.MainDataModule.myConnection;
ow_Operator(pConfig,pShareData,pPrivateData);
end;
else
end;
end;
回复人: zswangII(伴水II)(伤心中)(副本) (2001-11-30 23:22:30) 得10分
//你自己研究
//pas
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus, ActnList;type
TForm1 = class(TForm)
ActionList1: TActionList;
ActionAbout: TAction;
ActionHelp: TAction;
ActionExit: TAction;
MainMenu1: TMainMenu;
Button1: TButton;
MenuItemFileM: TMenuItem;
MenuItemHelpM: TMenuItem;
MenuItemExit: TMenuItem;
MenuItemHelp: TMenuItem;
MenuItemAbout: TMenuItem;
Button2: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure ActionAboutExecute(Sender: TObject);
procedure ActionHelpExecute(Sender: TObject);
procedure ActionExitExecute(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function StringToComponent(Value: string; Instance: TComponent): TComponent;
var
StrStream: TStringStream;
BinStream: TMemoryStream;
begin
StrStream := TStringStream.Create(Value);
try
BinStream := TMemoryStream.Create;
try
ObjectTextToBinary(StrStream, BinStream);
BinStream.Seek(0, soFromBeginning);
Result := BinStream.ReadComponent(Instance);
finally
BinStream.Free;
end;
finally
StrStream.Free;
end;
end; { StringToComponent }procedure TForm1.ActionAboutExecute(Sender: TObject);
begin
ShowMessage('一个范例');
end;procedure TForm1.ActionHelpExecute(Sender: TObject);
begin
ShowMessage('自己帮助自己才能成功');
end;procedure TForm1.ActionExitExecute(Sender: TObject);
begin
Close;
end;procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
vComponent: TComponent;
S: string;
begin
MainMenu1.Items.Clear;
StringToComponent(Memo1.Text, MainMenu1);
for I := 0 to ActionList1.ActionCount - 1 do begin
S := ActionList1.Actions[I].Name;
Delete(S, 1, 6);
vComponent := MainMenu1.FindComponent('MenuItem' + S);
if Assigned(vComponent) then
TMenuItem(vComponent).Action := ActionList1.Actions[I];
end;
end;procedure TForm1.Button2Click(Sender: TObject);
var
I: Integer;
vComponent: TComponent;
S: string;
begin
MainMenu1.Items.Clear;
StringToComponent(Memo2.Text, MainMenu1);
for I := 0 to ActionList1.ActionCount - 1 do begin
S := ActionList1.Actions[I].Name;
Delete(S, 1, 6);
vComponent := MainMenu1.FindComponent('MenuItem' + S);
if Assigned(vComponent) then
TMenuItem(vComponent).Action := ActionList1.Actions[I];
end;
end;end.//dfm
object Form1: TForm1
Left = 192
Top = 107
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
Menu = MainMenu1
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 56
Top = 192
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 304
Top = 184
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 1
OnClick = Button2Click
end
object Memo1: TMemo
Left = 8
Top = 8
Width = 265
Height = 145
Lines.Strings = (
'object MainMenu1: TMainMenu'
' Left = 48'
' Top = 24'
' object MenuItemFileM: TMenuItem'
' Caption = #25991#20214'
' object MenuItemExit: TMenuItem'
' Caption = #36864#20986'
' end'
' end'
' object MenuItemHelpM: TMenuItem'
' Caption = #24110#21161'
' object MenuItemHelp: TMenuItem'
' Caption = #24110#21161'
' end'
' object MenuItemAbout: TMenuItem'
' Caption = #20851#20110'
' end'
' end'
'end')
TabOrder = 2
end
object Memo2: TMemo
Left = 280
Top = 8
Width = 233
Height = 145
Lines.Strings = (
'object MainMenu1: TMainMenu'
' Left = 48'
' Top = 24'
' object MenuItemFileM: TMenuItem'
' Caption = #25991#20214'
' object MenuItemHelpM: TMenuItem'
' Caption = #24110#21161'
' object MenuItemHelp: TMenuItem'
' Caption = #24110#21161'
' end'
' object MenuItemAbout: TMenuItem'
' Caption = #20851#20110'
' end'
' end'
' object MenuItemExit: TMenuItem'
' Caption = #36864#20986'
' end'
' end'
'end')
TabOrder = 3
end
object ActionList1: TActionList
Left = 16
Top = 24
object ActionAbout: TAction
Caption = '关于'
OnExecute = ActionAboutExecute
end
object ActionHelp: TAction
Caption = '帮助'
OnExecute = ActionHelpExecute
end
object ActionExit: TAction
Caption = '退出'
OnExecute = ActionExitExecute
end
end
object MainMenu1: TMainMenu
Left = 48
Top = 24
end
end
回复人: Bellamy(酷公爵) (2001-12-1 0:02:33) 得0分
上面两位,不错,我就不贴了!
回复人: zzd(laoben) (2001-12-1 11:02:19) 得0分
谢过了
回复人: zzd(laoben) (2001-12-1 11:04:14) 得0分
给分
cxzc
dsadsa
fdsf
cxzc
cxzc
fdsf
fds
fdsf
我的treeivew1如上内容;
现在我想动态创建一个菜单(三级),如何做?
--------- cxzc
dsadsa
fdsf
--------- cxzc
------------cxzc
fdsf
fds
fdsf
我的treeivew1如上内容;
现在我想动态创建一个菜单(三级),如何做?