2012-10-24 13 views
4

在兩個組件之間拖放文本時,是否有一種簡單的方式顯示我拖動的文本作爲拖動光標?如何讓文本作爲我的拖動光標?

+1

當然,你的意思是*除了*拖動光標?我的意思是,你怎麼知道'文本光標'的熱點在哪裏? –

+0

+1好點 - 除非熱點位於文本的左上方 – Mawg

+1

將帶有文本的獨立AlwaysOnTop透明窗口移動並始終位於光標附近或下方。像Stardock CursorFX一樣。 –

回答

6

要從列表框中拖動一個項目,並顯示其文本表示與拖動光標一起:

type 
    TTextDragObject = class(TDragControlObjectEx) 
    private 
    FDragImages: TDragImageList; 
    FText: String; 
    protected 
    function GetDragImages: TDragImageList; override; 
    end; 

{ TTextDragObject } 

function TTextDragObject.GetDragImages: TDragImageList; 
var 
    Bmp: TBitmap; 
begin 
    if FDragImages = nil then 
    begin 
    FDragImages := TDragImageList.Create(Control); 
    Bmp := TBitmap.Create; 
    try 
     Bmp.Width := Bmp.Canvas.TextWidth(FText); 
     Bmp.Height := Bmp.Canvas.TextHeight(FText); 
     Bmp.Canvas.TextOut(0, 0, FText); 
     FDragImages.Width := Bmp.Width; 
     FDragImages.Height := Bmp.Height; 
     FDragImages.SetDragImage(FDragImages.Add(Bmp, nil), 0, 0); 
    finally 
     Bmp.Free; 
    end; 
    end; 
    Result := FDragImages; 
end; 

{ TForm1 } 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    ControlStyle := ControlStyle + [csDisplayDragImage]; 
    ListBox1.ControlStyle := ListBox1.ControlStyle + [csDisplayDragImage]; 
end; 

procedure TForm1.ListBox1StartDrag(Sender: TObject; 
    var DragObject: TDragObject); 
var 
    List: TListbox absolute Sender; 
begin 
    DragObject := TTextDragObject.Create(List); 
    if List.ItemIndex > -1 then 
    TTextDragObject(DragObject).FText := List.Items[List.ItemIndex]; 
end; 
+0

好吧,它的工作原理。 +1和答案,@ NLGN。但是,用戶應該注意,如果他們的OnDragDop處理程序檢查源組件,它將是TTextDragObject類型。對我來說沒問題,因爲我只有一個源和一個目的地,但是......另外,我在文本之前放置了一些空格,以便從光標下面得到它。 – Mawg

+3

@Mawg - 使用TTextDragObject的DragHotspot而不是偏移圖像。 –

+2

@Mawg'TTextDragObject(Source).Control'是源組件。 – NGLN