【发布时间】:2017-10-05 01:46:31
【问题描述】:
我的应用有用于剪切、复制和粘贴的菜单项。我知道如何执行这些操作,但不知道如何确定是否已选择某些内容。我需要知道这一点才能启用或禁用剪切和复制菜单项(我会在 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: 在 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;
【问题讨论】:
标签: delphi firemonkey