2011-03-26 60 views
0

我在我的Windows应用程序中使用.net树视图控件。我已经实现了可以正常工作的拖放功能。现在,我想要显示正在拖动的节点的文本/自定义图像及其图像,就像我们在窗口上拖动文件夹时看到的那样,即我们看到文件夹的淡出图像跟随着光标,直到发生拖放。将n拖放到.net树视图

如何在.net winform应用程序中执行此操作。

感谢, Omky

+0

阅读此:http://blog.stackoverflow.com/2010/10/vote-early-vote-often/ – 2011-03-26 07:51:10

回答

4

这里有一个链接几篇文章中,首先说明如何。
http://blogs.msdn.com/b/adamroot/archive/2008/02/19/shell-style-drag-and-drop-in-net-wpf-and-winforms.aspx

下面是使拖动效果起作用的最低限度。

using System; 
using System.Drawing; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
using ComIDataObject = System.Runtime.InteropServices.ComTypes.IDataObject; 

public static class DragDropEngine 
{ 
     public static void ProcessDragEnter(DragEventArgs e) 
     { 
      Point point = Cursor.Position; 
      WindowsPoint winpoint; 
      winpoint.X = point.X; 
      winpoint.Y = point.Y; 
      IDropTargetHelper dropHelper = (IDropTargetHelper)new DragDropHelper(); 
      dropHelper.DragEnter(IntPtr.Zero, (ComIDataObject)e.Data, 
        ref winpoint, (int)e.Effect); 
     } 
     public static void ProcessDragDrop(DragEventArgs e) 
     { 
      Point point = Cursor.Position; 
      WindowsPoint winpoint; 
      winpoint.X = point.X; 
      winpoint.Y = point.Y; 
      IDropTargetHelper dropHelper = (IDropTargetHelper)new DragDropHelper(); 
      dropHelper.Drop((ComIDataObject)e.Data, ref winpoint, (int)e.Effect); 
     } 
     public static void ProcessDragOver(DragEventArgs e) 
     { 
      Point point = Cursor.Position; 
      WindowsPoint winpoint; 
      winpoint.X = point.X; 
      winpoint.Y = point.Y; 
      IDropTargetHelper dropHelper = (IDropTargetHelper)new DragDropHelper(); 
      dropHelper.DragOver(ref winpoint, (int)e.Effect); 
     } 
     public static void ProcessDragLeave(EventArgs e) 
     { 
      IDropTargetHelper dropHelper = (IDropTargetHelper)new DragDropHelper(); 
      dropHelper.DragLeave(); 
     } 
} 
[ComImport] 
[Guid("4657278A-411B-11d2-839A-00C04FD918D0")] 
public class DragDropHelper 
{ 
} 
[ComVisible(true)] 
[ComImport] 
[Guid("4657278B-411B-11D2-839A-00C04FD918D0")] 
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
public interface IDropTargetHelper 
{ 
     void DragEnter(
      [In] IntPtr hwndTarget, 
      [In, MarshalAs(UnmanagedType.Interface)] 
      System.Runtime.InteropServices.ComTypes.IDataObject dataObject, 
      [In] ref WindowsPoint pt, 
      [In] int effect); 
     void DragLeave(); 
     void DragOver(
      [In] ref WindowsPoint pt, 
      [In] int effect); 
     void Drop(
      [In, MarshalAs(UnmanagedType.Interface)] 
      System.Runtime.InteropServices.ComTypes.IDataObject dataObject, 
      [In] ref WindowsPoint pt, 
      [In] int effect); 
     void Show(
      [In] bool show); 
} 
[StructLayout(LayoutKind.Sequential)] 
public struct WindowsPoint 
{ 
     public int X; 
     public int Y; 
} 
+0

感谢“bricklayer137”。文章和链接发现非常有用。 – Omkar 2011-03-27 11:04:11