delphi 菜单字体_将项目添加到TPopUp Delphi菜单

delphi 菜单字体

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.

在大多数情况下,在Delphi应用程序中使用“菜单”或“弹出菜单”时,您可以在设计时创建菜单项。 每个菜单项都由TMenuItem Delphi类表示。 当用户选择(单击)一个项目时,将为您(作为开发人员)触发OnClick事件,以获取事件并做出响应。

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).

在某些情况下,菜单的项目在设计时未知,但是需要在运行时添加( 动态实例化 )。

在运行时添加TMenuItem ( 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:

假设在Delphi窗体上有一个名为“ PopupMenu1”的TPopupMenu组件,要将一个项目添加到弹出菜单中,您可以编写如下代码:

 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.

    在上面的代码中,一项添加到PopupMenu1组件。 请注意,我们为Tag属性分配了一个整数值。 Tag属性(每个Delphi组件都具有)被设计为允许开发人员分配存储为组件一部分的任意整数值。

  • The GetTickCount API function retrieves the number of milliseconds that have elapsed since Windows was started.

    GetTickCount API函数检索自Windows启动以来经过的毫秒数。

  • For the OnClick event handler, we assigned "PopupItemClick" - the name of the function with the *correct* signature.

    对于OnClick事件处理程序,我们分配了“ PopupItemClick”-具有* correct *签名的函数名称。
 procedure TMenuTestForm.PopupItemClick(Sender: TObject) ;var
   menuItem : TMenuItem;beginif NOT (Sender is TMenuItem) thenbegin
     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:

    单击动态添加的项目时,将执行“ PopupItemClick”。 为了区分一个或多个运行时添加的项(全部在PopupItemClick中执行代码),我们可以使用Sender参数:

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.

“ PopupItemClick”方法首先检查发件人是否实际上是TMenuItem对象。 如果该方法是通过菜单项OnClick事件处理程序执行的,则仅显示一条对话框消息,当将菜单项添加到菜单时便会分配Tag值。

自定义字符串输入TMenuItem ( Custom String-In TMenuItem )

In real-world applications, you might/would need more flexibility. Let's 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.

在实际的应用程序中,您可能/将需要更大的灵活性。 假设每个项目都会“代表”一个网页-需要一个字符串值来保存网页的URL。 当用户选择此项目时,您可以打开默认的Web浏览器并导航到菜单项分配的URL。

Here's a custom TMenuItemExtended class equipped with a custom string "Value" property:

这是一个自定义TMenuItemExtended类,配备了自定义字符串“ Value”属性:

 type
  TMenuItemExtended = class(TMenuItem)private
    fValue: string;publishedproperty Value : string read fValue write fValue;end;

Here's how to add this "extended" menu item to a PoupMenu1:

这是将这个“扩展”菜单项添加到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:

现在,必须修改“ PopupItemClick”以正确处理此菜单项:

 procedure TMenuTestForm.PopupItemClick(Sender: TObject) ;var
   menuItem : TMenuItem;
begin//...same as above if sender is TMenuItemExtended thenbegin
     ShowMessage(Format('Ohoho Extended item .. here''s the string value : %s',[TMenuItemExtended(Sender).Value])) ;end;end;

That's all. It's 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.

就这样。 您可以根据需要扩展TMenuItemExtended。 创建自定义的Delphi组件是在创建自己的类/组件时寻求帮助的地方。

注意 ( Note)

To actually open up the default Web Browser you can use the Value property as a parameter to a ShellExecuteEx API function.

要实际打开默认的Web浏览器,可以将Value属性用作ShellExecuteEx API函数的参数。

翻译自: https://www.thoughtco.com/dynamically-add-items-tpopup-menu-1058152

delphi 菜单字体

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值