2017-10-05 41 views
0

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

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; 

回答

2

TEditTMemo(和“提供一个文本区域的所有控件”)实现ITextInput接口,它有GetSelection(),GetSelectionBounds()GetSelectionRect()方法。

+0

以您的回答为指导,我采用了编辑中显示的代码来显示我的问题,它似乎可行。 – Duns

+1

@Duns:你的第二个'if'可以被消除:'EditCut1.Enabled:= Length(Textinput.GetSelection)> 0;'这也可以工作,而不需要为临时'string'分配内存:'EditCut1。启用:=不是Textinput.GetSelectionBounds.IsEmpty;'或'EditCut1.Enabled:= not Textinput.GetSelectionRect.IsEmpty;' –

+0

确实看起来我的第二个如果可以被淘汰。我的if else可以重写为EditCut1.Enabled:= Length(Textinput.GetSelection)> 0;'。 'EditCut1.Enabled:=不是Textinput.GetSelectionRect.IsEmpty;'也可以。奇怪的是,'EditCut1.Enabled:= not Textinput.GetSelectionBounds.IsEmpty;'似乎不工作,因为它从来没有返回true。 – Duns