拖放在我的delphi项目中不再工作 [英] Drag and Drop does not work anymore in my project in delphi

查看:188
本文介绍了拖放在我的delphi项目中不再工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个话题显然已经在这里重复出现了,但是现在我从我的角度来看,我只是用完了选项。

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

操作系统: / strong> Windows XP SP3

OS: Windows XP SP3

所以,这里是 RichEdit的拖放示例我在我的应用程序中使用:

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;

问题是,在最近的一些变化之后(unforutinetly我不使用任何SVN,所以我无法跟踪哪个提交引入这个问题)拖放不再工作。

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;

我的应用程序正在处理这些WM:

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;

在表单上使用TRichEdit的空白项目 - 一切正常。

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

还尝试更改 DragAcceptFiles() Form1.Handle to RichEdit.Handle - 仍然没有运气。

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

当回显 nCount acFileName 参数,acFileName没有拖动文件的文件路径...为什么????

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

目前我只是不知道什么使acFileName参数丢失Dragged文件路径。你可以建议哪里存在问题吗?

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

推荐答案

恐怕我不是一个专家,如何实际拖放文件作品。所以我不能通过你的代码找出错误。

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.

我可以做的是给你我使用的代码。它现在适用于我在XP,Vista和Windows 7下的Delphi 2009中。当我以前在Windows 98和Windows XP上使用Delphi 4时也可以。

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.

也许你可以弄清楚使用这个代码的代码有什么问题,或者您可能想尝试使用或修改此代码。它最初来自于Delphi 3 - 用户界面设计,第169页 - 第171页。

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;

这篇关于拖放在我的delphi项目中不再工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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