3

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

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

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

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

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

4

2 回答 2

2

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;

该代码似乎可以与选择、选项卡和撤消一起正常工作。

于 2012-06-27T11:13:51.717 回答
1

我认为您可以通过将两个事件分配给您的编辑器来实现您的目标: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;

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

于 2012-06-19T15:15:05.277 回答