如何允许在Delphi中拖动文件以用于特定控件 [英] How do I allow dragging files for specific control(s) in Delphi

查看:266
本文介绍了如何允许在Delphi中拖动文件以用于特定控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人将文件放到特定控件(例如TMemo)后立即接受文件。我从以下示例开始: http://delphi.about.com/od /windowsshellapi/a/accept-filedrop.htm 并进行如下修改:

I would like to accept files as soon as someone drops a file to a specific control (e.g. TMemo). I started with this example: http://delphi.about.com/od/windowsshellapi/a/accept-filedrop.htm and modified it like this:

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

这允许控件显示拖动图标,但显示正确的 WM_DROPFILES 消息,因为 DragAcceptFiles 需要一个(Parent?)窗口句柄。我可以在 WMDROPFILES 过程中确定MemoHandle,但我不知道如何,而且拖动光标现在适用于所有控件。如何允许拖动特定控件(并阻止其他控件拖动)?

This allows the control to show the dragging icon but the proper WM_DROPFILES message is not getting called because DragAcceptFiles needs a (Parent?)windowhandle. I could determine the MemoHandle in the WMDROPFILES procedure but I don't how, plus the dragging cursor applies for all the controls now. How do I allow dragging for a specific control (and block other controls from dragging)?

推荐答案

您确实应该传递窗口句柄备忘录控件,但是您还需要收听发送到 it WM_DROPFILES 消息:

You should indeed pass the window handle of the memo control, but then you also need to listen to the WM_DROPFILES message sent to it:

unit Unit5;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellAPI;

type
  TMemo = class(StdCtrls.TMemo)
  protected
    procedure WMDropFiles(var Message: TWMDropFiles); message WM_DROPFILES;
    procedure CreateWnd; override;
    procedure DestroyWnd; override;
  end;

  TForm5 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form5: TForm5;

implementation

{$R *.dfm}

procedure TForm5.FormCreate(Sender: TObject);
begin
end;

{ TMemo }

procedure TMemo.CreateWnd;
begin
  inherited;
  DragAcceptFiles(Handle, true);
end;

procedure TMemo.DestroyWnd;
begin
  DragAcceptFiles(Handle, false);
  inherited;
end;

procedure TMemo.WMDropFiles(var Message: TWMDropFiles);
var
  c: integer;
  fn: array[0..MAX_PATH-1] of char;
begin

  c := DragQueryFile(Message.Drop, $FFFFFFFF, fn, MAX_PATH);

  if c <> 1 then
  begin
    MessageBox(Handle, 'Too many files.', 'Drag and drop error', MB_ICONERROR);
    Exit;
  end;

  if DragQueryFile(Message.Drop, 0, fn, MAX_PATH) = 0 then Exit;

  Text := fn;

end;


end.

以上示例仅接受单个文件删除。文件名将放在备忘控件中。但是您也可以允许删除多个选择:

The example above only accept a single file dropped. The file name will be put in the memo control. But you can also allow a multiple selection to be dropped:

var
c:整数;
fn:数组char [0..MAX_PATH-1];
i:整数;
开始

var c: integer; fn: array[0..MAX_PATH-1] of char; i: Integer; begin

c := DragQueryFile(Message.Drop, $FFFFFFFF, fn, MAX_PATH);

Clear;
for i := 0 to c - 1 do
begin
  if DragQueryFile(Message.Drop, i, fn, MAX_PATH) = 0 then Exit;
  Lines.Add(fn);
end;

这篇关于如何允许在Delphi中拖动文件以用于特定控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆