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

半世苍凉 提交于 2019-12-10 10:37:55

问题


This topic obviously has been hitted over and over again here, but now I just run out of options from my point of view.

OS: Windows XP SP3

So, here is Drag and Drop example for RichEdit I use in my app:

procedure TForm1.AcceptFiles( var msg : TMessage ); // or TWMDROPFILES
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, // or msg.Drop
                           $FFFFFFFF,
                           acFileName,
                           cnMaxFileNameLen );

  // query Windows one at a time for the file name
  for i := 0 to nCount-1 do
  begin
    DragQueryFile( msg.WParam, { or msg.Drop} 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 ); // or msg.Drop
end;

Problem is that after some recent changes ( unforutinetly I do not use any SVN so I cannot track which commit was introducing this issue ) Drag and Drop do not work any more.

I have run breakpoints without success in every event that might be somehow related ( called ):

RichEditMouseOver;

RichEditChange;

FormClick;

My app is processing these WM's:

procedure WMDropFiles(var Msg: TWMDROPFILES); message WM_DROPFILES;

procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;

procedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA;

procedure WMGetMinMaxInfo(var AMsg: TWMGetMinMaxInfo); message WM_GETMINMAXINFO;

procedure CMDialogKey(var Msg: TCMDialogKey ); message CM_DIALOGKEY;

On blank project with TRichEdit on form - all is working OK.

Also tried changing DragAcceptFiles() Form1.Handle to RichEdit.Handle - still no luck.

When echo'ing nCount and acFileName parameters, acFileName do not have File Path of Dragged file ... Why????

Currently I just have no clue what makes the acFileName parameter losing Dragged files path. Could you suggest where problem is hiding?


回答1:


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;



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4345643/drag-and-drop-does-not-work-anymore-in-my-project-in-delphi

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