Link to home
Start Free TrialLog in
Avatar of ST3VO
ST3VOFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of Geert G
Geert G
Flag of Belgium image

from where are you dragging ?
from delphi or from the windows explorer ?
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

ASKER CERTIFIED SOLUTION
Avatar of ThievingSix
ThievingSix
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ST3VO

ASKER

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!
Avatar of ST3VO

ASKER

Sorted! Thanks a million :o)
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);