如何从后代组件中删除属性

时间:2013-04-12 01:22:04

标签: delphi

我已经创建了一个TListView后代组件......它功能完美无缺,但我想知道是否有可能删除后代中我不想要的TListView属性。我不想在对象检查器中显示的属性是LargeImages,RowSelect,ShowColumnHeader,ShowWorkAreas,ViewStyle,OwnerData,OnData和OnDataFind。后代只有一个viewstyle vsIcon。

这是组件的接口部分:

  TImageEnListView = class(TListView)
  private
    FImageList: TImageList;
    FImageIndex: integer;
    FStringList: TStringList;
    FThumbnailWidth: integer;
    FThumbnailHeight: integer;
    FIconVerticalSpacing: integer;
    FIconHorzontalSpacing: integer;
    FFolder: string;
    FShadowedThumbnail: boolean;
    FShowCaptions: boolean;
    FShowTips: boolean;
    FBackgroundWorker: TBackgroundWorker;
    FTaskDialog: TTaskDialog;
    procedure BackgroundWorkerWork(Worker: TBackgroundWorker);
    { Event after threading is complete }
    procedure BackgroundWorkerWorkComplete(Worker: TBackgroundWorker; Cancelled: Boolean);
    { Event for feedback to GUI }
    procedure BackgroundWorkerWorkFeedback(Worker: TBackgroundWorker; FeedbackID,
      FeedbackValue: Integer);
  public
   { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    { Clears thumbnails, fileList and imageList }
    procedure ClearThumbnails;
    procedure InfoTip(Sender: TObject; Item: TListItem; var InfoTip: string);
    procedure Data(Sender: TObject; Item: TListItem);
    procedure DataFind(Sender: TObject; Find: TItemFind; const FindString: string;
      const FindPosition: TPoint; FindData: Pointer; StartIndex: Integer; Direction:
      TSearchDirection;
      Wrap: Boolean; var Index: Integer);
    procedure FillItems;
    property BackgroundWorker: TBackgroundWorker read FBackgroundWorker;
  published
    { Published declarations }
    property Folder: string read FFolder write FFolder;
    property FileList: TStringList read FStringList write FStringList;
    property ImageList: TImageList read FImageList write FImageList;
    property ThumbnailWidth: integer read FThumbnailWidth write FThumbnailWidth default 170;
    property ThumbnailHeight: integer read FThumbnailHeight write FThumbnailHeight default 120;
    property ShadowedThumbnail: boolean read FShadowedThumbnail write FShadowedThumbnail default
      True;
    property ShowTips: boolean read FShowTips write FShowTips default False;
    property ShowCaptions: boolean read FShowCaptions write FShowCaptions default True;
  end;

3 个答案:

答案 0 :(得分:13)

TTCustomListView而不是TListView创建您的课程,只展示您想要显示的属性和事件。您可以使用VCL源(在ComCtrls单元中)以完全相同的方式查看TListView的完成方式(当然,TListView除了它们之外)。这是一个(非常无用的)如何做的例子:

TImageEnListView = class(TCustomListView)
... other code
published
  // Only expose some of the properties that are protected
  // in TCustomListView. Meaningless from a use standpoint,
  // but demonstrates the technique
  property Columns;
  property ColumnClick;
  property Constraints;
  property DragCursor;
  property DragKind;
  property DragMode;
  property Enabled;
  property Font;
  property FlatScrollBars;
  property FullDrag;
  property GridLines;
  property HideSelection;
end;

对于没有TCustom祖先的类,您可以创建一个包装类,并将要更改的类包含在其中的私有字段中,并仅通过新属性公开所需的功能你发表。这样的事情应该让你开始(我只是暴露一两处房产,你可以从那里拿走):

type
  TMySpecialListView=class(TComponent)
  private
    FEnListView: TImageEnListView;
    function GetThumbnailHeight: Integer;
    function GetThumbnailWidth: Integer;
    procedure SetThumbnailHeight(Value: Integer);
    procedure SetThumbnailWidth(Value: Integer);
  public
    constructor Create(AOwner: TComponent); override;
  published
    property ThumbnailHeight: Integer read GetThumbnailHeight
      write SetThumbnailHeight;
    property ThumbnailWidth: Integer read GetThumbnailWidth
      write SetThumbnailWidth;
  end;

implementation

{ TMySpecialListView }

constructor TMySpecialListView.Create(AOwner: TComponent);
begin
  inherited;
  FEnhListView := TImageEnListView.Create(Self);
  FEnhListView.Parent := Self.Parent;
  // Set other properties needed like width and height. You
  // can get the ones you need from your current .dfm values
  // for a new blank form with your TImageEnListView dropped
  // on it.
end;

function TMySpecialListView.GetThumbnailHeight: Integer;
begin
  Result := FEnhListView.ThumbnailHeight;
end;

function TMySpecialListView.GetThumbnailWidth: Integer;
begin
  Result := FEnhListView.ThumbnailWidth;
end;

procedure TMySpecialListView.SetThumbnailHeight(Value: Integer);
begin
  if Value <> FEnhListView.ThumbnailHeight then
    FEnhListView.ThumbnailHeight := Value;
end;

procedure TMySpecialListView.SetThumbnailWidth(Value: Integer);
begin
  if Value <> FEnhListView.ThumbnailWidth then
    FEnhListView.ThumbnailWidth := Value;
end;

答案 1 :(得分:3)

在我看来,在Object Inspector中隐藏(删除)属性的最佳方法是使用:

UnlistPublishedProperty(TImageEnListView , 'LargeImages')

UnlistPublishedProperty程序位于DesignIntf​​.dcu中,这意味着您的组件包应包含designide

答案 2 :(得分:0)

为了完整起见,除了Ken's probably most applicable solution之外,还有一个(可能有点牵强)的解决方案,当没有其他可能性时,从子组件中删除属性。

my answer here的第三部分演示了如何通过注册组件PropertyEditor并覆盖GetProperties来过滤特定属性名称来过滤掉自定义组件的子组件的属性。