2012-06-19 85 views
2

我有一個窗體上的TSynEdit控制,我想將它從一個TVirtualStringTree下降聚焦節點文本。我想它以同樣的方式表現,當你拖動作爲和TSynEdit控制範圍之內下降高亮文本:拖放文本SynEdit控制

  • 當你拖過TSynEdit,光標應該標記當前下落位置。
  • 當文本被刪除時,它應該替換當前突出顯示的任何文本。
  • 放置位置應正確處理標籤。

我已經看過在TSynEdit的dragover事件的代碼,但它使用幾個變量和程序,只要是在宣佈私人我不能在子類中訪問。

我已經檢查了所有TSynEdit演示,並找不到滿足我需求的演示。

有人成功地做到了這一點嗎?

回答

0

我認爲您可以通過將兩個事件分配給您的編輯器來實現您的目標: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; 

這將不得不根據您的上下文進行調整。

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; 

該代碼似乎與選擇,選項卡和撤消正常工作。