views:

118

answers:

1

Based on this drag/drop article: http://delphi.about.com/od/adptips2005/qt/dropontimage.htm

I'm trying to catch an image dragged from Firefox onto a TPanel, I've got something that nearly works, but when I go to copy the file (that I thought would be temporarily created), the file did not exist, what did I miss?

procedure TformMain.FormCreate(Sender: TObject);
begin
  originalPanelWindowProc := pnMain.WindowProc;
  pnMain.WindowProc := PanelWindowProc;
  DragAcceptFiles(pnMain.Handle,true);
end;

procedure TformMain.PanelImageDrop(var Msg: TWMDROPFILES);
var
   numFiles : longInt;
   buffer : array[0..MAX_PATH] of char;
   src,dest : string;
begin
  numFiles := DragQueryFile(Msg.Drop, $FFFFFFFF, nil, 0) ;
  if numFiles > 1 then
    ShowMessage('You can drop only one image file at a time!')
  else begin
    DragQueryFile(Msg.Drop, 0, @buffer, sizeof(buffer)) ;
    src := string(AnsiString(buffer));
    dest := ExtractFilePath(Application.ExeName) + 'graphics\ ' + NewName + ExtractFileExt(src);
    ForceDirectories(ExtractFilePath(dest));
    CopyFile(PChar(src),PChar(dest),false);  (* <-- !!! src does not exist *)
  end;
end;

procedure TformMain.PanelWindowProc(var Msg: TMessage);
begin
  if Msg.Msg = WM_DROPFILES then
    PanelImageDrop(TWMDROPFILES(Msg))
  else
    originalPanelWindowProc(Msg) ;
end;
+3  A: 

this works for me:

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
  private
    originalPanelWindowProc : TWndMethod;
    procedure PanelWindowProc (var Msg : TMessage) ;
    procedure PanelImageDrop(var Msg: TWMDROPFILES);
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  originalPanelWindowProc := Panel1.WindowProc;
  Panel1.WindowProc := PanelWindowProc;
  DragAcceptFiles(panel1.Handle, true);
end;

procedure TForm1.PanelImageDrop(var Msg: TWMDROPFILES);
var
   numFiles : longInt;
   buffer : array[0..MAX_PATH] of char;
   src,dest : string;
begin
  numFiles := DragQueryFile(Msg.Drop, $FFFFFFFF, nil, 0) ;
  try
    if numFiles > 1 then
      ShowMessage('You can drop only one image file at a time!')
    else begin
      DragQueryFile(Msg.Drop, 0, @buffer, sizeof(buffer)) ;
      src := string(AnsiString(buffer));
      showmessage(
        src + #13 +
        booltostr(fileexists(src), true)
      );
    end;
  finally
    DragFinish(Msg.Drop);
  end;
end;

procedure TForm1.PanelWindowProc(var Msg: TMessage);
begin
  if Msg.Msg = WM_DROPFILES then
     PanelImageDrop(TWMDROPFILES(Msg))
   else
     originalPanelWindowProc(Msg) ;
end;

delphi 2007, firefox 3.5

glob
Hmm, looks like the first file action (FileExists or ForceDirectory) clears the file out, so this worked, after I adapted mine (moved the ForceDirectory up), thx for the example
Zartog