Drag and drop to TImages

Hi all,

I have 5 panels and on these panels I have 5 TImages (One on each set to alclient)
I need to drag and drop images to the TImage components.
I am using Delphi 2007, just in case it matters.

Hope you can help,

Thanks

ST3VO
ST3VOAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert GOracle dbaCommented:
from where are you dragging ?
from delphi or from the windows explorer ?
0
Dagan HooverDeveloperCommented:
This will do from windows.
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ShellAPI;
 
type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Image1: TImage;
    Image2: TImage;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure AcceptFiles(var Msg: TMessage); message WM_DROPFILES;
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.AcceptFiles(var Msg: TMessage);
const
  cnMaxFileNameLen = 255;
var
  nCount : Integer;
  acFileName : Array [0..cnMaxFileNameLen] Of Char;
  DropPosition : TPoint;
  DropControl : TControl;
begin
  nCount := DragQueryFile(Msg.WParam,$FFFFFFFF,acFileName,cnMaxFileNameLen);
  If nCount > 1 Then
    begin
    DragFinish(Msg.WParam);
    Exit;
  end;
  DragQueryFile(Msg.WParam,0,acFileName,cnMaxFileNameLen);
  DragQueryPoint(Msg.WParam,DropPosition);
  DropControl := ControlAtPos(DropPosition,False,True,True);
  If DropControl.ClassType = TImage Then
    begin
    Try
      If UpperCase(ExtractFileExt(acFileName)) = '.JPG' Then
        begin
        (DropControl as TImage).Picture.LoadFromFile(acFileName);
      end
      Else If UpperCase(ExtractFileExt(acFileName)) = '.BMP' Then
        begin
        (DropControl as TImage).Picture.LoadFromFile(acFileName);
      end;
    Finally
    end;
  end;
  DragFinish(Msg.WParam);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  DragAcceptFiles(Handle,True);
end;
 
end.

Open in new window

0
Dagan HooverDeveloperCommented:
This will work for both going timage to timage and windows to timage.
unit Unit13;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ShellAPI, JPEG;
 
type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Image1: TImage;
    Image2: TImage;
    procedure FormCreate(Sender: TObject);
    procedure ImageDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure ImageMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure AcceptFiles(var Msg: TMessage); message WM_DROPFILES;
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.AcceptFiles(var Msg: TMessage);
const
  cnMaxFileNameLen = 255;
var
  nCount : Integer;
  acFileName : Array [0..cnMaxFileNameLen] Of Char;
  DropPosition : TPoint;
  DropControl : TControl;
begin
  nCount := DragQueryFile(Msg.WParam,$FFFFFFFF,acFileName,cnMaxFileNameLen);
  If nCount > 1 Then
    begin
    DragFinish(Msg.WParam);
    Exit;
  end;
  DragQueryFile(Msg.WParam,0,acFileName,cnMaxFileNameLen);
  DragQueryPoint(Msg.WParam,DropPosition);
  DropControl := ControlAtPos(DropPosition,False,True,True);
  If DropControl is TImage Then
    begin
    Try
      If UpperCase(ExtractFileExt(acFileName)) = '.JPG' Then
        begin
        (DropControl as TImage).Picture.LoadFromFile(acFileName);
      end
      Else If UpperCase(ExtractFileExt(acFileName)) = '.BMP' Then
        begin
        (DropControl as TImage).Picture.LoadFromFile(acFileName);
      end;
    Finally
    end;
  end;
  DragFinish(Msg.WParam);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  DragAcceptFiles(Handle,True);
end;
 
procedure TForm1.ImageMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  (Sender as TImage).BeginDrag(True);
end;
 
procedure TForm1.ImageDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  If State = dsDragEnter Then
    begin
    Accept := (Source is TImage);
    If Accept Then
      begin
      (Sender as TImage).Picture := (Source as TImage).Picture;
    end;
  end;
end;
 
end.

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

ST3VOAuthor Commented:
Hmmm... strange!

If I put the TImages outside the TPanel it works BUT it doesn't work when TImage is on a TPanel.

Apart from that it would be perfect.

Any ideas why?

I'm using the last posted code btw!
0
ST3VOAuthor Commented:
Sorted! Thanks a million :o)
0
Geert GOracle dbaCommented:
ControlAtPos only finds the direct descendants of the form

you could call the function again until you get a TImage

DropControl := Self;
repeat
  if DropControl <> Self then
  begin
    DropPosition.X := DropPosition.X - DropControl.Left;
    DropPosition.Y := DropPosition.Y - DropControl.Top;
  end;
  DropControl := DropControl.ControlAtPos(DropPosition,False,True,True);
until (DropControl is TImage) or (DropControl = nil);

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.