請考慮這樣之情況:如何修改TComponentProperty以僅顯示下拉列表中的特定項目?
我有分量叫TMenuItemSelector
它有兩個公佈屬性:PopupMenu
- 允許挑選的TPopupMenu
從形式和MenuItem
一個實例,它允許從形式挑選的TMenuItem
任何實例。
我想修改MenuItem
屬性的屬性編輯器,當PopupMenu
被分配時,只有這個PopupMenu
的菜單項在下拉列表中可見。
我知道我需要寫我自己的TComponentProperty
的後代並覆蓋GetValues
方法。問題是我不知道如何訪問TMenuItemSelector
所在的表單。
原始TComponentProperty
就是採用這種方法來遍歷所有可用實例:
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.
任何人都可以幫助嗎?
謝謝。
使用'Designer.Root'來轉換表單,我想。 – 2012-04-02 21:07:57