ShellList.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. using BluePointLilac.Controls;
  2. using BluePointLilac.Methods;
  3. using ContextMenuManager.Controls.Interfaces;
  4. using Microsoft.Win32;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Windows.Forms;
  13. using System.Xml;
  14. namespace ContextMenuManager.Controls
  15. {
  16. sealed class ShellList : MyList
  17. {
  18. public const string MENUPATH_FILE = @"HKEY_CLASSES_ROOT\*";//文件
  19. public const string MENUPATH_FOLDER = @"HKEY_CLASSES_ROOT\Folder";//文件夹
  20. public const string MENUPATH_DIRECTORY = @"HKEY_CLASSES_ROOT\Directory";//目录
  21. public const string MENUPATH_BACKGROUND = @"HKEY_CLASSES_ROOT\Directory\Background";//目录背景
  22. public const string MENUPATH_DESKTOP = @"HKEY_CLASSES_ROOT\DesktopBackground";//桌面背景
  23. public const string MENUPATH_DRIVE = @"HKEY_CLASSES_ROOT\Drive";//磁盘分区
  24. public const string MENUPATH_ALLOBJECTS = @"HKEY_CLASSES_ROOT\AllFilesystemObjects";//所有对象
  25. public const string MENUPATH_COMPUTER = @"HKEY_CLASSES_ROOT\CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}";//此电脑
  26. public const string MENUPATH_RECYCLEBIN = @"HKEY_CLASSES_ROOT\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}";//回收站
  27. public const string MENUPATH_LIBRARY = @"HKEY_CLASSES_ROOT\LibraryFolder";//库
  28. public const string MENUPATH_LIBRARY_BACKGROUND = @"HKEY_CLASSES_ROOT\LibraryFolder\Background";//库背景
  29. public const string MENUPATH_LIBRARY_USER = @"HKEY_CLASSES_ROOT\UserLibraryFolder";//用户库
  30. public const string MENUPATH_UWPLNK = @"HKEY_CLASSES_ROOT\Launcher.ImmersiveApplication";//UWP快捷方式
  31. public const string MENUPATH_UNKNOWN = @"HKEY_CLASSES_ROOT\Unknown";//未知格式
  32. public const string SYSFILEASSPATH = @"HKEY_CLASSES_ROOT\SystemFileAssociations";//系统扩展名注册表父项路径
  33. private const string LASTKEYPATH = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit";//上次打开的注册表项路径记录
  34. public enum Scenes
  35. {
  36. File, Folder, Directory, Background, Desktop, Drive, AllObjects, Computer, RecycleBin, Library,
  37. LnkFile, UwpLnk, ExeFile, UnknownType, CustomExtension, PerceivedType, DirectoryType,
  38. CommandStore, DragDrop, CustomRegPath, MenuAnalysis, CustomExtensionPerceivedType
  39. }
  40. private static readonly string[] DirectoryTypes =
  41. {
  42. "Document", "Image", "Video", "Audio"
  43. };
  44. private static readonly string[] PerceivedTypes =
  45. {
  46. null, "Text", "Document", "Image",
  47. "Video", "Audio", "Compressed", "System"
  48. };
  49. private static readonly string[] FileObjectTypes =
  50. {
  51. AppString.SideBar.File,
  52. AppString.SideBar.Directory
  53. };
  54. private static readonly string[] PerceivedTypeNames =
  55. {
  56. AppString.Dialog.NoPerceivedType, AppString.Dialog.TextFile, AppString.Dialog.DocumentFile, AppString.Dialog.ImageFile,
  57. AppString.Dialog.VideoFile, AppString.Dialog.AudioFile, AppString.Dialog.CompressedFile, AppString.Dialog.SystemFile
  58. };
  59. private static readonly string[] DirectoryTypeNames =
  60. {
  61. AppString.Dialog.DocumentDirectory, AppString.Dialog.ImageDirectory,
  62. AppString.Dialog.VideoDirectory, AppString.Dialog.AudioDirectory
  63. };
  64. private static string GetDirectoryTypeName(string directoryType)
  65. {
  66. if(directoryType != null)
  67. {
  68. for(int i = 0; i < DirectoryTypes.Length; i++)
  69. {
  70. if(directoryType.Equals(DirectoryTypes[i], StringComparison.OrdinalIgnoreCase))
  71. {
  72. return DirectoryTypeNames[i];
  73. }
  74. }
  75. }
  76. return null;
  77. }
  78. private static string GetPerceivedTypeName(string perceivedType)
  79. {
  80. int index = 0;
  81. if(perceivedType != null)
  82. {
  83. for(int i = 1; i < PerceivedTypes.Length; i++)
  84. {
  85. if(perceivedType.Equals(PerceivedTypes[i], StringComparison.OrdinalIgnoreCase)) index = i;
  86. }
  87. }
  88. return PerceivedTypeNames[index];
  89. }
  90. private static readonly string[] DropEffectPaths =
  91. {
  92. MENUPATH_FILE, MENUPATH_ALLOBJECTS,
  93. MENUPATH_FOLDER, MENUPATH_DIRECTORY
  94. };
  95. private static readonly string[] DropEffectNames =
  96. {
  97. AppString.Dialog.DefaultDropEffect, AppString.Dialog.CopyDropEffect,
  98. AppString.Dialog.MoveDropEffect, AppString.Dialog.CreateLinkDropEffect
  99. };
  100. private enum DropEffect { Default = 0, Copy = 1, Move = 2, CreateLink = 4 }
  101. private static DropEffect DefaultDropEffect
  102. {
  103. get
  104. {
  105. foreach(string path in DropEffectPaths)
  106. {
  107. object value = Registry.GetValue(path, "DefaultDropEffect", null);
  108. if(value != null)
  109. {
  110. switch(value)
  111. {
  112. case 1:
  113. return DropEffect.Copy;
  114. case 2:
  115. return DropEffect.Move;
  116. case 4:
  117. return DropEffect.CreateLink;
  118. }
  119. }
  120. }
  121. return DropEffect.Default;
  122. }
  123. set
  124. {
  125. object data;
  126. switch(value)
  127. {
  128. case DropEffect.Copy:
  129. data = 1; break;
  130. case DropEffect.Move:
  131. data = 2; break;
  132. case DropEffect.CreateLink:
  133. data = 4; break;
  134. default:
  135. data = 0; break;
  136. }
  137. foreach(string path in DropEffectPaths)
  138. {
  139. Registry.SetValue(path, "DefaultDropEffect", data, RegistryValueKind.DWord);
  140. }
  141. }
  142. }
  143. private static string GetDropEffectName()
  144. {
  145. switch(DefaultDropEffect)
  146. {
  147. case DropEffect.Copy:
  148. return DropEffectNames[1];
  149. case DropEffect.Move:
  150. return DropEffectNames[2];
  151. case DropEffect.CreateLink:
  152. return DropEffectNames[3];
  153. default:
  154. return DropEffectNames[0];
  155. }
  156. }
  157. private static string CurrentExtension = null;
  158. private static string CurrentDirectoryType = null;
  159. private static string CurrentPerceivedType = null;
  160. public static string CurrentCustomRegPath = null;
  161. public static string CurrentFileObjectPath = null;
  162. private static string CurrentExtensionPerceivedType
  163. {
  164. get => Registry.GetValue(CurrentExtensionPath, "PerceivedType", null)?.ToString();
  165. set
  166. {
  167. if(value == null) RegistryEx.DeleteValue(CurrentExtensionPath, "PerceivedType");
  168. else Registry.SetValue(CurrentExtensionPath, "PerceivedType", value, RegistryValueKind.String);
  169. }
  170. }
  171. private static string GetShellPath(string scenePath) => $@"{scenePath}\shell";
  172. private static string GetShellExPath(string scenePath) => $@"{scenePath}\ShellEx";
  173. private static string GetSysAssExtPath(string typeName) => typeName != null ? $@"{SYSFILEASSPATH}\{typeName}" : null;
  174. private static string GetOpenModePath(string extension) => extension != null ? $@"{RegistryEx.CLASSESROOT}\{FileExtension.GetOpenMode(extension)}" : null;
  175. private static string CurrentExtensionPath => $@"{RegistryEx.CLASSESROOT}\{CurrentExtension}";
  176. public Scenes Scene { get; set; }
  177. public void LoadItems()
  178. {
  179. string scenePath = null;
  180. switch(Scene)
  181. {
  182. case Scenes.File:
  183. scenePath = MENUPATH_FILE; break;
  184. case Scenes.Folder:
  185. scenePath = MENUPATH_FOLDER; break;
  186. case Scenes.Directory:
  187. scenePath = MENUPATH_DIRECTORY; break;
  188. case Scenes.Background:
  189. scenePath = MENUPATH_BACKGROUND; break;
  190. case Scenes.Desktop:
  191. //Vista系统没有这一项
  192. if(WindowsOsVersion.IsEqualVista) return;
  193. scenePath = MENUPATH_DESKTOP; break;
  194. case Scenes.Drive:
  195. scenePath = MENUPATH_DRIVE; break;
  196. case Scenes.AllObjects:
  197. scenePath = MENUPATH_ALLOBJECTS; break;
  198. case Scenes.Computer:
  199. scenePath = MENUPATH_COMPUTER; break;
  200. case Scenes.RecycleBin:
  201. scenePath = MENUPATH_RECYCLEBIN; break;
  202. case Scenes.Library:
  203. //Vista系统没有这一项
  204. if(WindowsOsVersion.IsEqualVista) return;
  205. scenePath = MENUPATH_LIBRARY; break;
  206. case Scenes.LnkFile:
  207. scenePath = GetOpenModePath(".lnk"); break;
  208. case Scenes.UwpLnk:
  209. //Win8之前没有Uwp
  210. if(WindowsOsVersion.IsBefore8) return;
  211. scenePath = MENUPATH_UWPLNK; break;
  212. case Scenes.ExeFile:
  213. scenePath = GetSysAssExtPath(".exe"); break;
  214. case Scenes.UnknownType:
  215. scenePath = MENUPATH_UNKNOWN; break;
  216. case Scenes.CustomExtension:
  217. bool isLnk = CurrentExtension?.ToLower() == ".lnk";
  218. if(isLnk) scenePath = GetOpenModePath(".lnk");
  219. else scenePath = GetSysAssExtPath(CurrentExtension);
  220. break;
  221. case Scenes.PerceivedType:
  222. scenePath = GetSysAssExtPath(CurrentPerceivedType); break;
  223. case Scenes.DirectoryType:
  224. if(CurrentDirectoryType == null) scenePath = null;
  225. else scenePath = GetSysAssExtPath($"Directory.{CurrentDirectoryType}"); break;
  226. case Scenes.MenuAnalysis:
  227. this.AddItem(new SelectItem(Scene));
  228. this.LoadAnalysisItems();
  229. return;
  230. case Scenes.CustomRegPath:
  231. scenePath = CurrentCustomRegPath; break;
  232. case Scenes.CommandStore:
  233. //Vista系统没有这一项
  234. if(WindowsOsVersion.IsEqualVista) return;
  235. this.AddNewItem(RegistryEx.GetParentPath(ShellItem.CommandStorePath));
  236. this.LoadStoreItems();
  237. return;
  238. case Scenes.DragDrop:
  239. this.AddItem(new SelectItem(Scene));
  240. this.AddNewItem(MENUPATH_FOLDER);
  241. this.LoadShellExItems(GetShellExPath(MENUPATH_FOLDER));
  242. this.LoadShellExItems(GetShellExPath(MENUPATH_DIRECTORY));
  243. this.LoadShellExItems(GetShellExPath(MENUPATH_DRIVE));
  244. this.LoadShellExItems(GetShellExPath(MENUPATH_ALLOBJECTS));
  245. return;
  246. }
  247. this.AddNewItem(scenePath);
  248. this.LoadItems(scenePath);
  249. if(WindowsOsVersion.ISAfterOrEqual10)
  250. {
  251. string webPath = AppConfig.WebUwpModeItemsDic;
  252. string userPath = AppConfig.UserUwpModeItemsDic;
  253. string contents = Properties.Resources.UwpModeItemsDic;
  254. if(!File.Exists(webPath)) File.WriteAllText(webPath, contents, Encoding.Unicode);
  255. this.LoadUwpModeItem(webPath);
  256. this.LoadUwpModeItem(userPath);
  257. }
  258. switch(Scene)
  259. {
  260. case Scenes.Background:
  261. this.AddItem(new VisibleRegRuleItem(VisibleRegRuleItem.CustomFolder));
  262. break;
  263. case Scenes.Computer:
  264. this.AddItem(new VisibleRegRuleItem(VisibleRegRuleItem.NetworkDrive));
  265. break;
  266. case Scenes.RecycleBin:
  267. this.AddItem(new VisibleRegRuleItem(VisibleRegRuleItem.RecycleBinProperties));
  268. break;
  269. case Scenes.Library:
  270. this.LoadItems(MENUPATH_LIBRARY_BACKGROUND);
  271. this.LoadItems(MENUPATH_LIBRARY_USER);
  272. break;
  273. case Scenes.ExeFile:
  274. this.LoadItems(GetOpenModePath(".exe"));
  275. break;
  276. case Scenes.CustomExtension:
  277. case Scenes.PerceivedType:
  278. case Scenes.DirectoryType:
  279. case Scenes.CustomRegPath:
  280. this.InsertItem(new SelectItem(Scene), 0);
  281. if(Scene == Scenes.CustomExtension && CurrentExtension != null)
  282. {
  283. this.LoadItems(GetOpenModePath(CurrentExtension));
  284. this.InsertItem(new SelectItem(Scenes.CustomExtensionPerceivedType), 1);
  285. }
  286. break;
  287. }
  288. }
  289. private void LoadItems(string scenePath)
  290. {
  291. if(scenePath == null) return;
  292. RegTrustedInstaller.TakeRegKeyOwnerShip(scenePath);
  293. this.LoadShellItems(GetShellPath(scenePath));
  294. this.LoadShellExItems(GetShellExPath(scenePath));
  295. }
  296. private void LoadShellItems(string shellPath)
  297. {
  298. using(RegistryKey shellKey = RegistryEx.GetRegistryKey(shellPath))
  299. {
  300. if(shellKey == null) return;
  301. RegTrustedInstaller.TakeRegTreeOwnerShip(shellKey.Name);
  302. foreach(string keyName in shellKey.GetSubKeyNames())
  303. {
  304. this.AddItem(new ShellItem($@"{shellPath}\{keyName}"));
  305. }
  306. }
  307. }
  308. private void LoadShellExItems(string shellExPath)
  309. {
  310. List<string> names = new List<string>();
  311. using(RegistryKey shellExKey = RegistryEx.GetRegistryKey(shellExPath))
  312. {
  313. if(shellExKey == null) return;
  314. bool isDragDrop = Scene == Scenes.DragDrop;
  315. RegTrustedInstaller.TakeRegTreeOwnerShip(shellExKey.Name);
  316. Dictionary<string, Guid> dic = ShellExItem.GetPathAndGuids(shellExPath, isDragDrop);
  317. GroupPathItem groupItem = null;
  318. if(isDragDrop)
  319. {
  320. groupItem = GetDragDropGroupItem(shellExPath);
  321. this.AddItem(groupItem);
  322. }
  323. foreach(string path in dic.Keys)
  324. {
  325. string keyName = RegistryEx.GetKeyName(path);
  326. if(!names.Contains(keyName))
  327. {
  328. ShellExItem item = new ShellExItem(dic[path], path);
  329. if(groupItem != null) item.FoldGroupItem = groupItem;
  330. this.AddItem(item);
  331. names.Add(keyName);
  332. }
  333. }
  334. if(groupItem != null) groupItem.IsFold = true;
  335. }
  336. }
  337. private GroupPathItem GetDragDropGroupItem(string shellExPath)
  338. {
  339. string text = null;
  340. Image image = null;
  341. string path = shellExPath.Substring(0, shellExPath.LastIndexOf('\\'));
  342. switch(path)
  343. {
  344. case MENUPATH_FOLDER:
  345. text = AppString.SideBar.Folder;
  346. image = AppImage.Folder;
  347. break;
  348. case MENUPATH_DIRECTORY:
  349. text = AppString.SideBar.Directory;
  350. image = AppImage.Directory;
  351. break;
  352. case MENUPATH_DRIVE:
  353. text = AppString.SideBar.Drive;
  354. image = AppImage.Drive;
  355. break;
  356. case MENUPATH_ALLOBJECTS:
  357. text = AppString.SideBar.AllObjects;
  358. image = AppImage.AllObjects;
  359. break;
  360. }
  361. return new GroupPathItem(shellExPath, ObjectPath.PathType.Registry) { Text = text, Image = image };
  362. }
  363. private void AddNewItem(string scenePath)
  364. {
  365. NewItem newItem = new NewItem { Visible = scenePath != null };
  366. this.AddItem(newItem);
  367. newItem.AddNewItem += (sender, e) =>
  368. {
  369. bool isShell;
  370. if(Scene == Scenes.CommandStore) isShell = true;
  371. else if(Scene == Scenes.DragDrop) isShell = false;
  372. else
  373. {
  374. using(SelectDialog dlg = new SelectDialog())
  375. {
  376. dlg.Items = new[] { "Shell", "ShellEx" };
  377. dlg.Title = AppString.Dialog.SelectNewItemType;
  378. if(dlg.ShowDialog() != DialogResult.OK) return;
  379. isShell = dlg.SelectedIndex == 0;
  380. }
  381. }
  382. if(isShell) this.AddNewShellItem(scenePath);
  383. else this.AddNewShellExItem(scenePath);
  384. };
  385. }
  386. private void AddNewShellItem(string scenePath)
  387. {
  388. string shellPath = GetShellPath(scenePath);
  389. using(NewShellDialog dlg = new NewShellDialog())
  390. {
  391. dlg.ScenePath = scenePath;
  392. dlg.ShellPath = shellPath;
  393. if(dlg.ShowDialog() != DialogResult.OK) return;
  394. for(int i = 0; i < this.Controls.Count; i++)
  395. {
  396. if(this.Controls[i] is NewItem)
  397. {
  398. ShellItem item;
  399. if(Scene != Scenes.CommandStore) item = new ShellItem(dlg.NewItemRegPath);
  400. else item = new StoreShellItem(dlg.NewItemRegPath, true, false);
  401. this.InsertItem(item, i + 1);
  402. break;
  403. }
  404. }
  405. }
  406. }
  407. private void AddNewShellExItem(string scenePath)
  408. {
  409. bool isDragDrop = Scene == Scenes.DragDrop;
  410. using(InputDialog dlg1 = new InputDialog { Title = AppString.Dialog.InputGuid })
  411. {
  412. if(GuidEx.TryParse(Clipboard.GetText(), out Guid guid)) dlg1.Text = guid.ToString();
  413. if(dlg1.ShowDialog() != DialogResult.OK) return;
  414. if(GuidEx.TryParse(dlg1.Text, out guid))
  415. {
  416. if(isDragDrop)
  417. {
  418. using(SelectDialog dlg2 = new SelectDialog())
  419. {
  420. dlg2.Title = AppString.Dialog.SelectGroup;
  421. dlg2.Items = new[] { AppString.SideBar.Folder, AppString.SideBar.Directory,
  422. AppString.SideBar.Drive, AppString.SideBar.AllObjects };
  423. if(dlg2.ShowDialog() != DialogResult.OK) return;
  424. switch(dlg2.SelectedIndex)
  425. {
  426. case 0:
  427. scenePath = MENUPATH_FOLDER; break;
  428. case 1:
  429. scenePath = MENUPATH_DIRECTORY; break;
  430. case 2:
  431. scenePath = MENUPATH_DRIVE; break;
  432. case 3:
  433. scenePath = MENUPATH_ALLOBJECTS; break;
  434. }
  435. }
  436. }
  437. string shellExPath = GetShellExPath(scenePath);
  438. if(ShellExItem.GetPathAndGuids(shellExPath, isDragDrop).Values.Contains(guid))
  439. {
  440. MessageBoxEx.Show(AppString.Message.HasBeenAdded);
  441. }
  442. else
  443. {
  444. string part = isDragDrop ? ShellExItem.DdhParts[0] : ShellExItem.CmhParts[0];
  445. string regPath = $@"{shellExPath}\{part}\{guid:B}";
  446. Registry.SetValue(regPath, "", guid.ToString("B"));
  447. ShellExItem item = new ShellExItem(guid, regPath);
  448. for(int i = 0; i < this.Controls.Count; i++)
  449. {
  450. if(isDragDrop)
  451. {
  452. if(this.Controls[i] is GroupPathItem groupItem)
  453. {
  454. if(groupItem.TargetPath.Equals(shellExPath, StringComparison.OrdinalIgnoreCase))
  455. {
  456. this.InsertItem(item, i + 1);
  457. item.FoldGroupItem = groupItem;
  458. item.Visible = !groupItem.IsFold;
  459. break;
  460. }
  461. }
  462. }
  463. else
  464. {
  465. if(this.Controls[i] is NewItem)
  466. {
  467. this.InsertItem(item, i + 1);
  468. break;
  469. }
  470. }
  471. }
  472. }
  473. }
  474. else
  475. {
  476. MessageBoxEx.Show(AppString.Message.MalformedGuid);
  477. }
  478. }
  479. }
  480. private void LoadStoreItems()
  481. {
  482. using(var shellKey = RegistryEx.GetRegistryKey(ShellItem.CommandStorePath))
  483. {
  484. Array.ForEach(Array.FindAll(shellKey.GetSubKeyNames(), itemName =>
  485. !ShellItem.SysStoreItemNames.Contains(itemName, StringComparer.OrdinalIgnoreCase)), itemName =>
  486. this.AddItem(new StoreShellItem($@"{ShellItem.CommandStorePath}\{itemName}", true, false)));
  487. }
  488. }
  489. private void LoadUwpModeItem(string xmlPath)
  490. {
  491. XmlDocument doc = new XmlDocument();
  492. try { doc.LoadXml(File.ReadAllText(xmlPath, EncodingType.GetType(xmlPath))); }
  493. catch { return; }
  494. foreach(XmlElement sceneXE in doc.DocumentElement.ChildNodes)
  495. {
  496. if(sceneXE.Name == Scene.ToString())
  497. {
  498. foreach(XmlElement itemXE in sceneXE.ChildNodes)
  499. {
  500. if(GuidEx.TryParse(itemXE.GetAttribute("Guid"), out Guid guid))
  501. {
  502. bool isAdded = false;
  503. foreach(Control ctr in this.Controls)
  504. {
  505. if(ctr is UwpModeItem item && item.Guid == guid) { isAdded = true; break; }
  506. }
  507. if(isAdded) continue;
  508. string uwpName = GuidInfo.GetUwpName(guid);
  509. if(!string.IsNullOrEmpty(uwpName))
  510. {
  511. this.AddItem(new UwpModeItem(uwpName, guid));
  512. }
  513. }
  514. }
  515. }
  516. }
  517. }
  518. private void LoadAnalysisItems()
  519. {
  520. if(CurrentFileObjectPath == null) return;
  521. void AddFileItems(string filePath)
  522. {
  523. string extension = Path.GetExtension(filePath).ToLower();
  524. if(extension == string.Empty) extension = ".";
  525. string perceivedType = Registry.GetValue($@"{RegistryEx.CLASSESROOT}\{extension}", "PerceivedType", null)?.ToString();
  526. string perceivedTypeName = GetPerceivedTypeName(perceivedType);
  527. string openMode = FileExtension.GetOpenMode(extension);
  528. JumpItem.TargetPath = filePath;
  529. JumpItem.Extension = extension;
  530. JumpItem.PerceivedType = perceivedType;
  531. this.AddItem(new JumpItem(Scenes.File));
  532. this.AddItem(new JumpItem(Scenes.AllObjects));
  533. if(extension == ".exe") this.AddItem(new JumpItem(Scenes.ExeFile));
  534. else this.AddItem(new JumpItem(Scenes.CustomExtension));
  535. if(openMode == null) this.AddItem(new JumpItem(Scenes.UnknownType));
  536. if(perceivedType != null) this.AddItem(new JumpItem(Scenes.PerceivedType));
  537. }
  538. void AddDirItems(string dirPath)
  539. {
  540. if(!dirPath.EndsWith(":\\"))
  541. {
  542. this.AddItem(new JumpItem(Scenes.Folder));
  543. this.AddItem(new JumpItem(Scenes.Directory));
  544. this.AddItem(new JumpItem(Scenes.AllObjects));
  545. this.AddItem(new JumpItem(Scenes.DirectoryType));
  546. }
  547. else
  548. {
  549. this.AddItem(new JumpItem(Scenes.Drive));
  550. }
  551. }
  552. if(File.Exists(CurrentFileObjectPath))
  553. {
  554. string extension = Path.GetExtension(CurrentFileObjectPath).ToLower();
  555. if(extension == ".lnk")
  556. {
  557. this.AddItem(new JumpItem(Scenes.LnkFile));
  558. using(ShellLink shellLink = new ShellLink(CurrentFileObjectPath))
  559. {
  560. string targetPath = shellLink.TargetPath;
  561. if(File.Exists(targetPath))
  562. {
  563. AddFileItems(targetPath);
  564. }
  565. else if(Directory.Exists(targetPath))
  566. {
  567. AddDirItems(targetPath);
  568. }
  569. }
  570. }
  571. else
  572. {
  573. AddFileItems(CurrentFileObjectPath);
  574. }
  575. }
  576. else if(Directory.Exists(CurrentFileObjectPath))
  577. {
  578. AddDirItems(CurrentFileObjectPath);
  579. }
  580. }
  581. public sealed class SelectItem : MyListItem
  582. {
  583. public SelectItem(Scenes scene)
  584. {
  585. this.Scene = scene;
  586. this.AddCtr(BtnSelect);
  587. this.SetTextAndTip();
  588. this.SetImage();
  589. BtnSelect.MouseDown += (sender, e) => ShowSelectDialog();
  590. this.ImageDoubleClick += (sender, e) => ShowSelectDialog();
  591. this.TextDoubleClick += (sender, e) => ShowSelectDialog();
  592. }
  593. readonly PictureButton BtnSelect = new PictureButton(AppImage.Select);
  594. public Scenes Scene { get; private set; }
  595. private void SetTextAndTip()
  596. {
  597. string tip = "";
  598. string text = "";
  599. switch(Scene)
  600. {
  601. case Scenes.CustomExtension:
  602. tip = AppString.Dialog.SelectExtension;
  603. if(CurrentExtension == null) text = tip;
  604. else text = AppString.Other.CurrentExtension.Replace("%s", CurrentExtension);
  605. break;
  606. case Scenes.PerceivedType:
  607. tip = AppString.Dialog.SelectPerceivedType;
  608. if(CurrentPerceivedType == null) text = tip;
  609. else text = AppString.Other.CurrentPerceivedType.Replace("%s", GetPerceivedTypeName(CurrentPerceivedType));
  610. break;
  611. case Scenes.DirectoryType:
  612. tip = AppString.Dialog.SelectDirectoryType;
  613. if(CurrentDirectoryType == null) text = tip;
  614. else text = AppString.Other.CurrentDirectoryType.Replace("%s", GetDirectoryTypeName(CurrentDirectoryType));
  615. break;
  616. case Scenes.CustomRegPath:
  617. tip = AppString.Other.SelectRegPath;
  618. if(CurrentCustomRegPath == null) text = tip;
  619. else text = AppString.Other.CurrentRegPath + "\n" + CurrentCustomRegPath;
  620. break;
  621. case Scenes.MenuAnalysis:
  622. tip = AppString.Tip.DropOrSelectObject;
  623. if(CurrentFileObjectPath == null) text = tip;
  624. else text = AppString.Other.CurrentFilePath + "\n" + CurrentFileObjectPath;
  625. break;
  626. case Scenes.DragDrop:
  627. tip = AppString.Dialog.SelectDropEffect;
  628. text = AppString.Other.SetDefaultDropEffect + " " + GetDropEffectName();
  629. break;
  630. case Scenes.CustomExtensionPerceivedType:
  631. tip = AppString.Dialog.SelectPerceivedType;
  632. text = AppString.Other.SetPerceivedType.Replace("%s", CurrentExtension)
  633. + " " + GetPerceivedTypeName(CurrentExtensionPerceivedType);
  634. break;
  635. }
  636. MyToolTip.SetToolTip(BtnSelect, tip);
  637. this.Text = text;
  638. }
  639. private void SetImage()
  640. {
  641. switch(Scene)
  642. {
  643. case Scenes.CustomExtensionPerceivedType:
  644. using(Icon icon = ResourceIcon.GetExtensionIcon(CurrentExtension))
  645. this.Image = icon?.ToBitmap();
  646. break;
  647. }
  648. if(this.Image == null) this.Image = AppImage.Custom;
  649. }
  650. private void ShowSelectDialog()
  651. {
  652. SelectDialog dlg = null;
  653. switch(Scene)
  654. {
  655. case Scenes.CustomExtension:
  656. dlg = new FileExtensionDialog
  657. {
  658. Selected = CurrentExtension?.Substring(1)
  659. };
  660. break;
  661. case Scenes.PerceivedType:
  662. dlg = new SelectDialog
  663. {
  664. Items = PerceivedTypeNames,
  665. Title = AppString.Dialog.SelectPerceivedType,
  666. Selected = GetPerceivedTypeName(CurrentPerceivedType)
  667. };
  668. break;
  669. case Scenes.DirectoryType:
  670. dlg = new SelectDialog
  671. {
  672. Items = DirectoryTypeNames,
  673. Title = AppString.Dialog.SelectDirectoryType,
  674. Selected = GetDirectoryTypeName(CurrentDirectoryType)
  675. };
  676. break;
  677. case Scenes.CustomExtensionPerceivedType:
  678. dlg = new SelectDialog
  679. {
  680. Items = PerceivedTypeNames,
  681. Title = AppString.Dialog.SelectPerceivedType,
  682. Selected = GetPerceivedTypeName(CurrentExtensionPerceivedType)
  683. };
  684. break;
  685. case Scenes.DragDrop:
  686. dlg = new SelectDialog
  687. {
  688. Items = DropEffectNames,
  689. Title = AppString.Dialog.SelectDropEffect,
  690. Selected = GetDropEffectName()
  691. };
  692. break;
  693. case Scenes.MenuAnalysis:
  694. dlg = new SelectDialog
  695. {
  696. Items = FileObjectTypes,
  697. Title = AppString.Dialog.SelectObjectType,
  698. };
  699. break;
  700. case Scenes.CustomRegPath:
  701. if(MessageBoxEx.Show(AppString.Message.SelectRegPath,
  702. MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) return;
  703. Form frm = this.FindForm();
  704. frm.Hide();
  705. using(Process process = Process.Start("regedit.exe", "-m"))
  706. {
  707. process.WaitForExit();
  708. }
  709. string path = Registry.GetValue(LASTKEYPATH, "LastKey", "").ToString();
  710. int index = path.IndexOf('\\');
  711. if(index == -1) return;
  712. path = path.Substring(index + 1);
  713. CurrentCustomRegPath = path;
  714. this.RefreshList();
  715. frm.Show();
  716. frm.Activate();
  717. break;
  718. }
  719. switch(Scene)
  720. {
  721. case Scenes.CustomExtension:
  722. case Scenes.PerceivedType:
  723. case Scenes.DirectoryType:
  724. case Scenes.MenuAnalysis:
  725. case Scenes.DragDrop:
  726. case Scenes.CustomExtensionPerceivedType:
  727. if(dlg.ShowDialog() != DialogResult.OK) return;
  728. break;
  729. }
  730. switch(Scene)
  731. {
  732. case Scenes.CustomExtension:
  733. CurrentExtension = dlg.Selected;
  734. this.RefreshList();
  735. break;
  736. case Scenes.PerceivedType:
  737. CurrentPerceivedType = PerceivedTypes[dlg.SelectedIndex];
  738. this.RefreshList();
  739. break;
  740. case Scenes.DirectoryType:
  741. CurrentDirectoryType = DirectoryTypes[dlg.SelectedIndex];
  742. this.RefreshList();
  743. break;
  744. case Scenes.CustomExtensionPerceivedType:
  745. string selected = PerceivedTypes[dlg.SelectedIndex];
  746. CurrentExtensionPerceivedType = selected;
  747. this.Text = AppString.Other.SetPerceivedType.Replace("%s", CurrentExtension)
  748. + " " + GetPerceivedTypeName(selected);
  749. break;
  750. case Scenes.DragDrop:
  751. switch(dlg.SelectedIndex)
  752. {
  753. case 0: DefaultDropEffect = DropEffect.Default; break;
  754. case 1: DefaultDropEffect = DropEffect.Copy; break;
  755. case 2: DefaultDropEffect = DropEffect.Move; break;
  756. case 3: DefaultDropEffect = DropEffect.CreateLink; break;
  757. }
  758. this.Text = AppString.Other.SetDefaultDropEffect + " " + GetDropEffectName();
  759. break;
  760. case Scenes.MenuAnalysis:
  761. if(dlg.SelectedIndex == 0)
  762. {
  763. using(var dlg1 = new System.Windows.Forms.OpenFileDialog())
  764. {
  765. dlg1.DereferenceLinks = false;
  766. if(dlg1.ShowDialog() != DialogResult.OK) return;
  767. CurrentFileObjectPath = dlg1.FileName;
  768. }
  769. }
  770. else
  771. {
  772. using(var dlg2 = new FolderBrowserDialog())
  773. {
  774. if(dlg2.ShowDialog() != DialogResult.OK) return;
  775. CurrentFileObjectPath = dlg2.SelectedPath;
  776. }
  777. }
  778. this.RefreshList();
  779. break;
  780. }
  781. }
  782. private void RefreshList()
  783. {
  784. ShellList list = (ShellList)this.Parent;
  785. list.ClearItems();
  786. list.LoadItems();
  787. }
  788. }
  789. sealed class JumpItem : MyListItem
  790. {
  791. public JumpItem(Scenes scene)
  792. {
  793. this.AddCtr(btnJump);
  794. string text = "";
  795. Image image = null;
  796. int index1 = 0;
  797. int index2 = 0;
  798. switch(scene)
  799. {
  800. case Scenes.File:
  801. text = $"[ {AppString.ToolBar.Home} ] ▶ [ {AppString.SideBar.File} ]";
  802. image = AppImage.File;
  803. break;
  804. case Scenes.Folder:
  805. text = $"[ {AppString.ToolBar.Home} ] ▶ [ {AppString.SideBar.Folder} ]";
  806. image = AppImage.Folder;
  807. index2 = 1;
  808. break;
  809. case Scenes.Directory:
  810. text = $"[ {AppString.ToolBar.Home} ] ▶ [ {AppString.SideBar.Directory} ]";
  811. image = AppImage.Directory;
  812. index2 = 2;
  813. break;
  814. case Scenes.Drive:
  815. text = $"[ {AppString.ToolBar.Home} ] ▶ [ {AppString.SideBar.Drive} ]";
  816. image = AppImage.Drive;
  817. index2 = 5;
  818. break;
  819. case Scenes.AllObjects:
  820. text = $"[ {AppString.ToolBar.Home} ] ▶ [ {AppString.SideBar.AllObjects} ]";
  821. image = AppImage.AllObjects;
  822. index2 = 6;
  823. break;
  824. case Scenes.LnkFile:
  825. text = $"[ {AppString.ToolBar.Type} ] ▶ [ {AppString.SideBar.LnkFile} ]";
  826. image = AppImage.LnkFile;
  827. index1 = 1;
  828. break;
  829. case Scenes.ExeFile:
  830. text = $"[ {AppString.ToolBar.Type} ] ▶ [ {AppString.SideBar.ExeFile} ]";
  831. using(Icon icon = ResourceIcon.GetExtensionIcon(TargetPath)) image = icon.ToBitmap();
  832. index1 = 1;
  833. index2 = 2;
  834. break;
  835. case Scenes.UnknownType:
  836. text = $"[ {AppString.ToolBar.Type} ] ▶ [ {AppString.SideBar.UnknownType} ]";
  837. image = AppImage.NotFound;
  838. index1 = 1;
  839. index2 = 8;
  840. break;
  841. case Scenes.CustomExtension:
  842. text = $"[ {AppString.ToolBar.Type} ] ▶ [ {AppString.SideBar.CustomExtension} ] ▶ [ {Extension} ]";
  843. using(Icon icon = ResourceIcon.GetExtensionIcon(Extension)) image = icon.ToBitmap();
  844. index1 = 1;
  845. index2 = 4;
  846. break;
  847. case Scenes.PerceivedType:
  848. text = $"[ {AppString.ToolBar.Type} ] ▶ [ {AppString.SideBar.PerceivedType} ] ▶ [ {GetPerceivedTypeName(PerceivedType)} ]";
  849. image = AppImage.File;
  850. index1 = 1;
  851. index2 = 5;
  852. break;
  853. case Scenes.DirectoryType:
  854. text = $"[ {AppString.ToolBar.Type} ] ▶ [ {AppString.SideBar.DirectoryType} ]";
  855. image = AppImage.Directory;
  856. index1 = 1;
  857. index2 = 6;
  858. break;
  859. }
  860. this.Text = text;
  861. this.Image = image;
  862. void SwitchTab()
  863. {
  864. switch(scene)
  865. {
  866. case Scenes.CustomExtension:
  867. CurrentExtension = Extension; break;
  868. case Scenes.PerceivedType:
  869. CurrentPerceivedType = PerceivedType; break;
  870. }
  871. ((MainForm)this.FindForm()).SwitchTab(index1, index2);
  872. };
  873. btnJump.MouseDown += (sender, e) => SwitchTab();
  874. this.ImageDoubleClick += (sender, e) => SwitchTab();
  875. this.TextDoubleClick += (sender, e) => SwitchTab();
  876. }
  877. readonly PictureButton btnJump = new PictureButton(AppImage.Jump);
  878. public static string Extension = null;
  879. public static string PerceivedType = null;
  880. public static string TargetPath = null;
  881. }
  882. }
  883. }