Drag and Drop does not work anymore in my project in delphi

点点圈 提交于 2019-12-06 14:26:13

I'm afraid I'm not an expert on how Dragging and Dropping files actually works. So I can't go through your code an figure out what's wrong.

What I can do is give you the code that I use. It works for me now under Delphi 2009 on XP, Vista and Windows 7. It also worked when I was previously using Delphi 4 on Windows 98 and Windows XP.

Maybe you can figure out what's wrong in your code using this, or you might want to try using or adapting this code. It is originally from the book: "Delphi 3 - User Interface Design", pages 169 - 171.

If I've left out an important routine, let me know in a comment and I'll edit my answer to include it.

type
  TMainForm = class(TForm)
    procedure WMDropFiles(var WinMsg: TMessage);
              message wm_DropFiles;
    procedure AppMessageHandler(var Msg: TMsg; var Handled: Boolean);

procedure TMainForm.FormShow(Sender: TObject);
begin
  DragAcceptFiles(Handle, true);
  Application.OnMessage := AppMessageHandler;
end;

procedure TLogoAppForm.WMDropFiles(var WinMsg: TMessage);
const
  BufSize = 255;
var
  TempStr : array[0..BufSize] of Char;
  NumDroppedFiles, I: integer;
  Filenames: TStringList;
begin
  NumDroppedFiles := DragQueryFile(TWMDropFiles(WinMsg).Drop, $ffffffff, nil, 0);
  if NumDroppedFiles >= 1 then begin
    Filenames := TStringList.Create;
    for I := 0 to NumDroppedFiles - 1 do begin
      DragQueryFile(TWMDropFiles(WinMsg).Drop, I, TempStr, BufSize);
      Filenames.Add(TempStr);
    end;
    OpenFiles(Filenames, '');
    Filenames.Free;
  end;
  DragFinish(TWMDropFiles(WinMsg).Drop);
  WinMsg.Result := 0;
end;

procedure TLogoAppForm.AppMessageHandler(var Msg: TMsg; var Handled: Boolean);
begin
  if (Msg.Message = WM_DropFiles) and IsIconic(Application.Handle) then begin
    Perform(Msg.Message, Msg.Wparam, Msg.lParam);
    Handled := true;
  end
end;

Extracted from the official Microsoft support forum:

A possible reason for the not being able to drag & drop files to the application could be UAC integrity level (IL). With UAC enabled in Vista or Win7, drag and drop is not allowed to happen from low IL process to high IL process by default. Please check whether your application is elevated (run as admin) when the problem happens.

If you don't want to disable UAC, you could try just disabling UIPI (User Interface Privilege Isolation).

Open regedit and go to: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System

Add a new DWORD (32-bit) Value called EnableUIPI and set it to 0.

Restart the machine and see if it behaves as you want it to.

Allen Bauer

I'm going to take a stab at psychic debugging here; You say it "doesn't work any more" implying that it worked at some point. While you haven't mentioned which OS you're using, I'm also going to divine that you're using Windows Vista or Windows 7 (or a correlating Server version).

What I suspect is happening is that your application is running as a different user or privilege level than the shell. Windows doesn't allow drag (especially file drag) data to go from an application at one privilege level to another. If you're running this from the IDE, and the IDE is running as Administrator, then the spawned processes are also running at that level.

Check to make sure your application is actually running as the logged in user. I've been bitten by this too many times to count and is so subtle that it is never immediately obvious as to what is going on.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!