• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1727
  • 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 GOracle 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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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