ShellList.cs 43 KB

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