【发布时间】:2016-09-05 10:57:32
【问题描述】:
我正在尝试实现一个简单的拖放面板,用户可以在其中从 Windows 资源管理器中拖放文件。在我找到this Thread 之后,基本功能已经可以使用了。
现在我正在尝试更改面板的颜色,而用户正在将文件拖到面板上。我尝试使用 OnDragOver,但没有任何反应。我做错了什么?
这是我当前的代码:
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ShellApi,
Vcl.ExtCtrls, Vcl.Imaging.pngimage;
type
TPanel = class(Vcl.ExtCtrls.TPanel)
protected
procedure WMDropFiles(var Message: TWMDropFiles); message WM_DROPFILES;
procedure CreateWnd; override;
procedure DestroyWnd; override;
end;
TfrmMain = class(TForm)
panFileDrop: TPanel;
lblFileName: TLabel;
procedure panFileDropDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TPanel.CreateWnd;
begin
inherited;
DragAcceptFiles(Handle, true);
end;
procedure TPanel.DestroyWnd;
begin
DragAcceptFiles(Handle, false);
inherited;
end;
procedure TPanel.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;
frmMain.lblFileName.Caption := fn;
end;
procedure TfrmMain.panFileDropDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
panFileDrop.Color := $00d4d3d2;
end;
end.
【问题讨论】:
-
你调试过DragOver事件吗?
-
是的,我使用了 ShowMessage 但它没有显示出来。
-
showmessage 不是一个好的调试器,在 onDragOver 事件的第一行设置一个断点并检查你是否到达那里,如果你这样做了,然后调试那里的每一行代码。如果您不这样做,那么我们看不到的某些代码中还有其他问题
-
我会在我回到办公室时尝试。但我的做法对吗?
-
OnDragOver是一个 VCL 鼠标拖动事件,与从资源管理器中拖动 shell 数据完全无关。如果你想接收拖动事件,那么用DragAcceptFiles注册WM_DROPFILES是不够的,因为这只会给你最终的放置事件,没有拖动。您需要通过实现IDropTarget接口来关注Transferring Shell Objects with Drag-and-Drop and the Clipboard。
标签: delphi drag-and-drop