2

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?

lkessler
  • 19,819
  • 36
  • 132
  • 203
HX_unbanned
  • 583
  • 1
  • 15
  • 51

3 Answers3

4

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.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
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.

Gregor Brandt
  • 7,659
  • 38
  • 59
Allen Bauer
  • 16,657
  • 2
  • 56
  • 74
  • I am using WinXP, so I doubt file permissions and privilege levels are the cause. I drag file from project folders child folder. – HX_unbanned Dec 03 '10 at 17:49
  • 2
    Well, I tried ;-). I would suggest you create a scratch project and copy the code from the problem project and try and isolate it from other changes that may be causing the problem. If the same code works from the scratch project, then you can begin broadening the search. If it still fails, you at least now have a test-bed to make experimental changes without affecting your main application. – Allen Bauer Dec 03 '10 at 21:18
  • Thank you for giving tip ;) I will have to focus on this a bit later ... now I have to deal with TCanvas and GDI... – HX_unbanned Dec 04 '10 at 11:37
2

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;
lkessler
  • 19,819
  • 36
  • 132
  • 203
  • Thanks. I did not use this code, but I took very scrupulous look at code and found one bug. Thanks you! Besides - this is my honour to give you my points. – HX_unbanned Feb 01 '11 at 08:49
  • @HX: I felt for you. I remember times I've had some code that used to work but later just wouldn't work no matter what I tried, and I know how frustrating that is. What I needed then was assurance from someone else that it could still be made to work, and then a template just like this to give me some ideas of things to try. I'm glad this somehow led you to your solution. – lkessler Feb 01 '11 at 20:36