在 .net 树视图中拖放

发布于 2024-10-27 17:30:21 字数 187 浏览 0 评论 0原文

我在 Windows 应用程序中使用 .net 树视图控件。我已经实现了拖放功能,效果很好。现在,我想显示正在拖动的节点的文本/自定义图像及其图像,就像我们在 Windows 上拖动文件夹时看到的那样,即我们看到文件夹的淡入淡出图像跟随光标直到发生掉落。

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

谢谢, 奥姆基

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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2024-11-03 17:30:21

这是几篇文章中第一篇的链接,解释了如何操作。
http://blogs.msdn.com/b/adamroot/archive/2008/02/19/shell-style-drag-and-drop-in-net-wpf-and-winforms.aspx< /a>

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

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文