确定是否选择了可以剪切或复制到剪贴板的文本
Determine if text has been selected that may be cut or copied to clipboard
我的应用程序有用于剪切、复制和粘贴的菜单项。我知道如何执行这些操作,但不知道如何确定是否已选择某些内容。我需要知道这一点才能启用或禁用剪切和复制菜单项(我会在 TAction.OnUpdate
事件中执行)。
例如,要从当前获得焦点的控件中复制选定的文本,我使用这个:
if Assigned(Focused) and TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService, Svc) then
if Supports(Focused.GetObject, ITextActions, TextAction) then
TextAction.CopyToClipboard;
但是,如何确定是否已在当前获得焦点的控件中选择了任何文本?
我可以遍历所有控件并使用如下条件:
if ((Focused.GetObject is TMemo) and (TMemo(Focused.GetObject).SelLength > 0) then
<Enable the Cut and Copy menu items>
但这似乎并不优雅。有没有更好的方法?
编辑:
根据 Remy 的回答,我编写了以下程序并且似乎有效:
procedure TMyMainForm.EditCut1Update(Sender: TObject);
var
Textinput: ITextinput;
begin
if Assigned(Focused) and Supports(Focused.GetObject, ITextinput, Textinput) then
if Length(Textinput.GetSelection) > 0 then
EditCut1.Enabled := True
else
EditCut1.Enabled := False;
end;
EditCut1
是我的 TAction
用于剪切操作,EditCut1Update
是它的 OnUpdate
事件处理程序。
编辑 2:
根据雷米对我第一次编辑的评论,我现在使用:
procedure TMyMainForm.EditCut1Update(Sender: TObject);
var
TextInput: ITextInput;
begin
if Assigned(Focused) and Supports(Focused.GetObject, ITextInput, TextInput)
then
EditCut1.Enabled := not TextInput.GetSelectionRect.IsEmpty;
end;
TEdit
和 TMemo
(以及 "all controls that provide a text area")实现了 ITextInput
interface, which has GetSelection()
, GetSelectionBounds()
, and GetSelectionRect()
方法。
我的应用程序有用于剪切、复制和粘贴的菜单项。我知道如何执行这些操作,但不知道如何确定是否已选择某些内容。我需要知道这一点才能启用或禁用剪切和复制菜单项(我会在 TAction.OnUpdate
事件中执行)。
例如,要从当前获得焦点的控件中复制选定的文本,我使用这个:
if Assigned(Focused) and TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService, Svc) then
if Supports(Focused.GetObject, ITextActions, TextAction) then
TextAction.CopyToClipboard;
但是,如何确定是否已在当前获得焦点的控件中选择了任何文本?
我可以遍历所有控件并使用如下条件:
if ((Focused.GetObject is TMemo) and (TMemo(Focused.GetObject).SelLength > 0) then
<Enable the Cut and Copy menu items>
但这似乎并不优雅。有没有更好的方法?
编辑:
根据 Remy 的回答,我编写了以下程序并且似乎有效:
procedure TMyMainForm.EditCut1Update(Sender: TObject);
var
Textinput: ITextinput;
begin
if Assigned(Focused) and Supports(Focused.GetObject, ITextinput, Textinput) then
if Length(Textinput.GetSelection) > 0 then
EditCut1.Enabled := True
else
EditCut1.Enabled := False;
end;
EditCut1
是我的 TAction
用于剪切操作,EditCut1Update
是它的 OnUpdate
事件处理程序。
编辑 2: 根据雷米对我第一次编辑的评论,我现在使用:
procedure TMyMainForm.EditCut1Update(Sender: TObject);
var
TextInput: ITextInput;
begin
if Assigned(Focused) and Supports(Focused.GetObject, ITextInput, TextInput)
then
EditCut1.Enabled := not TextInput.GetSelectionRect.IsEmpty;
end;
TEdit
和 TMemo
(以及 "all controls that provide a text area")实现了 ITextInput
interface, which has GetSelection()
, GetSelectionBounds()
, and GetSelectionRect()
方法。