2017-10-05 56 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