EnhanceMenusList.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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)).Trim()); }
  34. catch(Exception e) { MessageBoxEx.Show(e.Message); 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. string keyName = itemXE.GetAttribute("KeyName");
  129. if(keyName.IsNullOrWhiteSpace()) continue;
  130. EnhanceShellItem item = new EnhanceShellItem()
  131. {
  132. RegPath = $@"{groupItem.TargetPath}\shell\{keyName}",
  133. FoldGroupItem = groupItem,
  134. ItemXE = itemXE
  135. };
  136. foreach(XmlElement szXE in itemXE.SelectNodes("Value/REG_SZ"))
  137. {
  138. if(szXE.HasAttribute("MUIVerb")) item.Text = ResourceString.GetDirectString(szXE.GetAttribute("MUIVerb"));
  139. if(szXE.HasAttribute("Icon")) item.Image = ResourceIcon.GetIcon(szXE.GetAttribute("Icon"))?.ToBitmap();
  140. else if(szXE.HasAttribute("HasLUAShield")) item.Image = AppImage.Shield;
  141. }
  142. if(item.Image == null)
  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. else
  154. {
  155. XmlElement fileXE = (XmlElement)cmdXE.SelectSingleNode("FileName");
  156. if(fileXE != null)
  157. {
  158. string filePath = ObjectPath.ExtractFilePath(fileXE.InnerText);
  159. icon = ResourceIcon.GetIcon(filePath);
  160. }
  161. }
  162. item.Image = icon?.ToBitmap();
  163. icon?.Dispose();
  164. }
  165. }
  166. if(item.Image == null) item.Image = AppImage.NotFound;
  167. if(item.Text.IsNullOrWhiteSpace()) item.Text = keyName;
  168. string tip = itemXE.GetAttribute("Tip");
  169. if(itemXE.GetElementsByTagName("CreateFile").Count > 0)
  170. {
  171. if(!tip.IsNullOrWhiteSpace()) tip += "\n";
  172. tip += AppString.Tip.CommandFiles;
  173. }
  174. ToolTipBox.SetToolTip(item.ChkVisible, tip);
  175. this.AddItem(item);
  176. }
  177. }
  178. private void LoadShellExItems(XmlElement shellExXE, SubGroupItem groupItem)
  179. {
  180. foreach(XmlElement itemXE in shellExXE.SelectNodes("Item"))
  181. {
  182. if(!JudgeOSVersion(itemXE)) continue;
  183. if(!GuidEx.TryParse(itemXE.GetAttribute("Guid"), out Guid guid)) continue;
  184. EnhanceShellExItem item = new EnhanceShellExItem
  185. {
  186. FoldGroupItem = groupItem,
  187. ShellExPath = $@"{groupItem.TargetPath}\ShellEx",
  188. Image = ResourceIcon.GetIcon(itemXE.GetAttribute("Icon"))?.ToBitmap() ?? AppImage.SystemFile,
  189. Text = ResourceString.GetDirectString(itemXE.GetAttribute("Text")),
  190. DefaultKeyName = itemXE.GetAttribute("KeyName"),
  191. Guid = guid
  192. };
  193. if(item.Text.IsNullOrWhiteSpace()) item.Text = GuidInfo.GetText(guid);
  194. if(item.DefaultKeyName.IsNullOrWhiteSpace()) item.DefaultKeyName = guid.ToString("B");
  195. ToolTipBox.SetToolTip(item.ChkVisible, itemXE.GetAttribute("Tip"));
  196. this.AddItem(item);
  197. }
  198. }
  199. public static bool JudgeOSVersion(XmlElement itemXE)
  200. {
  201. //return true;//测试用
  202. bool JudgeOne(XmlElement osXE)
  203. {
  204. Version ver = new Version(osXE.InnerText);
  205. Version osVer = Environment.OSVersion.Version;
  206. int compare = osVer.CompareTo(ver);
  207. string symbol = osXE.GetAttribute("Compare");
  208. switch(symbol)
  209. {
  210. case ">":
  211. return compare > 0;
  212. case "<":
  213. return compare < 0;
  214. case "=":
  215. return compare == 0;
  216. case ">=":
  217. return compare >= 0;
  218. case "<=":
  219. return compare <= 0;
  220. default:
  221. return true;
  222. }
  223. }
  224. foreach(XmlElement osXE in itemXE.SelectNodes("OSVersion"))
  225. {
  226. if(!JudgeOne(osXE)) return false;
  227. }
  228. return true;
  229. }
  230. private static bool FileExists(XmlElement itemXE)
  231. {
  232. //return true;//测试用
  233. foreach(XmlElement feXE in itemXE.SelectNodes("FileExists"))
  234. {
  235. string path = Environment.ExpandEnvironmentVariables(feXE.InnerText);
  236. if(!File.Exists(path)) return false;
  237. }
  238. return true;
  239. }
  240. public static byte[] ConvertToBinary(string value)
  241. {
  242. try
  243. {
  244. string[] strs = value.Split(' ');
  245. byte[] bs = new byte[strs.Length];
  246. for(int i = 0; i < strs.Length; i++)
  247. {
  248. bs[i] = Convert.ToByte(strs[i], 16);
  249. }
  250. return bs;
  251. }
  252. catch { return null; }
  253. }
  254. }
  255. }