2

我已经创建了一个 TListView 后代组件...它的功能完美无缺,但我想知道是否可以删除我不希望在后代中的 TListView 属性。我不想出现在对象检查器中的属性是 LargeImages、RowSelect、ShowColumnHeader、ShowWorkAreas、ViewStyle、OwnerData、OnData 和 OnDataFind。后代只有一种视图样式 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;
4

3 回答 3

15

TTCustomListView从而不是创建您的类TListView,并只公开您想要使其可见的属性和事件。您可以使用 VCL 源(在单元中)以完全相同的方式ComCtrls查看它是如何完成的(当然,除了将它们全部公开)。这是一个(非常无用的)示例,说明如何这样做:TListViewTListView

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;
于 2013-04-12T01:24:25.050 回答
3

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

UnlistPublishedProperty(TImageEnListView , 'LargeImages')

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

于 2015-04-08T12:15:54.957 回答
0

只是为了完整起见,除了Ken 可能最适用的解决方案之外,特此提供一个(可能有些牵强的)解决方案,用于在没有其他可能性时从子组件中删除属性。

我在这里回答的第三部分演示了如何通过注册组件 PropertyEditor 并覆盖GetProperties以过滤特定属性名称来过滤掉自定义组件的子组件的属性。

于 2013-04-23T20:07:40.417 回答