DragDropHelper.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Interop;
  9. namespace PDF_Master.Helper
  10. {
  11. /// <summary>
  12. /// 显示拖拽时图片的辅助类
  13. /// </summary>
  14. public static class DragDropHelper
  15. {
  16. private static IDropTargetHelper helper;
  17. /// <summary>
  18. /// 拖拽相关操作的实例
  19. /// </summary>
  20. public static IDropTargetHelper Helper
  21. {
  22. get
  23. {
  24. if (helper == null)
  25. {
  26. helper = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("4657278A-411B-11D2-839A-00C04FD918D0"))) as IDropTargetHelper;
  27. }
  28. return helper;
  29. }
  30. set { helper = value; }
  31. }
  32. public static void DragEnter(FrameworkElement element, DragEventArgs e)
  33. {
  34. try
  35. {
  36. Point iconPoint = e.GetPosition(element);
  37. Helper?.DragEnter(new WindowInteropHelper(Window.GetWindow(element)).Handle, (System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
  38. }
  39. catch { }
  40. }
  41. public static void DragOver(FrameworkElement element, DragEventArgs e)
  42. {
  43. try
  44. {
  45. Point iconPoint = e.GetPosition(element);
  46. Helper?.DragOver(ref iconPoint, e.Effects);
  47. }
  48. catch { }
  49. }
  50. public static void DragLeave()
  51. {
  52. try
  53. {
  54. Helper?.DragLeave();
  55. }
  56. catch { }
  57. }
  58. public static void Drop(FrameworkElement element, DragEventArgs e)
  59. {
  60. try
  61. {
  62. Point iconPoint = e.GetPosition(element);
  63. Helper?.Drop((System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
  64. }
  65. catch { }
  66. }
  67. }
  68. /// <summary>
  69. /// 系统设置拖拽时图片的接口
  70. /// </summary>
  71. [ComImport]
  72. [GuidAttribute("4657278B-411B-11d2-839A-00C04FD918D0")]
  73. [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
  74. public interface IDropTargetHelper
  75. {
  76. // Notifies the drag-image manager that the drop target's IDropTarget::DragEnter method has been called
  77. [PreserveSig]
  78. Int32 DragEnter(IntPtr hwndTarget, System.Runtime.InteropServices.ComTypes.IDataObject pDataObject, ref Point ppt, DragDropEffects dwEffect);
  79. // Notifies the drag-image manager that the drop target's IDropTarget::DragLeave method has been called
  80. [PreserveSig]
  81. Int32 DragLeave();
  82. // Notifies the drag-image manager that the drop target's IDropTarget::DragOver method has been called
  83. [PreserveSig]
  84. Int32 DragOver(ref Point ppt, DragDropEffects dwEffect);
  85. // Notifies the drag-image manager that the drop target's IDropTarget::Drop method has been called
  86. [PreserveSig]
  87. Int32 Drop(System.Runtime.InteropServices.ComTypes.IDataObject pDataObject, ref Point ppt, DragDropEffects dwEffect);
  88. // Notifies the drag-image manager to show or hide the drag image
  89. [PreserveSig]
  90. Int32 Show(bool fShow);
  91. }
  92. }