如何将文件从 Explorer Shell 拖放到 Delphi 应用程序中的 VirtualTreeView 控件中? [英] How do you drag and drop a file from Explorer Shell into a VirtualTreeView control in a Delphi application?

查看:19
本文介绍了如何将文件从 Explorer Shell 拖放到 Delphi 应用程序中的 VirtualTreeView 控件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mike Lischke 在 VirtualTreeView 中提供了广泛的拖放支持,我正在使用 TVirtualStringTree,它有一些拖放事件,但我不知道如何让它接受外壳拖放 -并将一些文件从 Windows 资源管理器外壳拖放到我的应用程序中.我想加载文件,当它们被拖到放置控件上时.

There is extensive drag and drop support in VirtualTreeView by Mike Lischke, and I am using TVirtualStringTree, which has some on-drag-and-drop events, but I can not figure out how to get it to accept a shell drag-and-drop of some files from the windows explorer shell, into my application. I want to load the files, when they are dragged onto the drop control.

我尝试使用来自 Anders Melander 的第三方代码集来处理拖放,但由于 VirtualTreeView 已经将自己注册为放置目标,我无法使用它.

I tried using a third-party set of code from Anders Melander, to handle drag and drop, but because VirtualTreeView already registers itself as a drop target, I can't use that.

我找到了一个简单的解决方法:在 VT.TreeOptions.MiscOptions 中关闭 toAcceptOLEDrop.如果有人知道如何使用 VirtualTreeView 而不使用第三方 OLE-shell-drag-drop 库并使用其广泛的 OLE 拖放支持来提取从 Shell 中拖入的文件名列表,那将是很酷的.

edit: I found a simple workaround: Turn off toAcceptOLEDrop in VT.TreeOptions.MiscOptions. It would be cool if anybody knows a way to use VirtualTreeView without using a third party OLE-shell-drag-drop library and using its extensive OLE drag/drop support to extract a list of filenames dragged in from the Shell.

推荐答案

我的实现(在 Delphi 2010 上运行良好.必须添加 ActiveX、ShellApi 才能编译使用):

My implementation (Works very well with Delphi 2010. Must add ActiveX, ShellApi to uses to compile):

procedure TfMain.vstTreeDragDrop(Sender: TBaseVirtualTree; Source: TObject;
  DataObject: IDataObject; Formats: TFormatArray; Shift: TShiftState;
  Pt: TPoint; var Effect: Integer; Mode: TDropMode);
var
  I, j: Integer;
  MyList: TStringList;
  AttachMode: TVTNodeAttachMode;
begin
  if Mode = dmOnNode then
    AttachMode := amInsertBefore
  else if Mode = dmAbove then
    AttachMode := amInsertBefore
  else if Mode = dmBelow then
    AttachMode := amInsertAfter
  else
    AttachMode := amAddChildLast;

  MyList := TStringList.Create;
  try
    for i := 0 to High(formats) - 1 do
    begin
      if (Formats[i] = CF_HDROP) then
      begin
        GetFileListFromObj(DataObject, MyList);

        //here we have all filenames
        for j:=0 to MyList.Count - 1 do
        begin
          Sender.InsertNode(Sender.DropTargetNode, AttachMode);
        end; 
      end;  
    end;
  finally
    MyList.Free;
  end;
end;

procedure TfMain.GetFileListFromObj(const DataObj: IDataObject;
  FileList: TStringList);
var
  FmtEtc: TFormatEtc;                   // specifies required data format
  Medium: TStgMedium;                   // storage medium containing file list
  DroppedFileCount: Integer;            // number of dropped files
  I: Integer;                           // loops thru dropped files
  FileNameLength: Integer;              // length of a dropped file name
  FileName: string;                 // name of a dropped file
begin
  // Get required storage medium from data object
  FmtEtc.cfFormat := CF_HDROP;
  FmtEtc.ptd := nil;
  FmtEtc.dwAspect := DVASPECT_CONTENT;
  FmtEtc.lindex := -1;
  FmtEtc.tymed := TYMED_HGLOBAL;
  OleCheck(DataObj.GetData(FmtEtc, Medium));
  try
    try
      // Get count of files dropped
      DroppedFileCount := DragQueryFile(
        Medium.hGlobal, $FFFFFFFF, nil, 0
        );
      // Get name of each file dropped and process it
      for I := 0 to Pred(DroppedFileCount) do
        begin
          // get length of file name, then name itself
          FileNameLength := DragQueryFile(Medium.hGlobal, I, nil, 0);
          SetLength(FileName, FileNameLength);
          DragQueryFileW(
            Medium.hGlobal, I, PWideChar(FileName), FileNameLength + 1
            );
          // add file name to list
          FileList.Append(FileName);
        end;
    finally
      // Tidy up - release the drop handle
      // don't use DropH again after this
      DragFinish(Medium.hGlobal);
    end;
  finally
    ReleaseStgMedium(Medium);
  end;

end;

这篇关于如何将文件从 Explorer Shell 拖放到 Delphi 应用程序中的 VirtualTreeView 控件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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