Tuesday, November 5, 2019

How to Add Items to a TPopUp Delphi Menu

How to Add Items to a TPopUp Delphi Menu When working with Menus or PopUp menus in Delphi applications, in most scenarios, you create the menu items at design-time. Each menu item is represented by a TMenuItem Delphi class. When a user selects (clicks) an item, the OnClick event is fired for you (as a developer) to grab the event and respond to it. There may be situations when the items of the menu are not known at design time, but need to be added at run-time (dynamically instantiated). Add TMenuItem at Run-Time Suppose there is a TPopupMenu component named PopupMenu1 on a Delphi form, to add an item to the popup menu you could write a piece of code as: var   Ã‚   menuItem : TMenuItem; begin   Ã‚  menuItem : TMenuItem.Create(PopupMenu1) ;   Ã‚  menuItem.Caption : Item added at TimeToStr(now) ;   Ã‚  menuItem.OnClick : PopupItemClick;   Ã‚  //assign it a custom integer value..   Ã‚  menuItem.Tag : GetTickCount;   Ã‚  PopupMenu1.Items.Add(menuItem) ; end; Notes In the above code, one item is added to the PopupMenu1 component. Note that we assigned an integer value to the Tag property. The Tag property (every Delphi component has it) is designed to allow a developer to assign an arbitrary integer value stored as part of the component.The GetTickCount API function retrieves the number of milliseconds that have elapsed since Windows was started.For the OnClick event handler, we assigned PopupItemClick - the name of the function with the *correct* signature. procedure TMenuTestForm.PopupItemClick(Sender: TObject) ; var   Ã‚   menuItem : TMenuItem; begin   Ã‚   if NOT (Sender is TMenuItem) then   Ã‚   begin   Ã‚  Ã‚  Ã‚   ShowMessage(Hm, if this was not called by Menu Click, who called this?!) ;   Ã‚  Ã‚  Ã‚   ShowMessage(Sender.ClassName) ;   Ã‚  Ã‚  Ã‚   exit;   Ã‚   end;   Ã‚   menuItem : TMenuItem(sender) ;   Ã‚   ShowMessage(Format(Clicked on %s, TAG value: %d,[menuItem.Name, menuItem.Tag])) ; end; Important When a dynamically added item is clicked, the PopupItemClick will be executed. In order to differentiate between one or more run-time added items (all executing the code in PopupItemClick) we can use the Sender parameter: The PopupItemClick method first checks if the Sender is actually a TMenuItem object. If the method is executed as a result of a menu item OnClick event handler we simply show a dialog message with the Tag value being assigned when the menu item was added to the menu. Custom String-In TMenuItem In real-world applications, you might/would need more flexibility. Lets say that each item will represent a web page - a string value would be required to hold the URL of the web page. When the user selects this item you could open the default web browser and navigate to the URL assigned with the menu item. Heres a custom TMenuItemExtended class equipped with a custom string Value property: type    TMenuItemExtended class(TMenuItem)    private   Ã‚  Ã‚   fValue: string;    published   Ã‚  Ã‚   property Value : string read fValue write fValue;    end; Heres how to add this extended menu item to a PoupMenu1: var   Ã‚   menuItemEx : TMenuItemExtended; begin   Ã‚   menuItemEx : TMenuItemExtended.Create(PopupMenu1) ;   Ã‚   menuItemEx.Caption : Extended added at TimeToStr(now) ;   Ã‚   menuItemEx.OnClick : PopupItemClick;   Ã‚   //assign it a custom integer value..   Ã‚   menuItemEx.Tag : GetTickCount;   Ã‚   //this one can even hold a string value   Ã‚   menuItemEx.Value : http://delphi.about.com;   Ã‚   PopupMenu1.Items.Add(menuItemEx) ; end; Now, the PopupItemClick must be modified to properly process this menu item: procedure TMenuTestForm.PopupItemClick(Sender: TObject) ; var   Ã‚   menuItem : TMenuItem; begin   Ã‚   //...same as above   Ã‚   if sender is TMenuItemExtended then   Ã‚   begin   Ã‚  Ã‚  Ã‚   ShowMessage(Format(Ohoho Extended item .. heres the string value : %s,[TMenuItemExtended(Sender).Value])) ;   Ã‚   end; end; Thats all. Its up to you to extend the TMenuItemExtended as per your needs. Creating custom Delphi components is where to look for help on creating your own classes/components. Note To actually open up the default Web Browser you can use the Value property as a parameter to a ShellExecuteEx API function.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.