ClassicFileDialog.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using SmartCoalApplication.Base;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace SmartCoalApplication.SystemLayer
  10. {
  11. public abstract class ClassicFileDialog : IFileDialog
  12. {
  13. private FileDialog fileDialog;
  14. protected FileDialog FileDialog
  15. {
  16. get
  17. {
  18. return this.fileDialog;
  19. }
  20. }
  21. public bool CheckPathExists
  22. {
  23. get
  24. {
  25. return this.fileDialog.CheckPathExists;
  26. }
  27. set
  28. {
  29. this.fileDialog.CheckPathExists = value;
  30. }
  31. }
  32. public bool DereferenceLinks
  33. {
  34. get
  35. {
  36. return this.fileDialog.DereferenceLinks;
  37. }
  38. set
  39. {
  40. this.fileDialog.DereferenceLinks = value;
  41. }
  42. }
  43. public string Filter
  44. {
  45. get
  46. {
  47. return this.fileDialog.Filter;
  48. }
  49. set
  50. {
  51. this.fileDialog.Filter = value;
  52. }
  53. }
  54. public int FilterIndex
  55. {
  56. get
  57. {
  58. return this.fileDialog.FilterIndex;
  59. }
  60. set
  61. {
  62. this.fileDialog.FilterIndex = value;
  63. }
  64. }
  65. public string InitialDirectory
  66. {
  67. get
  68. {
  69. return this.fileDialog.InitialDirectory;
  70. }
  71. set
  72. {
  73. this.fileDialog.InitialDirectory = value;
  74. }
  75. }
  76. public string Title
  77. {
  78. set
  79. {
  80. this.fileDialog.Title = value;
  81. }
  82. }
  83. private static void EnableThumbnailView(FileDialog ofd)
  84. {
  85. try
  86. {
  87. Type ofdType = typeof(FileDialog);
  88. FieldInfo fi = ofdType.GetField("dialogHWnd", BindingFlags.Instance | BindingFlags.NonPublic);
  89. if (fi != null)
  90. {
  91. object dialogHWndObject = fi.GetValue(ofd);
  92. IntPtr dialogHWnd = (IntPtr)dialogHWndObject;
  93. IntPtr hwndLV = SafeNativeMethods.FindWindowExW(dialogHWnd, IntPtr.Zero, "SHELLDLL_DefView", null);
  94. if (hwndLV != IntPtr.Zero)
  95. {
  96. SafeNativeMethods.SendMessageW(hwndLV, NativeConstants.WM_COMMAND, new IntPtr(NativeConstants.SHVIEW_THUMBNAIL), IntPtr.Zero);
  97. }
  98. }
  99. }
  100. catch (Exception)
  101. {
  102. // Ignore.
  103. }
  104. }
  105. public DialogResult ShowDialog(IWin32Window owner, IFileDialogUICallbacks uiCallbacks)
  106. {
  107. Control ownerAsControl = owner as Control;
  108. if (uiCallbacks == null)
  109. {
  110. throw new ArgumentNullException("uiCallbacks");
  111. }
  112. Cursor.Current = Cursors.WaitCursor;
  113. if ((Control.ModifierKeys & Keys.Shift) != 0)
  114. {
  115. UI.InvokeThroughModalTrampoline(
  116. owner,
  117. delegate (IWin32Window modalOwner)
  118. {
  119. while ((Control.ModifierKeys & Keys.Shift) != 0)
  120. {
  121. System.Threading.Thread.Sleep(1);
  122. Application.DoEvents();
  123. }
  124. });
  125. }
  126. Cursor.Current = Cursors.Default;
  127. DialogResult result = DialogResult.Cancel;
  128. UI.InvokeThroughModalTrampoline(
  129. owner,
  130. delegate (IWin32Window modalOwner)
  131. {
  132. if (ownerAsControl != null && ownerAsControl.IsHandleCreated)
  133. {
  134. ownerAsControl.BeginInvoke(new Procedure<FileDialog>(EnableThumbnailView), new object[] { this.fileDialog });
  135. }
  136. result = this.fileDialog.ShowDialog(modalOwner);
  137. });
  138. return result;
  139. }
  140. protected ClassicFileDialog(FileDialog fileDialog)
  141. {
  142. this.fileDialog = fileDialog;
  143. this.fileDialog.RestoreDirectory = true;
  144. this.fileDialog.ShowHelp = false;
  145. this.fileDialog.ValidateNames = true;
  146. }
  147. ~ClassicFileDialog()
  148. {
  149. Dispose(false);
  150. }
  151. public void Dispose()
  152. {
  153. Dispose(true);
  154. GC.SuppressFinalize(this);
  155. }
  156. protected virtual void Dispose(bool disposing)
  157. {
  158. if (disposing)
  159. {
  160. if (this.fileDialog != null)
  161. {
  162. this.fileDialog.Dispose();
  163. this.fileDialog = null;
  164. }
  165. }
  166. }
  167. }
  168. }