如何抓取从Firefox拖动的图像?

时间:2009-11-03 03:07:31

标签: delphi image firefox drag-and-drop

基于此拖放文章:http://delphi.about.com/od/adptips2005/qt/dropontimage.htm

我正在尝试捕捉从Firefox拖到TPanel上的图像,我有一些几乎可以工作的东西,但是当我去复制文件时(我认为会暂时创建),该文件不存在,我错过了什么?

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;

1 个答案:

答案 0 :(得分:3)

这对我有用:

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