【问题标题】:Drag and drop text into SynEdit control将文本拖放到 SynEdit 控件中
【发布时间】:2012-06-21 08:27:27
【问题描述】:

我在表单上有一个 TSynEdit 控件,我想从 TVirtualStringTree 拖放焦点节点文本。我希望它的行为方式与您在 TSynEdit 控件中拖放突出显示的文本时相同:

  • 当您拖动 TSynEdit 时,插入符号应标记当前放置位置。
  • 删除文本时,它应该替换当前突出显示的所有文本。
  • 放置位置应正确处理标签。

我查看了 TSynEdit DragOver 事件中的代码,但它使用了几个我无法在后代类中访问的变量和过程,因为它们已被声明私人

我检查了所有 TSynEdit 演示,但找不到满足我需求的演示。

有人成功地做到了这一点吗?

【问题讨论】:

    标签: delphi drag-and-drop synedit


    【解决方案1】:

    AZ01 的回答并没有完全符合我的要求,但它确实为我指明了正确的方向。这是我最终使用的代码:

    procedure TfrmTemplateEdit.memTemplateDragDrop(Sender, Source: TObject; X,
      Y: Integer);
    var
      InsertText: String;
      ASynEdit: TSynEdit;
      OldSelStart, DropIndex: Integer;
      LCoord: TBufferCoord;
    begin
      // Set the Insert Text
      InsertText := 'The text to insert';
    
      // Set the SynEdit memo
      ASynEdit := TSynEdit(Sender);
    
      // Get the position on the mouse
      ASynEdit.GetPositionOfMouse(LCoord);
    
      // Find the index of the mouse position
      DropIndex := ASynEdit.RowColToCharIndex(LCoord);
    
      // Delete any selected text
      If (ASynEdit.SelAvail) and
         (DropIndex >= ASynEdit.SelStart) and
         (DropIndex <= ASynEdit.SelEnd) then
      begin
        // Store the old selection start
        OldSelStart := ASynEdit.SelStart;
    
        // Delete the text
        ASynEdit.ExecuteCommand(ecDeleteChar, #0, Nil);
    
        // Move the caret
        ASynEdit.SelStart := OldSelStart;
      end
      else
      begin
        // Position the caret at the mouse location
        ASynEdit.CaretXY := LCoord;
      end;
    
      // Insert the text into the memo
      ASynEdit.ExecuteCommand(ecImeStr, #0, PWideChar(InsertText));
    
      // Set the selection start and end points to selected the dropped text
      ASynEdit.SelStart := ASynEdit.SelStart - length(InsertText);
      ASynEdit.SelEnd := ASynEdit.SelStart + length(InsertText);
    
      // Set the focus to the SynEdit
      ASynEdit.SetFocus;
    end;
    

    代码似乎可以正常使用选择、选项卡和撤消。

    【讨论】:

      【解决方案2】:

      我认为您可以通过将两个事件分配给您的编辑器来实现您的目标:DragOver/DragDrop。

      1/ 您在 DragOver 事件 中测试有效性,您还可以通过调用 GetPositionOfMouse

      移动插入符号
      Procedure TForm1.EditorDragOver(Sender,Source: TObject;X,Y: Integer; State: TDragState; Var Accept: Boolean);
      Var
        LCoord: TBufferCoord;
        LMemo: TSynMemo;
      Begin
        LMemo := TSynMemo(Sender);
        // In your case you would rather test something with your tree...
        Accept := Clipboard.AsText <> '';
        // "As you drag over the TSynEdit, the caret should mark the current drop position": OK
        If LMemo.GetPositionOfMouse(LCoord) Then
          LMemo.CaretXY := LCoord;
      End;
      

      2/ 您在 DragDrop 事件 中使用编辑器命令来清除选择并插入字符

      Procedure TForm1.EditorDragDrop(Sender, Source: TObject; X,Y: Integer);
      Var
        LMemo: TSynMemo;
      Begin
        LMemo := TSynMemo(Sender);
        // "When the text is dropped, it should replace any text that is currently highlighted." : OK
        If LMemo.SelAvail Then
          LMemo.ExecuteCommand( ecDeleteChar , #0, Nil );
        // Paste, same comment as previously, here it's just an example...
        LMemo.ExecuteCommand( ecPaste, #0, Nil );
      End;
      

      这必须根据您的上下文进行一些调整。

      【讨论】:

        猜你喜欢
        • 2011-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-05
        • 2013-04-21
        • 2010-11-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多