开发者

Drag n Drop in .net Treeview

开发者 https://www.devze.com 2023-02-20 11:24 出处:网络
I am using .net tree view control in my windows application. I have implemented the drag drop functionality which is working fine. Now, I want to show the text/custom imageof the node that is being dr

I am using .net tree view control in my windows application. I have implemented the drag drop functionality which is working fine. Now, I want to show the text/custom image of the node that is being drag and its image much like we see 开发者_JAVA百科when we drag the folder on windows i.e. we see fade image of folder follows the cursor till drop happen.

How to do this in .net winform app.

Thanks, Omky


Here's a link to first of several articles that explains how.
http://blogs.msdn.com/b/adamroot/archive/2008/02/19/shell-style-drag-and-drop-in-net-wpf-and-winforms.aspx

Below is a bare minimum to get drag effect to work.

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

精彩评论

暂无评论...
验证码 换一张
取 消