PdnMenuItem.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. using Resources;
  2. using SmartCoalApplication.Base;
  3. using SmartCoalApplication.Core;
  4. using System;
  5. using System.ComponentModel;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Reflection;
  9. using System.Windows.Forms;
  10. namespace SmartCoalApplication
  11. {
  12. internal class PdnMenuItem : ToolStripMenuItem
  13. {
  14. /// <summary>
  15. /// 是否可以用在脚本功能里,默认true
  16. /// </summary>
  17. private bool canUseInScript = true;
  18. /// <summary>
  19. /// true可以自动执行 false只能手动执行,默认true
  20. /// </summary>
  21. private bool automaticScript = true;
  22. /// <summary>
  23. /// true自动执行下一步 false手动点击下一步执行,默认false
  24. /// </summary>
  25. private bool autoNextScript = false;
  26. /// <summary>
  27. /// 脚本是否需要加等待执行完成后才能继续的处理
  28. /// </summary>
  29. private bool needWaitKey = false;
  30. /// <summary>
  31. /// 是否可以选中
  32. /// </summary>
  33. public bool canChecked = false;
  34. /// <summary>
  35. /// AppWorkspace的引用
  36. /// </summary>
  37. private AppWorkspace appWorkspace;
  38. private string textResourceName = null;
  39. private const char noMnemonicChar = (char)0;
  40. private const char mnemonicPrefix = '&';
  41. /// <summary>
  42. /// icon loaded的标记
  43. /// </summary>
  44. private bool iconsLoaded = false;
  45. /// <summary>
  46. /// 标题 loaded的标记
  47. /// </summary>
  48. public bool namesLoaded = false;
  49. /// <summary>
  50. /// 当前菜单的唯一id
  51. /// </summary>
  52. private int menuId;
  53. public int MenuId
  54. {
  55. get
  56. {
  57. return this.menuId;
  58. }
  59. set
  60. {
  61. this.menuId = value;
  62. }
  63. }
  64. public bool CanUseInSenseShield
  65. {
  66. get
  67. {
  68. return true;
  69. }
  70. }
  71. /// <summary>
  72. /// 是否可以用在脚本功能里,默认true
  73. /// </summary>
  74. public bool CanUseInScript
  75. {
  76. get
  77. {
  78. return this.canUseInScript;
  79. }
  80. set
  81. {
  82. this.canUseInScript = value;
  83. }
  84. }
  85. /// <summary>
  86. /// true可以自动执行 false只能手动执行,默认true
  87. /// </summary>
  88. public bool AutomaticScript
  89. {
  90. get
  91. {
  92. return this.automaticScript;
  93. }
  94. set
  95. {
  96. this.automaticScript = value;
  97. }
  98. }
  99. /// <summary>
  100. /// true自动执行下一步 false手动点击下一步执行,默认false
  101. /// </summary>
  102. public bool AutoNextScript
  103. {
  104. get
  105. {
  106. return this.autoNextScript;
  107. }
  108. set
  109. {
  110. this.autoNextScript = value;
  111. }
  112. }
  113. /// <summary>
  114. /// 脚本是否需要加等待执行完成后才能继续的处理
  115. /// </summary>
  116. public bool NeedWaitKey
  117. {
  118. get
  119. {
  120. return this.needWaitKey;
  121. }
  122. set
  123. {
  124. this.needWaitKey = value;
  125. }
  126. }
  127. [Browsable(false)]
  128. public AppWorkspace AppWorkspace
  129. {
  130. get
  131. {
  132. return this.appWorkspace;
  133. }
  134. set
  135. {
  136. if (value != this.appWorkspace)
  137. {
  138. OnAppWorkspaceChanging();
  139. this.appWorkspace = value;
  140. OnAppWorkspaceChanged();
  141. }
  142. }
  143. }
  144. /// <summary>
  145. /// 是否授权显示该菜单
  146. /// </summary>
  147. public bool CanShowInSenseShield
  148. {
  149. get
  150. {
  151. return Program.getMenuIdVisible(this.menuId);
  152. }
  153. }
  154. public Form AssociatedForm
  155. {
  156. get
  157. {
  158. if (this.appWorkspace == null)
  159. {
  160. return null;
  161. }
  162. else
  163. {
  164. return this.appWorkspace.FindForm();
  165. }
  166. }
  167. }
  168. public new Keys ShortcutKeys
  169. {
  170. get
  171. {
  172. return base.ShortcutKeys;
  173. }
  174. set
  175. {
  176. if (ShortcutKeys != Keys.None)
  177. {
  178. PdnBaseForm.UnregisterFormHotKey(ShortcutKeys, OnShortcutKeyPressed);
  179. }
  180. PdnBaseForm.RegisterFormHotKey(value, OnShortcutKeyPressed);
  181. base.ShortcutKeys = value;
  182. }
  183. }
  184. public void resetShortcutKeys(string itemHotKeys)
  185. {
  186. if (itemHotKeys != null && !itemHotKeys.Equals(""))
  187. {
  188. string[] keys = itemHotKeys.Trim().Split('+');
  189. Keys Key = Keys.None;
  190. foreach (string key in keys)
  191. {
  192. if (Key == Keys.None)
  193. {
  194. Key = (Keys)(Enum.Parse(typeof(Keys), key.Trim()));
  195. }
  196. else
  197. {
  198. Key = (Keys)(Enum.Parse(typeof(Keys), key.Trim())) | Key;
  199. }
  200. }
  201. this.ShortcutKeys = Key;
  202. }
  203. else
  204. {
  205. this.ShortcutKeys = Keys.None;
  206. }
  207. }
  208. public bool HasMnemonic
  209. {
  210. get
  211. {
  212. return (Mnemonic != noMnemonicChar);
  213. }
  214. }
  215. public char Mnemonic
  216. {
  217. get
  218. {
  219. if (string.IsNullOrEmpty(this.Text))
  220. {
  221. return noMnemonicChar;
  222. }
  223. int mnemonicPrefixIndex = this.Text.IndexOf(mnemonicPrefix);
  224. if (mnemonicPrefixIndex >= 0 && mnemonicPrefixIndex < this.Text.Length - 1)
  225. {
  226. return this.Text[mnemonicPrefixIndex + 1];
  227. }
  228. else
  229. {
  230. return noMnemonicChar;
  231. }
  232. }
  233. }
  234. public void PerformClickAsync()
  235. {
  236. this.Owner.BeginInvoke(new Procedure(this.PerformClick));
  237. }
  238. protected virtual void OnAppWorkspaceChanging()
  239. {
  240. foreach (ToolStripItem item in this.DropDownItems)
  241. {
  242. PdnMenuItem asPMI = item as PdnMenuItem;
  243. if (asPMI != null)
  244. {
  245. asPMI.AppWorkspace = null;
  246. }
  247. }
  248. }
  249. protected virtual void OnAppWorkspaceChanged()
  250. {
  251. foreach (ToolStripItem item in this.DropDownItems)
  252. {
  253. PdnMenuItem asPMI = item as PdnMenuItem;
  254. if (asPMI != null)
  255. {
  256. asPMI.AppWorkspace = AppWorkspace;
  257. }
  258. }
  259. }
  260. public PdnMenuItem(string name, Image image, EventHandler eventHandler)
  261. : base(name, image, eventHandler)
  262. {
  263. Constructor();
  264. }
  265. /// <summary>
  266. /// 目前用的构造方法
  267. /// </summary>
  268. /// <param name="type"></param>
  269. public PdnMenuItem(ActionType type)
  270. {
  271. Constructor();
  272. //设置快捷键,如果配置了
  273. SetHotKeyFromConfig((int)type);
  274. //设置名称
  275. this.Name = GetDescription(type);
  276. //设置id
  277. this.MenuId = (int)type;
  278. }
  279. public PdnMenuItem(ActionType type, bool canChecked)
  280. {
  281. Constructor();
  282. //设置快捷键,如果配置了
  283. SetHotKeyFromConfig((int)type);
  284. //设置名称
  285. this.Name = GetDescription(type);
  286. //设置id
  287. this.MenuId = (int)type;
  288. //设置可以被选中
  289. this.canChecked = canChecked;
  290. }
  291. public PdnMenuItem()
  292. {
  293. Constructor();
  294. }
  295. private void Constructor()
  296. {
  297. InitializeComponent();
  298. }
  299. private void InitializeComponent()
  300. {
  301. this.DropDownOpening += new EventHandler(PdnMenuItem_DropDownOpening);
  302. }
  303. private bool OnShortcutKeyPressed(Keys keys)
  304. {
  305. PerformClick();
  306. return true;
  307. }
  308. private void PdnMenuItem_DropDownOpening(object sender, EventArgs e)
  309. {
  310. OnDropDownOpening(e);
  311. }
  312. protected virtual void OnDropDownOpening(EventArgs e)
  313. {
  314. if (!this.namesLoaded)
  315. {
  316. LoadNames(this.Name);
  317. }
  318. if (!this.iconsLoaded)
  319. {
  320. LoadIcons();
  321. }
  322. }
  323. public void LoadNames(string baseName)
  324. {
  325. foreach (ToolStripItem item in this.DropDownItems)
  326. {
  327. if (!string.IsNullOrWhiteSpace(item.Name))
  328. {
  329. string itemNameBase = baseName + "." + item.Name;
  330. string itemNameText = itemNameBase + ".Text";
  331. string text = PdnResources.GetString(itemNameText);
  332. if (text != null)
  333. {
  334. item.Text = text;
  335. }
  336. PdnMenuItem pmi = item as PdnMenuItem;
  337. if (pmi != null)
  338. {
  339. pmi.textResourceName = itemNameText;
  340. pmi.LoadNames(itemNameBase);
  341. }
  342. }
  343. }
  344. this.namesLoaded = true;
  345. }
  346. public void SetIcon(string imageName)
  347. {
  348. this.ImageTransparentColor = Utility.TransparentKey;
  349. this.Image = PdnResources.GetImageResource(imageName).Reference;
  350. }
  351. public void SetIcon(ImageResource image)
  352. {
  353. this.ImageTransparentColor = Utility.TransparentKey;
  354. this.Image = image.Reference;
  355. }
  356. public void LoadIcons()
  357. {
  358. Type ourType = this.GetType();
  359. FieldInfo[] fields = ourType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
  360. foreach (FieldInfo fi in fields)
  361. {
  362. if (fi.FieldType.IsSubclassOf(typeof(PdnMenuItem)) ||
  363. fi.FieldType == typeof(PdnMenuItem))
  364. {
  365. string iconFileName = "Icons." + fi.Name[0].ToString().ToUpper() + fi.Name.Substring(1) + "Icon.png";
  366. PdnMenuItem mi = (PdnMenuItem)fi.GetValue(this);
  367. Stream iconStream = PdnResources.GetResourceStream(iconFileName);
  368. if (iconStream != null)
  369. {
  370. iconStream.Dispose();
  371. mi.SetIcon(iconFileName);
  372. }
  373. }
  374. }
  375. this.iconsLoaded = true;
  376. }
  377. protected override void OnDropDownClosed(EventArgs e)
  378. {
  379. foreach (ToolStripItem item in this.DropDownItems)
  380. {
  381. item.Enabled = true;
  382. }
  383. base.OnDropDownClosed(e);
  384. }
  385. protected override void OnClick(EventArgs e)
  386. {
  387. if ((this.DropDownItems == null || this.DropDownItems.Count == 0) && !this.CanUseInSenseShield)
  388. {
  389. string featureNameNotShield = this.Name ?? this.Text;
  390. return;
  391. }
  392. if (Form.ActiveForm != null)
  393. {
  394. Form.ActiveForm.BeginInvoke(new Procedure(PdnBaseForm.UpdateAllForms));
  395. }
  396. string featureName = this.Name ?? this.Text;
  397. base.OnClick(e);
  398. }
  399. private void SetHotKeyFromConfig(int menuId)
  400. {
  401. /*if (Startup.instance.hotkeyModel != null
  402. && Startup.instance.hotkeyModel.items != null
  403. && Startup.instance.hotkeyModel.items.Count > 0)
  404. {
  405. HotkeyModel.Item item = Startup.instance.hotkeyModel.items.Find(m => m.MenuId == menuId);
  406. if (item != null)
  407. {
  408. if (item.HotKeys != null && !item.HotKeys.Equals(""))
  409. {
  410. string[] keys = item.HotKeys.Trim().Split('+');
  411. Keys Key = Keys.None;
  412. foreach (string key in keys)
  413. {
  414. if (Key == Keys.None)
  415. {
  416. Key = (Keys)(Enum.Parse(typeof(Keys), key.Trim()));
  417. }
  418. else
  419. {
  420. Key = (Keys)(Enum.Parse(typeof(Keys), key.Trim())) | Key;
  421. }
  422. }
  423. this.ShortcutKeys = Key;
  424. }
  425. }
  426. }*/
  427. }
  428. /// <summary>
  429. /// 获取枚举的描述
  430. /// </summary>
  431. /// <param name="en">枚举</param>
  432. /// <returns>返回枚举的描述</returns>
  433. public static string GetDescription(Enum en)
  434. {
  435. Type type = en.GetType(); //获取类型
  436. MemberInfo[] memberInfos = type.GetMember(en.ToString()); //获取成员
  437. if (memberInfos != null && memberInfos.Length > 0)
  438. {
  439. DescriptionAttribute[] attrs = memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; //获取描述特性
  440. if (attrs != null && attrs.Length > 0)
  441. {
  442. return attrs[0].Description; //返回当前描述
  443. }
  444. }
  445. return en.ToString();
  446. }
  447. }
  448. }