EnhanceMenusList.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using BluePointLilac.Controls;
  2. using BluePointLilac.Methods;
  3. using ContextMenuManager.Controls.Interfaces;
  4. using System;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Text;
  8. using System.Xml;
  9. namespace ContextMenuManager.Controls
  10. {
  11. sealed class EnhanceMenusList : MyList
  12. {
  13. public void LoadItems()
  14. {
  15. string webPath = AppConfig.WebEnhanceMenusDic;
  16. string userPath = AppConfig.UserEnhanceMenusDic;
  17. string contents = Properties.Resources.EnhanceMenusDic;
  18. if(!File.Exists(webPath)) File.WriteAllText(webPath, contents, Encoding.Unicode);
  19. GroupPathItem webGroupItem = new GroupPathItem(webPath, ObjectPath.PathType.File);
  20. GroupPathItem userGroupItem = new GroupPathItem(userPath, ObjectPath.PathType.File);
  21. webGroupItem.Text = AppString.SideBar.Dictionaries;
  22. userGroupItem.Text = AppString.Other.UserDictionaries;
  23. webGroupItem.Image = AppImage.App;
  24. userGroupItem.Image = AppImage.User;
  25. LoadDocItems(webPath, webGroupItem);
  26. LoadDocItems(userPath, userGroupItem);
  27. }
  28. private void LoadDocItems(string xmlPath, GroupPathItem groupItem)
  29. {
  30. if(!File.Exists(xmlPath)) return;
  31. this.AddItem(groupItem);
  32. XmlDocument doc = new XmlDocument();
  33. try { doc.LoadXml(File.ReadAllText(xmlPath, EncodingType.GetType(xmlPath))); }
  34. catch { return; }
  35. foreach(XmlNode xn in doc.DocumentElement.ChildNodes)
  36. {
  37. try
  38. {
  39. SubGroupItem subGroupItem = GetGroupPathItem(xn);
  40. if(subGroupItem == null) continue;
  41. this.AddItem(subGroupItem);
  42. XmlElement shellXE = (XmlElement)xn.SelectSingleNode("Shell");
  43. XmlElement shellExXE = (XmlElement)xn.SelectSingleNode("ShellEx");
  44. if(shellXE != null) LoadShellItems(shellXE, subGroupItem);
  45. if(shellExXE != null) LoadShellExItems(shellExXE, subGroupItem);
  46. subGroupItem.HideWhenNoSubItem();
  47. subGroupItem.FoldGroupItem = groupItem;
  48. }
  49. catch { continue; }
  50. }
  51. groupItem.IsFold = true;
  52. groupItem.HideWhenNoSubItem();
  53. }
  54. private SubGroupItem GetGroupPathItem(XmlNode xn)
  55. {
  56. string path;
  57. string text;
  58. Image image;
  59. switch(xn.Name)
  60. {
  61. case "File":
  62. path = ShellList.MENUPATH_FILE;
  63. text = AppString.SideBar.File;
  64. image = AppImage.File;
  65. break;
  66. case "Folder":
  67. path = ShellList.MENUPATH_FOLDER;
  68. text = AppString.SideBar.Folder;
  69. image = AppImage.Folder;
  70. break;
  71. case "Directory":
  72. path = ShellList.MENUPATH_FOLDER;
  73. text = AppString.SideBar.Directory;
  74. image = AppImage.Directory;
  75. break;
  76. case "Background":
  77. path = ShellList.MENUPATH_BACKGROUND;
  78. text = AppString.SideBar.Background;
  79. image = AppImage.Background;
  80. break;
  81. case "Desktop":
  82. path = ShellList.MENUPATH_DESKTOP;
  83. //Vista没有桌面右键菜单的独立注册表项
  84. if(WindowsOsVersion.IsEqualVista) path = ShellList.MENUPATH_BACKGROUND;
  85. text = AppString.SideBar.Desktop;
  86. image = AppImage.Desktop;
  87. break;
  88. case "Drive":
  89. path = ShellList.MENUPATH_DRIVE;
  90. text = AppString.SideBar.Drive;
  91. image = AppImage.Drive;
  92. break;
  93. case "AllObjects":
  94. path = ShellList.MENUPATH_ALLOBJECTS;
  95. text = AppString.SideBar.AllObjects;
  96. image = AppImage.AllObjects;
  97. break;
  98. case "Computer":
  99. path = ShellList.MENUPATH_COMPUTER;
  100. text = AppString.SideBar.Computer;
  101. image = AppImage.Computer;
  102. break;
  103. case "RecycleBin":
  104. path = ShellList.MENUPATH_RECYCLEBIN;
  105. text = AppString.SideBar.RecycleBin;
  106. image = AppImage.RecycleBin;
  107. break;
  108. default:
  109. XmlElement xe = (XmlElement)xn;
  110. path = xe.GetAttribute("RegPath");
  111. text = ResourceString.GetDirectString(xe.GetAttribute("Text"));
  112. if(string.IsNullOrEmpty(path) || string.IsNullOrEmpty(text)) return null;
  113. using(Icon icon = ResourceIcon.GetIcon(xe.GetAttribute("Icon")))
  114. {
  115. if(icon != null) image = icon.ToBitmap();
  116. else image = AppImage.NotFound;
  117. }
  118. break;
  119. }
  120. return new SubGroupItem(path, ObjectPath.PathType.Registry) { Image = image, Text = text };
  121. }
  122. private void LoadShellItems(XmlElement shellXE, SubGroupItem groupItem)
  123. {
  124. foreach(XmlElement itemXE in shellXE.SelectNodes("Item"))
  125. {
  126. if(!JudgeOSVersion(itemXE)) continue;
  127. if(!FileExists(itemXE)) continue;
  128. XmlElement szXE = (XmlElement)itemXE.SelectSingleNode("Value/REG_SZ");
  129. string keyName = itemXE.GetAttribute("KeyName");
  130. if(keyName.IsNullOrWhiteSpace()) continue;
  131. EnhanceShellItem item = new EnhanceShellItem()
  132. {
  133. RegPath = $@"{groupItem.TargetPath}\shell\{keyName}",
  134. FoldGroupItem = groupItem,
  135. ItemXE = itemXE
  136. };
  137. if(szXE != null)
  138. {
  139. item.Text = ResourceString.GetDirectString(szXE.GetAttribute("MUIVerb"));
  140. if(szXE.HasAttribute("Icon")) item.Image = ResourceIcon.GetIcon(szXE.GetAttribute("Icon"))?.ToBitmap();
  141. else if(szXE.HasAttribute("HasLUAShield")) item.Image = AppImage.Shield;
  142. else
  143. {
  144. XmlElement cmdXE = (XmlElement)itemXE.SelectSingleNode("SubKey/Command");
  145. if(cmdXE != null)
  146. {
  147. Icon icon = null;
  148. if(cmdXE.HasAttribute("Default"))
  149. {
  150. string filePath = ObjectPath.ExtractFilePath(cmdXE.GetAttribute("Default"));
  151. icon = ResourceIcon.GetIcon(filePath);
  152. }
  153. item.Image = icon?.ToBitmap();
  154. icon?.Dispose();
  155. }
  156. }
  157. }
  158. if(item.Image == null) item.Image = AppImage.NotFound;
  159. if(item.Text.IsNullOrWhiteSpace()) item.Text = keyName;
  160. item.ChkVisible.Checked = item.ItemVisible;
  161. string tip = itemXE.GetAttribute("Tip");
  162. if(itemXE.GetElementsByTagName("CreateFile").Count > 0)
  163. {
  164. if(!tip.IsNullOrWhiteSpace()) tip += "\n";
  165. tip += AppString.Tip.CommandFiles;
  166. }
  167. MyToolTip.SetToolTip(item.ChkVisible, tip);
  168. this.AddItem(item);
  169. }
  170. }
  171. private void LoadShellExItems(XmlElement shellExXE, SubGroupItem groupItem)
  172. {
  173. foreach(XmlElement itemXE in shellExXE.SelectNodes("Item"))
  174. {
  175. if(!JudgeOSVersion(itemXE)) continue;
  176. if(!GuidEx.TryParse(itemXE.GetAttribute("Guid"), out Guid guid)) continue;
  177. EnhanceShellExItem item = new EnhanceShellExItem
  178. {
  179. FoldGroupItem = groupItem,
  180. ShellExPath = $@"{groupItem.TargetPath}\ShellEx",
  181. Image = ResourceIcon.GetIcon(itemXE.GetAttribute("Icon"))?.ToBitmap() ?? AppImage.SystemFile,
  182. Text = ResourceString.GetDirectString(itemXE.GetAttribute("Text")),
  183. DefaultKeyName = itemXE.GetAttribute("KeyName"),
  184. Guid = guid
  185. };
  186. if(item.Text.IsNullOrWhiteSpace()) item.Text = GuidInfo.GetText(guid);
  187. if(item.DefaultKeyName.IsNullOrWhiteSpace()) item.DefaultKeyName = guid.ToString("B");
  188. item.ChkVisible.Checked = item.ItemVisible;
  189. MyToolTip.SetToolTip(item.ChkVisible, itemXE.GetAttribute("Tip"));
  190. this.AddItem(item);
  191. }
  192. }
  193. public static bool JudgeOSVersion(XmlElement itemXE)
  194. {
  195. bool JudgeOne(XmlElement osXE)
  196. {
  197. Version ver = new Version(osXE.InnerText);
  198. Version osVer = Environment.OSVersion.Version;
  199. int compare = osVer.CompareTo(ver);
  200. string symbol = osXE.GetAttribute("Compare");
  201. switch(symbol)
  202. {
  203. case ">":
  204. return compare > 0;
  205. case "<":
  206. return compare < 0;
  207. case "=":
  208. return compare == 0;
  209. case ">=":
  210. return compare >= 0;
  211. case "<=":
  212. return compare <= 0;
  213. default:
  214. return true;
  215. }
  216. }
  217. foreach(XmlElement osXE in itemXE.SelectNodes("OSVersion"))
  218. {
  219. if(!JudgeOne(osXE)) return false;
  220. }
  221. return true;
  222. }
  223. private static bool FileExists(XmlElement itemXE)
  224. {
  225. foreach(XmlElement feXE in itemXE.SelectNodes("FileExists"))
  226. {
  227. string path = Environment.ExpandEnvironmentVariables(feXE.InnerText);
  228. if(!File.Exists(path)) return false;
  229. }
  230. return true;
  231. }
  232. public static byte[] ConvertToBinary(string value)
  233. {
  234. try
  235. {
  236. string[] strs = value.Split(' ');
  237. byte[] bs = new byte[strs.Length];
  238. for(int i = 0; i < strs.Length; i++)
  239. {
  240. bs[i] = Convert.ToByte(strs[i], 16);
  241. }
  242. return bs;
  243. }
  244. catch { return null; }
  245. }
  246. }
  247. }