请考虑这样的场景:
我有一个名为的组件TMenuItemSelector
,它有两个已发布的属性:PopupMenu
- 允许从表单中选择一个实例,TPopupMenu
并MenuItem
允许从表单中选择任何实例TMenuItem
。
我想修改属性的属性编辑器,以便MenuItem
在PopupMenu
分配时只有菜单项PopupMenu
在下拉列表中可见。
我知道我需要编写自己的后代TComponentProperty
和覆盖GetValues
方法。问题是我不知道如何访问其所在的表单TMenuItemSelector
。
OriginalTComponentProperty
使用此方法迭代所有可用实例:
procedure TComponentProperty.GetValues(Proc: TGetStrProc);
begin
Designer.GetComponentNames(GetTypeData(GetPropType), Proc);
end;
但是,Designer
似乎是预编译的,所以我不知道如何GetComponentNames
工作。
这就是我到目前为止所拥有的,我想我唯一缺少的是实现GetValues
:
unit uMenuItemSelector;
interface
uses
Classes, Menus, DesignIntf, DesignEditors;
type
TMenuItemSelector = class(TComponent)
private
FPopupMenu: TPopUpMenu;
FMenuItem: TMenuItem;
procedure SetPopupMenu(const Value: TPopUpMenu);
procedure SetMenuItem(const Value: TMenuItem);
published
property PopupMenu: TPopUpMenu read FPopupMenu write SetPopupMenu;
property MenuItem: TMenuItem read FMenuItem write SetMenuItem;
end;
type
TMenuItemProp = class(TComponentProperty)
public
function GetAttributes: TPropertyAttributes; override;
procedure GetValues(Proc: TGetStrProc); override;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterPropertyEditor(TypeInfo(TMenuItem), TMenuItemSelector, 'MenuItem', TMenuItemProp);
RegisterComponents('Test', [TMenuItemSelector]);
end;
{ TMenuItemSelector }
procedure TMenuItemSelector.SetMenuItem(const Value: TMenuItem);
begin
FMenuItem := Value;
end;
procedure TMenuItemSelector.SetPopupMenu(const Value: TPopUpMenu);
begin
FPopupMenu := Value;
end;
{ TMenuItemProperty }
function TMenuItemProp.GetAttributes: TPropertyAttributes;
begin
Result := inherited GetAttributes + [paValueList, paSortList];
end;
procedure TMenuItemProp.GetValues(Proc: TGetStrProc);
begin
//How to filter MenuItems from the form in a way that only
//MenuItems which belong to TMenuItemSelector.PopupMenu are displayed? \
//And how to get to that form?
//inherited;
end;
end.
任何人都可以帮忙吗?
谢谢。