Solved

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

Posted on 2004-10-27
616 Views
Last Modified: 2010-04-05
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
0
Question by:edeaux
    19 Comments
     
    LVL 12

    Expert Comment

    by:Lee_Nover
    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
     
    LVL 27

    Assisted Solution

    by:kretzschmar
    0
     

    Author Comment

    by:edeaux
    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
     
    LVL 12

    Expert Comment

    by:Lee_Nover
    :)

    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
     
    LVL 12

    Expert Comment

    by:Lee_Nover
    oh .. and in LabelStartDrag change:
    FDragObject := TMyDragObject.Create(Label1);
    0
     

    Author Comment

    by:edeaux
    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
     

    Author Comment

    by:edeaux
    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
     
    LVL 12

    Accepted Solution

    by:
    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
     

    Author Comment

    by:edeaux
    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
     

    Author Comment

    by:edeaux
    How to check if the dropping is successful to the target control? I'm asking Lee.
    0
     
    LVL 12

    Expert Comment

    by:Lee_Nover
    TDragObject has a property Dropped
    that's what I checked in the example and set the labels visible to true/false
    0
     

    Author Comment

    by:edeaux
    anybody here knows how to check if the dropping to other control is successful or not?
    0
     
    LVL 12

    Expert Comment

    by:Lee_Nover
    ? use the 'other control's' OnDragDrop event
    0
     

    Author Comment

    by:edeaux
    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
     
    LVL 12

    Expert Comment

    by:Lee_Nover
    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
     

    Author Comment

    by:edeaux
    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
     
    LVL 27

    Expert Comment

    by:kretzschmar
    thanks for the points, edeaux, i did not exepected it ;-)

    lee did a great job, well done

    meikl ;-)
    0
     
    LVL 12

    Expert Comment

    by:Lee_Nover
    tnx :)

    just yesterday I implemented OLE d&d for VTV .. so I was warmed up :)
    0
     

    Author Comment

    by:edeaux
    you're welcome kretz :) yeah you're right he's the guy :)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: JavaScript Coding - Massive 12-Part Bundle

    Regardless of your programming skill level, you'll go from basics to advanced concepts in a vast array of JavaScript subjects including Sammy.js, Agility.js, Ember.js, Node.js, jQuery, AJAX, Extjs, AngularJS, Knockout.js, and JSON.

    This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

    877 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