• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1637
  • Last Modified:

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
0
ST3VO
Asked:
ST3VO
  • 2
  • 2
  • 2
1 Solution
 
Geert GruwezOracle dbaCommented:
from where are you dragging ?
from delphi or from the windows explorer ?
0
 
ThievingSixCommented:
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
 
ThievingSixCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 GruwezOracle 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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now