确定是否已选择可以剪切或复制到剪贴板的文本

时间:2017-10-05 01:46:32

标签: delphi firemonkey

我的应用程序包含剪切,复制和粘贴的菜单项。我知道如何执行这些操作,但不知道如何确定是否已选择某些操作。我需要知道这一点来启用或禁用剪切和复制菜单项(我将在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>

但这似乎并不优雅。还有更好的方法吗?

编辑:

根据雷米的回答,我编写了以下内容,似乎有效:

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;

EditCut1TAction用于剪切操作,EditCut1Update是其OnUpdate事件处理程序。

编辑2: 按照Remy对我的第一次编辑的评论,我现在正在使用:

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;

1 个答案:

答案 0 :(得分:2)

TEditTMemo(以及&#34;提供文本区域的所有控件&#34;)实现ITextInput界面,其中包含GetSelection(),{{ 3}}和GetSelectionBounds()方法。

相关问题