DragDropManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #region Using directives
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Diagnostics;
  5. #endregion
  6. namespace DocToolkit
  7. {
  8. /// <summary>
  9. /// DragDropManager class allows to open files dropped from
  10. /// Windows Explorer in Windows Form application.
  11. ///
  12. /// Using:
  13. /// 1) Write function which opens file selected from MRU:
  14. ///
  15. /// private void dragDropManager_FileDroppedEvent(object sender, FileDroppedEventArgs e)
  16. /// {
  17. /// // open file(s) from e.FileArray:
  18. /// // e.FileArray.GetValue(0).ToString() ...
  19. /// }
  20. ///
  21. /// 2) Add member of this class to the parent form:
  22. ///
  23. /// private DragDropManager dragDropManager;
  24. ///
  25. /// 3) Create class instance in parent form initialization code:
  26. ///
  27. /// dragDropManager = new DragDropManager(this);
  28. /// dragDropManager.FileDroppedEvent += dragDropManager_FileDroppedEvent;
  29. ///
  30. /// </summary>
  31. public class DragDropManager
  32. {
  33. private Form frmOwner; // reference to owner form
  34. // Event raised when drops file(s) to the form
  35. public event FileDroppedEventHandler FileDroppedEvent;
  36. public DragDropManager(Form owner)
  37. {
  38. frmOwner = owner;
  39. // ensure that parent form allows dropping
  40. frmOwner.AllowDrop = true;
  41. // subscribe to parent form's drag-drop events
  42. frmOwner.DragEnter += OnDragEnter;
  43. frmOwner.DragDrop += OnDragDrop;
  44. }
  45. /// <summary>
  46. /// Handle parent form DragEnter event
  47. /// </summary>
  48. /// <param name="sender"></param>
  49. /// <param name="e"></param>
  50. private void OnDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
  51. {
  52. // If file is dragged, show cursor "Drop allowed"
  53. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  54. e.Effect = DragDropEffects.Copy;
  55. else
  56. e.Effect = DragDropEffects.None;
  57. }
  58. /// <summary>
  59. /// Handle parent form DragDrop event
  60. /// </summary>
  61. /// <param name="sender"></param>
  62. /// <param name="e"></param>
  63. private void OnDragDrop(object sender, System.Windows.Forms.DragEventArgs e)
  64. {
  65. // When file(s) are dragged from Explorer to the form, IDataObject
  66. // contains array of file names. If one file is dragged,
  67. // array contains one element.
  68. Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
  69. if (a != null)
  70. {
  71. if (FileDroppedEvent != null)
  72. {
  73. // Raise event asynchronously.
  74. // Explorer instance from which file is dropped is not responding
  75. // all the time when DragDrop handler is active, so we need to return
  76. // immidiately (especially if OpenFiles shows MessageBox).
  77. FileDroppedEvent.BeginInvoke(this, new FileDroppedEventArgs(a), null, null);
  78. frmOwner.Activate(); // in the case Explorer overlaps parent form
  79. }
  80. }
  81. // NOTE: exception handling is not used here.
  82. // Caller responsibility is to handle exceptions
  83. // in the function invoked by FileDroppedEvent.
  84. }
  85. }
  86. public delegate void FileDroppedEventHandler(object sender, FileDroppedEventArgs e);
  87. public class FileDroppedEventArgs : System.EventArgs
  88. {
  89. private Array fileArray;
  90. public FileDroppedEventArgs(Array array)
  91. {
  92. this.fileArray = array;
  93. }
  94. public Array FileArray
  95. {
  96. get
  97. {
  98. return fileArray;
  99. }
  100. }
  101. }
  102. }