这是我出来实施的hack。
它不需要手动更改我已经拥有的 ActionList 或 MenuItems 和 ToolButtons,因为我为该类保留了相同的名称TEditCopy。
type
TEditCopy = class(StdActns.TEditCopy)
public
function HandlesTarget(Target: TObject): Boolean; override;
procedure ExecuteTarget(Target: TObject); override;
procedure UpdateTarget(Target: TObject); override;
end;
它还扩展了标准 TEditCopy 操作,并具有支持 TListBox 所需的功能。
function TEditCopy.HandlesTarget(Target: TObject): Boolean;
begin
result:=(inherited handlesTarget(Target)) or (target is TListbox);
end;
procedure TEditCopy.ExecuteTarget(Target: TObject);
begin
if (target is TListBox) and (TListBox(Target).ItemIndex<>-1) then
clipboard.AsText:=TListBox(Target).Items[TListBox(Target).ItemIndex]
else
inherited;
end;
procedure TEditCopy.UpdateTarget(Target: TObject);
begin
if target is TListbox then
Enabled := true
else
inherited;
end;
应用程序的其余部分保持不变。所有的复制/粘贴功能仍然完全没有代码实现。