在两个组件之间拖放文本时,是否有一种简单的方法来显示我拖动的文本作为拖动光标?
答案 0 :(得分: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;