FileDialogPlaces.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using PaintDotNet.SystemLayer.FileDlgExtenders.Win32Types;
  8. using System.ComponentModel;
  9. using Microsoft.Win32;
  10. namespace PaintDotNet.SystemLayer.FileDlgExtenders.FileDialogExtenders
  11. {
  12. public static class Extensions
  13. {
  14. #region extension Methods
  15. public static DialogResult ShowDialog(this FileDialog fdlg, FileDialogControlBase ctrl, IWin32Window owner) //where T : FileDialogControlBase, new()
  16. {
  17. ctrl.FileDlgType = (fdlg is SaveFileDialog) ? FileDialogType.SaveFileDlg : FileDialogType.OpenFileDlg;
  18. Form form = new Form();
  19. form.TopMost = true;
  20. form.Visible = false;
  21. DialogResult r = DialogResult.Ignore;
  22. form.Load += (s, e) =>
  23. {
  24. r = ctrl.ShowDialogExt(fdlg, form);
  25. form.Close();
  26. };
  27. form.ShowDialog();
  28. if (r == DialogResult.OK)
  29. return DialogResult.OK;
  30. else
  31. return DialogResult.Ignore;
  32. }
  33. #endregion
  34. }
  35. public static class FileDialogPlaces
  36. {
  37. private static readonly string TempKeyName = "TempPredefKey_" + Guid.NewGuid().ToString();
  38. private const string Key_PlacesBar = @"Software\Microsoft\Windows\CurrentVersion\Policies\ComDlg32\PlacesBar";
  39. private static RegistryKey _fakeKey;
  40. private static IntPtr _overriddenKey;
  41. private static object[] m_places;
  42. public static void SetPlaces(this FileDialog fd, object[] places)
  43. {
  44. if (fd == null || places == null)
  45. return;
  46. if (m_places == null)
  47. m_places = new object[places.GetLength(0)];
  48. for (int i = 0; i < m_places.GetLength(0); i++)
  49. {
  50. m_places[i] = places[i];
  51. }
  52. if (_fakeKey != null)
  53. ResetPlaces(fd);
  54. SetupFakeRegistryTree();
  55. if (fd != null)
  56. fd.Disposed += (object sender, EventArgs e) => { if (m_places != null && fd != null) ResetPlaces(fd); };
  57. }
  58. static FileDialogPlaces()
  59. {
  60. }
  61. static public void ResetPlaces(this FileDialog fd)
  62. {
  63. if (_overriddenKey != IntPtr.Zero)
  64. {
  65. ResetRegistry(_overriddenKey);
  66. _overriddenKey = IntPtr.Zero;
  67. }
  68. if (_fakeKey != null)
  69. {
  70. _fakeKey.Close();
  71. _fakeKey = null;
  72. }
  73. //delete the key tree
  74. Registry.CurrentUser.DeleteSubKeyTree(TempKeyName);
  75. m_places = null;
  76. }
  77. private static void SetupFakeRegistryTree()
  78. {
  79. try
  80. {
  81. _fakeKey = Registry.CurrentUser.CreateSubKey(TempKeyName);
  82. _overriddenKey = InitializeRegistry();
  83. // at this point, m_TempKeyName equals places key
  84. // write dynamic places here reading from Places
  85. RegistryKey reg = Registry.CurrentUser.CreateSubKey(Key_PlacesBar);
  86. for (int i = 0; i < m_places.GetLength(0); i++)
  87. {
  88. if (m_places[i] != null)
  89. {
  90. reg.SetValue("Place" + i.ToString(), m_places[i]);
  91. }
  92. }
  93. }
  94. catch
  95. {
  96. }
  97. }
  98. //public static IntPtr GetRegistryHandle(RegistryKey registryKey)
  99. //{
  100. // Type type = registryKey.GetType();
  101. // FieldInfo fieldInfo = type.GetField("hkey", BindingFlags.Instance | BindingFlags.NonPublic);
  102. // return (IntPtr)fieldInfo.GetValue(registryKey);
  103. //}
  104. static readonly UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u);
  105. private static IntPtr InitializeRegistry()
  106. {
  107. IntPtr hkMyCU;
  108. Win32Types.NativeMethods.RegCreateKeyW(HKEY_CURRENT_USER, TempKeyName, out hkMyCU);
  109. Win32Types.NativeMethods.RegOverridePredefKey(HKEY_CURRENT_USER, hkMyCU);
  110. return hkMyCU;
  111. }
  112. static void ResetRegistry(IntPtr hkMyCU)
  113. {
  114. try
  115. {
  116. Win32Types.NativeMethods.RegOverridePredefKey(HKEY_CURRENT_USER, IntPtr.Zero);
  117. Win32Types.NativeMethods.RegCloseKey(hkMyCU);
  118. }
  119. catch
  120. {
  121. }
  122. }
  123. }
  124. public enum Places
  125. {
  126. [Description("Desktop")]
  127. Desktop = 0,
  128. [Description("Internet Explorer ")]
  129. InternetExplorer = 1,
  130. [Description("Program Files")]
  131. Programs = 2,
  132. [Description("Control Panel")]
  133. ControlPanel = 3,
  134. [Description("Printers")]
  135. Printers = 4,
  136. [Description("My Documents")]
  137. MyDocuments = 5,
  138. [Description("Favorites")]
  139. Favorites = 6,
  140. [Description("Startup folder")]
  141. StartupFolder = 7,
  142. [Description("Recent Files")]
  143. RecentFiles = 8,
  144. [Description("Send To")]
  145. SendTo = 9,
  146. [Description("Recycle Bin")]
  147. RecycleBin = 0xa,
  148. [Description("Start menu")]
  149. StartMenu = 0xb,
  150. [Description("Logical My Documents")]
  151. Logical_MyDocuments = 0xc,
  152. [Description("My Music")]
  153. MyMusic = 0xd,
  154. [Description("My Videos")]
  155. MyVideos = 0xe,
  156. [Description("<user name>\\Desktop")]
  157. UserName_Desktop = 0x10,
  158. [Description("My Computer")]
  159. MyComputer = 0x11,
  160. [Description("My Network Places")]
  161. MyNetworkPlaces = 18,
  162. [Description("<user name>\nethood")]
  163. User_Name_Nethood = 0x13,
  164. [Description("Fonts")]
  165. Fonts = 0x14,
  166. [Description("All Users\\Start Menu")]
  167. All_Users_StartMenu = 0x16,
  168. [Description("All Users\\Start Menu\\Programs ")]
  169. All_Users_StartMenu_Programs = 0x17,
  170. [Description("All Users\\Startup")]
  171. All_Users_Startup = 0x18,
  172. [Description("All Users\\Desktop")]
  173. All_Users_Desktop = 0x19,
  174. [Description("<user name>\\Application Data ")]
  175. User_name_ApplicationData = 0x1a,
  176. [Description("<user name>\\PrintHood ")]
  177. User_Name_PrintHood = 0x1b,
  178. [Description("<user name>\\Local Settings\\Applicaiton Data (nonroaming)")]
  179. Local_ApplicaitonData = 0x1c,
  180. [Description("Nonlocalized common startup ")]
  181. NonlocalizedCommonStartup = 0x1e,
  182. [Description("")]
  183. CommonFavorites = 0x1f,
  184. [Description("Internet Cache ")]
  185. InternetCache = 0x20,
  186. [Description("Cookies ")]
  187. Cookies = 0x21,
  188. [Description("History")]
  189. History = 0x22,
  190. [Description("All Users\\Application Data ")]
  191. All_Users_ApplicationData = 0x23,
  192. [Description("Windows Directory")]
  193. WindowsDirectory = 0x24,
  194. [Description("System Directory")]
  195. SystemDirectory = 0x25,
  196. [Description("Program Files ")]
  197. ProgramFiles = 0x26,
  198. [Description("My Pictures ")]
  199. MyPictures = 0x27,
  200. [Description("USERPROFILE")]
  201. USERPROFILE = 0x28,
  202. [Description("system directory on RISC")]
  203. SYSTEN_RISC = 0x29,
  204. [Description("Program Files on RISC ")]
  205. Program_Files_RISC = 0x2a,
  206. [Description("Program Files\\Common")]
  207. Common = 0x2b,
  208. [Description("Program Files\\Common on RISC")]
  209. Common_RISC = 0x2c,
  210. [Description("All Users\\Templates ")]
  211. Templates = 0x2d,
  212. [Description("All Users\\Documents")]
  213. All_Users_Documents = 0x2e,
  214. [Description("All Users\\Start Menu\\Programs\\Administrative Tools")]
  215. AdministrativeTools = 0x2f,
  216. [Description("<user name>\\Start Menu\\Programs\\Administrative Tools")]
  217. USER_AdministrativeTools = 0x30,
  218. [Description("Network and Dial-up Connections")]
  219. Network_DialUp_Connections = 0x31,
  220. [Description("All Users\\My Music")]
  221. All_Users_MyMusic = 0x35,
  222. [Description("All Users\\My Pictures")]
  223. All_Users_MyPictures = 0x36,
  224. [Description("All Users\\My Video")]
  225. All_Users_MyVideo = 0x37,
  226. [Description("Resource Directory")]
  227. Resource = 0x38,
  228. [Description("Localized Resource Directory ")]
  229. Localized_Resource = 0x39,
  230. [Description("OEM specific apps")]
  231. OEM_Specific = 0x3a,
  232. [Description("USERPROFILE\\Local Settings\\Application Data\\Microsoft\\CD Burning")]
  233. CDBurning = 0x3b
  234. }
  235. }