以embedde形式接受Drop

时间:2017-07-18 16:41:07

标签: delphi

我可以设置拖放简单示例,如下面的代码所示 (摘自http://www.chami.com/tips/delphi/111196D.html

但是如果我使用嵌入的表单(包含在另一种表单中的表单,我无法在嵌入的表单上删除文件:嵌入的表单不能作为放置目标

unit dropfile;

interface

uses
  Windows, Messages, SysUtils, Classes,
  Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

    //>>>

    // declare our DROPFILES message handler
    procedure AcceptFiles( var msg : TMessage );
      message WM_DROPFILES;
    //<<<
  end;

var
  Form1: TForm1;

implementation

uses
  //>>>
  //
  // this unit contains certain
  // functions that we'll be using
  //
  ShellAPI;
  //<<<

{$R *.DFM}

//>>>
procedure TForm1.AcceptFiles( var msg : TMessage );
const
  cnMaxFileNameLen = 255;
var
  i,
  nCount     : integer;
  acFileName : array [0..cnMaxFileNameLen] of char;
begin
  // find out how many files we're accepting
  nCount := DragQueryFile( msg.WParam,
                           $FFFFFFFF,
                           acFileName,
                           cnMaxFileNameLen );

  // query Windows one at a time for the file name
  for i := 0 to nCount-1 do
  begin
    DragQueryFile( msg.WParam, i,
                   acFileName, cnMaxFileNameLen );

    // do your thing with the acFileName
    MessageBox( Handle, acFileName, '', MB_OK );
  end;

  // let Windows know that you're done
  DragFinish( msg.WParam );
end;
//<<<

procedure TForm1.FormCreate(Sender: TObject);
begin
  //>>>
  //
  // tell Windows that you're
  // accepting drag and drop files
  //
  DragAcceptFiles( Handle, True );
  //<<<
end;

end.

1 个答案:

答案 0 :(得分:4)

您在表格DragAcceptFiles()活动中呼叫OnCreate。在Form的生命周期中,该事件仅被调用一次。但是Form的窗口可能会在Form的生命周期中多次重建。当在另一个窗口中嵌入Form时,情况就是如此。表单窗口被重新创建,但您没有在新重新创建的窗体窗口上调用DragAcceptFiles()。这就是您的WM_DROPFILES消息处理程序停止工作的原因。

要考虑窗口娱乐,您需要覆盖表单的虚拟CreateWnd()并从那里调用DragAcceptFiles()

unit dropfile;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
  private
  protected
    procedure CreateWnd; override;
    procedure DestroyWnd; override;
  public
    // declare our DROPFILES message handler
    procedure AcceptFiles(var msg: TMessage); message WM_DROPFILES;
  end;

var
  Form1: TForm1;

implementation

uses
  ShellAPI;

{$R *.DFM}

procedure TForm1.AcceptFiles(var msg: TMessage);
var
  i, nCount: Integer;
  acFileName: array [0..MAX_PATH-1] of Char;
begin
  // find out how many files we're accepting
  nCount := DragQueryFile(msg.WParam, $FFFFFFFF, nil, 0);
  // query Windows one at a time for the file name
  for i := 0 to nCount-1 do
  begin
    DragQueryFile(msg.WParam, i, acFileName, MAX_PATH);
    // do your thing with the acFileName
    MessageBox(Handle, acFileName, '', MB_OK);
  end;
  // let Windows know that you're done
  DragFinish(msg.WParam);
end;

procedure TForm1.CreateWnd;
begin
  inherited;
  // tell Windows that you're
  // accepting drag and drop files
  DragAcceptFiles(Handle, True);
end;

procedure TForm1.DestroyWnd;
begin
  // tell Windows that you're no
  // longer accepting drag and drop files
  DragAcceptFiles(Handle, False);
  inherited;
end;

end.