主页:http://www.maxss.net
邮件:[email protected]
TMenuItem *mnuItem = new TMenuItem(pMenu); // Create new item
pMenu->Items->Add(mnuItem); // Add it to pMenu
mnuItem->Name = "CM_" + IntToStr(tag);
mnuItem->Caption = caption;
mnuItem->ImageIndex = imgIndex;
mnuItem->Tag = tag;
mnuItem->OnClick = notifyEvent; // Assign it an event handler
然后编写调用AddContextMenuItem(...)的函数CreateContextMenu(),定义如下:
void __fastcall TForm1::CreateContextMenu(void)
{
FTagInc = 0;
FContextMenu = new TPopupMenu(this);
FContextMenu->AutoHotkeys = maManual;
FContextMenu->OnPopup = UpdateContextMenuItem;
AddContextMenuItem(FContextMenu, "菜单项 1", FTagInc++, MenuItemClick);
AddContextMenuItem(FContextMenu, "菜单项 2", FTagInc++, MenuItemClick);
AddContextMenuItem(FContextMenu, "-", FTagInc++, NullNotifyEvent);
AddContextMenuItem(FContextMenu, "菜单项 3", FTagInc++, MenuItemClick);
}
FContextMenu是TForm1的成员变量,UpdateContextMenuItem()是用于在FContextMenu的Popup事件中更新菜单项。
MenuItemClick与NullNotifyEvent都是TForm1中成员函数,是动态创建的控件的事件处理函数,格式为:
void __fastcall TForm1::MenuItemClick(TObject *Sender)
void __fastcall TForm1::NullNotifyEvent(TObject *Sender)
上面是我自己的一点心得,希望可以与大学继续交流程序设计的经验。
本文地址:http://com.8s8s.com/it/it1530.htm