Wikia

Delphi Programming

Drag and Drop

2,918pages on
this wiki
Talk0

Drag and Drop filesEdit

First, make sure to add ShellAPI to your uses clause, then use the DragAcceptFiles function in the OnCreate event for the form that will accept them.   This will tell Windows to send WM_DROPFILES messages to the application that is registered to accept dropped files, for example:

procedure TForm1.FormCreate(Sender: TObject); 
begin 
  DragAcceptFiles(Handle, True); 
end;

Then create a procedure that process the WM_DROPFILES message

procedure DropFiles(var msg: TMessage ); message WM_DROPFILES;

Example implementation:

procedure TForm1.DropFiles(var msg: TMessage ); 
var 
  i, count  : integer; 
  dropFileName : array [0..511] of Char; 
  MAXFILENAME: integer; 
begin 
  MAXFILENAME := 511; 
  // we check the query for amount of files received 
  count := DragQueryFile(msg.WParam, $FFFFFFFF, dropFileName, MAXFILENAME); 
  // we process them all 
  for i := 0 to count - 1 do 
  begin 
    DragQueryFile(msg.WParam, i, dropFileName, MAXFILENAME); 
     
    // dropFileName now has the current filename of dropped object. 
    // ** 
    // do something :D 
    // ** 
  end; 
  // release memory used 
  DragFinish(msg.WParam); 
end;

See alsoEdit

Code Snippets
DatabasesFiles and I/OForms/WindowsGraphicsNetworkingMath and AlgorithmsMiscellaneousMultimediaSystemVCL

Around Wikia's network

Random Wiki