Show TLabel's content while dragging to TMemo and vice versa

Anybody here knows how to show the content of TLabel while dragging to TMemo and vice versa? I'm having a hard time finding a reference about it. Thanks in advance.

edwin
edeauxAsked:
Who is Participating?
 
Lee_NoverConnect With a Mentor Commented:
something like this then:

(descend from TDragObjectEx so it's automatically free'd)

unit Unit1;

interface

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

type
  TMyDragObject = class(TDragObjectEx)
  private
    FDragImages: TDragImageList;
    FLabel: TLabel;
  protected
    function GetDragImages: TDragImageList; override;
    function GetDragCursor(Accepted: Boolean; X: Integer;
      Y: Integer): TCursor; override;
  public
    constructor Create(ALabel: TLabel);
    destructor Destroy; override;
  end;

  TForm1 = class(TForm)
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Label1StartDrag(Sender: TObject;
      var DragObject: TDragObject);
    procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
  private
    FDragObject: TMyDragObject;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ TMyDragObject protected }

function TMyDragObject.GetDragImages: TDragImageList;
begin
  Result := FDragImages;
end;

{ TMyDragObject public }

constructor TMyDragObject.Create(ALabel: TLabel);
var
  B: TBitmap;
begin
  inherited Create;
  FLabel:=ALabel;
  FDragImages := TDragImageList.CreateSize(ALabel.Width, ALabel.Height);
  B := TBitmap.Create;
  B.Height := FDragImages.Height;
  B.Width := FDragImages.Width;
  try
    B.Canvas.CopyRect(Rect(0, 0, B.Width, B.Height), ALabel.Canvas, Rect(0, 0, B.Width, B.Height));
    FDragImages.AddMasked(B, ALabel.Color);
  finally
    B.Free;
  end;
  ALabel.Visible:=false;
  ALabel.Update;
end;

destructor TMyDragObject.Destroy;
begin
  if not Dropped then
    FLabel.Visible:=true;
  FDragImages.Free;
  inherited Destroy;
end;


function TMyDragObject.GetDragCursor(Accepted: Boolean; X,
  Y: Integer): TCursor;
begin
  Result:=crNone;
end;

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

procedure TForm1.Label1StartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
  FDragObject := TMyDragObject.Create(Label1);
  DragObject := FDragObject;
  Label1.Visible:=false;
  Label1.Update;
end;

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

end.
0
 
Lee_NoverCommented:
you have to add OLE Drag&Drop support to your app
one example is : http://delphi.icm.edu.pl/ftp/d30free/gwdrgdrp.zip
and the other easier way is to use Drag&Drop Suite
http://www.users.on.net/johnson/delphi/dragdrop0400.exe
or check how it's done in VirtualTreeView: http://www.soft-gems.net/VirtualTreeview/VT.php
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
edeauxAuthor Commented:
thanks for the post Kretz :) I have one thing to ask. How to create a bitmap of the Label? I need to show the label itself while dragging and also hide the drag cursor so only the label will be shown.
0
 
Lee_NoverCommented:
:)

just copy the canvas'

a small mod and it will do:

  TMyDragObject = class(TDragObject)
  private
    FDragImages: TDragImageList;
  protected
    function GetDragImages: TDragImageList; override;
    function GetDragCursor(Accepted: Boolean; X: Integer;
      Y: Integer): TCursor; override;
  public
    constructor Create(ALabel: TLabel);
    destructor Destroy; override;
  end;

constructor TMyDragObject.Create(ALabel: TLabel);
var
  B: TBitmap;
begin
  inherited Create;
  FDragImages := TDragImageList.CreateSize(ALabel.Width, ALabel.Height);
  B := TBitmap.Create;
  B.Height := FDragImages.Height;
  B.Width := FDragImages.Width;
  try
    B.Canvas.CopyRect(Rect(0, 0, B.Width, B.Height), ALabel.Canvas, Rect(0, 0, B.Width, B.Height));
    FDragImages.AddMasked(B, ALabel.Color);
  finally
    B.Free;
  end;
end;

function TMyDragObject.GetDragCursor(Accepted: Boolean; X,
  Y: Integer): TCursor;
begin
  Result:=crNone;
end;
0
 
Lee_NoverCommented:
oh .. and in LabelStartDrag change:
FDragObject := TMyDragObject.Create(Label1);
0
 
edeauxAuthor Commented:
thanks Lee :) I want to hide the label when it is dropped to a control like memo or anything that could accept a text from the caption of the label. I tried assigning the visible value of label to false but there was a portion of label visible. I put the assignment inn LabelStartDrag event.
0
 
edeauxAuthor Commented:
Anyway I wanted to mimic a drag and move. In that way i can move back the caption of the label to its original caption.
0
 
edeauxAuthor Commented:
why is it the dropped property of TMyDragObject becomes True even drop to itself? That makes the label invisible even isn't drop to other control.
0
 
edeauxAuthor Commented:
How to check if the dropping is successful to the target control? I'm asking Lee.
0
 
Lee_NoverCommented:
TDragObject has a property Dropped
that's what I checked in the example and set the labels visible to true/false
0
 
edeauxAuthor Commented:
anybody here knows how to check if the dropping to other control is successful or not?
0
 
Lee_NoverCommented:
? use the 'other control's' OnDragDrop event
0
 
edeauxAuthor Commented:
I found out that when the DragObject in Label's StartDrag is assigned a Class derived from TDragObject the EndDrag won't trigger. If the said event triggered i could set visible into true if the target is nil. Any idea how to trigger the event? I increased the points to 160.
0
 
Lee_NoverCommented:
why don't you check that in your TDragObject descendant ?

destructor TMyDragObject.Destroy;
begin
  if (not Dropped) or (DragTarget = nil) then
    FLabel.Visible:=true;
  FDragImages.Free;
  inherited Destroy;
end;
0
 
edeauxAuthor Commented:
i got it :) I created a field class name FSuccessfulDropped in the TMyDragObject that could be used to check if the dropped to other control is successful or not and used it on Lee_Nover recent post (changed DragTarget to FSuccessfulDropped).
0
 
kretzschmarCommented:
thanks for the points, edeaux, i did not exepected it ;-)

lee did a great job, well done

meikl ;-)
0
 
Lee_NoverCommented:
tnx :)

just yesterday I implemented OLE d&d for VTV .. so I was warmed up :)
0
 
edeauxAuthor Commented:
you're welcome kretz :) yeah you're right he's the guy :)
0
All Courses

From novice to tech pro — start learning today.