How to Drop Images from Windows Explorer to a TImage control

106 65


Delphi makes it really easy to implement dragging & dropping into your applications.

Let's say you need to "open" you application to the Windows - allowing it to accept files dragged from the Windows Explorer.

Here's how to accept dragging (and dropping) an image file onto a TImage...

To be able to accept files dropped from the Windows Explorer, you need to let Windows know that your application accepts dropped files (by calling the DragAcceptFiles procedure from the ShellApi unit).

Next, you must respond to the drag events by creating a procedure to handle the WM_DROPFILES message. (Hint: Handling Windows Message).

The DragAcceptFiles procedure accepts a Handle as the first parameter. Since the TImage control does not provide a handle you cannot register a TImage to accept files :(
What you *can* do is to:
  1. Place a TImage on a Panel (Image.Align := alClient)
  2. Call DragAcceptFiles for the Panel
  3. Subclass the Panel's WindowProcedure to make it respond to the WM_DROPFILES message
  4. (Try to) load the image file dropeed in the TImage after getting your hands ot the image file name (using the DragQueryFile procedure)

This is the full source of the Form1's unit (where a TImage named Image1 is placed on a TPanel named Panel1):

unit Unit1;interfaceuses    Windows, Messages, SysUtils, Variants, Classes,    Graphics, Controls, Forms, Dialogs, ExtCtrls;type    TForm1 = class(TForm)      Panel1: TPanel;      Image1: TImage;      procedure FormCreate(Sender: TObject) ;    private      originalPanelWindowProc : TWndMethod;      procedure PanelWindowProc (var Msg : TMessage) ;      procedure PanelImageDrop (var Msg : TWMDROPFILES) ;    public      { Public declarations }    end;var    Form1: TForm1;implementation {$R *.dfm}uses ShellApi;procedure TForm1.FormCreate(Sender: TObject) ; begin    originalPanelWindowProc := Panel1.WindowProc;    Panel1.WindowProc := PanelWindowProc;   DragAcceptFiles(Panel1.Handle,true) ; end; (*FormCreate*)procedure TForm1.PanelWindowProc(var Msg: TMessage) ; begin    if Msg.Msg = WM_DROPFILES then      PanelImageDrop(TWMDROPFILES(Msg))    else      originalPanelWindowProc(Msg) ; end; (*PanelWindowProc*)procedure TForm1.PanelImageDrop(var Msg: TWMDROPFILES) ; var    numFiles : longInt;    buffer : array[0..MAX_PATH] of char; begin    numFiles := DragQueryFile(Msg.Drop, $FFFFFFFF, nil, 0) ;    if numFiles > 1 then    begin      ShowMessage('You can drop only one image file at a time!') ;    end    else    begin      DragQueryFile(Msg.Drop, 0, @buffer, sizeof(buffer)) ;      try        Image1.Picture.LoadFromFile(buffer) ;      except        on EInvalidGraphic do ShowMessage('Unsupported image file, or not an image!') ;      end;    end; end; (*PanelImageDrop*)end.

Delphi tips navigator:
» How to Parse a Delimited String Into a String List
« How to change the default Windows printer from Delphi code

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.