Solved

Drag and Drop texto from a listbox to another showing the word being dragged.

Posted on 2013-12-04
3
536 Views
Last Modified: 2013-12-04
Hello,

I have a listbox1 with some words and a second empty listbox2. I can drag any word from listbox1 and drop it to listbox2, but the cursor is a drag figure and can't see the word that I am draging to the destination.

Is it possible to do it as it was made to picture? When I drag the image, I can pic it and it float with my cursor until the destination, can I do that with words?

pic drag from this thread http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_28302574.html#a39695283

thanks
Alex
0
Comment
Question by:hidrau
3 Comments
 
LVL 19

Accepted Solution

by:
Thommy earned 250 total points
ID: 39695643
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, jpeg, ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    ListBox1: TListBox;
    ListBox2: TListBox;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Image1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure Image1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure ListBox2StartDrag(Sender: TObject; var DragObject: TDragObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

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


var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListBox2.DragMode := dmAutomatic;
  ControlStyle := ControlStyle + [csDisplayDragImage];
  Image1.ControlStyle := ListBox2.ControlStyle + [csDisplayDragImage];
end;

procedure TForm1.Image1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  ListBox1.Items.Add(ListBox2.Items[ListBox2.Tag]);
end;

procedure TForm1.Image1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept:=true;
end;


procedure TForm1.ListBox2StartDrag(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];
    List.Tag:=List.ItemIndex;
end;

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;


end.

Open in new window

0
 
LVL 25

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 250 total points
ID: 39696634
My example is very similar. I use transparency in dragging text. Works as I described in another thread and Thommy did it similar too.

Thommy, small comment if I may - you didn't destroy your TDragImageList after usage.
drag.zip
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 39697035
Thanks very much everybody
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now