FastTools.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using PaintDotNet.Base.SettingModel;
  11. using PaintDotNet.Base.CommTool;
  12. using System.IO;
  13. namespace PaintDotNet.Instrument.CustomInterface
  14. {
  15. internal class FastTools : UserControl
  16. {
  17. /// <summary>
  18. /// 右侧树形菜单
  19. /// </summary>
  20. private ToolStripItemCollection collectionRight;
  21. /// <summary>
  22. /// 工作空间
  23. /// </summary>
  24. private AppWorkspace appWorkspace;
  25. /// <summary>
  26. /// 快捷菜单的数据
  27. /// </summary>
  28. private ShortcutbarModel shortcutbar_Model = Startup.instance.shortcutbarModel;
  29. private string txtPath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\ModuleConfig.txt";
  30. private string[] menuIdArr;
  31. #region 控件
  32. private GroupBox groupBoxLeft;
  33. private ListView listViewLeft;
  34. private ListViewItem listViewLeftDown = null;
  35. private ImageList imageListLeft;
  36. private GroupBox groupBoxRight;
  37. private TreeView treeViewRight;
  38. private Button buttonAdd;
  39. private Button buttonRemove;
  40. private Button buttonUp;
  41. private Button buttonDown;
  42. private IContainer components;
  43. #endregion
  44. public FastTools(AppWorkspace appWorkspace)
  45. {
  46. this.appWorkspace = appWorkspace;
  47. InitializeComponent();
  48. InitializeLanguageText();
  49. InitVisibleMenuId();
  50. InitializeLeftTreeData();
  51. InitializeRightTreeData();
  52. InitializeTreeEvent();
  53. }
  54. /// <summary>
  55. /// 获取txt文件中已保存的菜单可用id
  56. /// </summary>
  57. private void InitVisibleMenuId()
  58. {
  59. if (System.IO.File.Exists(txtPath))
  60. {
  61. string str = System.IO.File.ReadAllText(txtPath);
  62. if (str.IndexOf(',') != -1)
  63. {
  64. menuIdArr = str.Split(',');
  65. }
  66. else
  67. {
  68. if (!string.IsNullOrEmpty(str))
  69. {
  70. menuIdArr = new string[] { str };
  71. }
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// 初始化treeview的事件
  77. /// </summary>
  78. private void InitializeTreeEvent()
  79. {
  80. this.treeViewRight.HideSelection = false;
  81. this.treeViewRight.DrawMode = TreeViewDrawMode.OwnerDrawText;
  82. this.treeViewRight.DrawNode += new DrawTreeNodeEventHandler(this.treeViewRight_DrawNode);
  83. this.treeViewRight.Invalidated += new InvalidateEventHandler(this.treeViewRight_Invalidated);
  84. this.treeViewRight.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeViewRight_MouseDown);
  85. }
  86. /// <summary>
  87. /// 右侧功能列表鼠标按下事件
  88. /// </summary>
  89. /// <param name="sender"></param>
  90. /// <param name="e"></param>
  91. private void treeViewRight_MouseDown(object sender, MouseEventArgs e)
  92. {
  93. if ((sender as TreeView) != null && e.Clicks == 2 && this.buttonAdd.Enabled)
  94. {//自定义软件界面,双击可添加
  95. if (this.treeViewRight.GetNodeAt(e.X, e.Y) == null)
  96. return;
  97. }
  98. else
  99. return;
  100. //自定义软件界面,双击可添加
  101. TreeNode downNode = this.treeViewRight.GetNodeAt(e.X, e.Y);
  102. //if ((sender as TreeView) != null)
  103. //{
  104. // this.treeViewRight.SelectedNode = this.treeViewRight.GetNodeAt(e.X, e.Y);
  105. // this.treeViewRight.Refresh();
  106. //}
  107. if (/*this.treeViewRight.SelectedNode != null && */downNode.Nodes.Count == 0)
  108. {
  109. ToolStripItem[] items = this.appWorkspace.ToolBar.MainMenu.Items.Find(downNode.Name, true);
  110. if (items != null && items.Length > 0)
  111. {
  112. if (((PdnMenuItem)(items[0])).Image != null)
  113. this.listViewLeft.SmallImageList.Images.Add(downNode.Name, ((PdnMenuItem)(items[0])).Image);
  114. else
  115. this.listViewLeft.SmallImageList.Images.Add(downNode.Name, new Bitmap(16, 16));
  116. }
  117. else
  118. {
  119. this.listViewLeft.SmallImageList.Images.Add(downNode.Name, new Bitmap(16, 16));
  120. }
  121. ListViewItem item = new ListViewItem();
  122. item.Text = downNode.Text;
  123. item.Tag = downNode.Tag;
  124. item.Name = downNode.Name;
  125. item.ImageIndex = this.listViewLeft.SmallImageList.Images.Count - 1;
  126. this.listViewLeft.Items.Add(item);
  127. }
  128. }
  129. /// <summary>
  130. /// treeView1的重绘事件
  131. /// </summary>
  132. /// <param name="sender"></param>
  133. /// <param name="e"></param>
  134. private void treeViewRight_Invalidated(object sender, InvalidateEventArgs e)
  135. {
  136. }
  137. /// <summary>
  138. /// 自定义绘制
  139. /// 参考https://www.cnblogs.com/JiYF/p/6693503.html
  140. /// </summary>
  141. /// <param name="sender"></param>
  142. /// <param name="e"></param>
  143. private void treeViewRight_DrawNode(object sender, DrawTreeNodeEventArgs e)
  144. {
  145. /**用默认颜色
  146. e.DrawDefault = true;
  147. return;
  148. **/
  149. //以下是自定义颜色
  150. if ((e.State & TreeNodeStates.Selected) != 0)
  151. {
  152. //演示为蓝(绿)底白字
  153. e.Graphics.FillRectangle(SystemBrushes.Highlight/*Brushes.Green*/, e.Node.Bounds);
  154. Font nodeFont = e.Node.NodeFont;
  155. if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
  156. e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, Rectangle.Inflate(e.Bounds, 2, 0));
  157. }
  158. else
  159. {
  160. e.DrawDefault = true;
  161. }
  162. if ((e.State & TreeNodeStates.Focused) != 0)
  163. {
  164. using (Pen focusPen = new Pen(Color.Black))
  165. {
  166. focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
  167. Rectangle focusBounds = e.Node.Bounds;
  168. focusBounds.Size = new Size(focusBounds.Width - 1,
  169. focusBounds.Height - 1);
  170. e.Graphics.DrawRectangle(focusPen, focusBounds);
  171. }
  172. }
  173. }
  174. /// <summary>
  175. /// 初始化左侧listview菜单
  176. /// </summary>
  177. private void InitializeLeftTreeData()
  178. {
  179. this.listViewLeft.View = View.Details;
  180. ColumnHeader header = new ColumnHeader();
  181. header.Text = PdnResources.GetString("Menu.tool.Generateshortcut.functionlist.text");
  182. header.Width = 240;
  183. this.listViewLeft.Columns.Add(header);
  184. this.listViewLeft.Items.Clear();
  185. if (shortcutbar_Model!=null) {
  186. if (shortcutbar_Model.Menus != null)
  187. {
  188. for (int i=0; i< shortcutbar_Model.Menus.Count; i++)
  189. {
  190. ToolStripItem[] items = this.appWorkspace.ToolBar.MainMenu.Items.Find(shortcutbar_Model.Menus[i].Description, true);
  191. ListViewItem item = new ListViewItem();
  192. if (items != null && items.Length > 0)
  193. {
  194. if (((PdnMenuItem)(items[0])).Image != null)
  195. this.listViewLeft.SmallImageList.Images.Add(shortcutbar_Model.Menus[i].Description, ((PdnMenuItem)(items[0])).Image);
  196. else
  197. this.listViewLeft.SmallImageList.Images.Add(shortcutbar_Model.Menus[i].Description, new Bitmap(16, 16));
  198. item.Text = ((PdnMenuItem)(items[0])).Text;
  199. }
  200. else
  201. {
  202. this.listViewLeft.SmallImageList.Images.Add(shortcutbar_Model.Menus[i].Description, new Bitmap(16, 16));
  203. item.Text = shortcutbar_Model.Menus[i].Name;// getShowName(Startup.instance.configModel.Language);
  204. }
  205. item.Tag = shortcutbar_Model.Menus[i].Id;
  206. item.Name = shortcutbar_Model.Menus[i].Description;
  207. item.ImageIndex = i;
  208. this.listViewLeft.Items.Add(item);
  209. }
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// 初始化右侧树形菜单数据
  215. /// </summary>
  216. private void InitializeRightTreeData()
  217. {
  218. this.collectionRight = this.appWorkspace.ToolBar.MainMenu.Items;
  219. TreeNode anime = new TreeNode(PdnResources.GetString("Menu.menu.Text"));
  220. this.RecursiveData(collectionRight, anime);
  221. anime.Expand();
  222. this.treeViewRight.Nodes.Add(anime);
  223. }
  224. /// <summary>
  225. /// 递归进行数据组织
  226. /// </summary>
  227. private void RecursiveData(ToolStripItemCollection collection, TreeNode anime)
  228. {
  229. //for (int i = 0; i < collection.Count; i++)
  230. //{
  231. // //排除掉最近打开的文件,或者可以用数字id判断更准确
  232. // if (!collection[i].Name.Equals("OpenRecent") && !collection[i].Name.Equals("CameraSelection"))
  233. // {
  234. // if (collection[i].GetType() != typeof(ToolStripSeparator) && ((PdnMenuItem)collection[i]).CanShowInSenseShield)
  235. // {
  236. // TreeNode node = new TreeNode();
  237. // node.Name = collection[i].Name;
  238. // node.Text = collection[i].Text;// "打开(&Open)";//
  239. // node.Tag = ((PdnMenuItem)collection[i]).MenuId;
  240. // anime.Nodes.Add(node);
  241. // RecursiveData(((PdnMenuItem)collection[i]).DropDownItems, node);
  242. // }
  243. // }
  244. //}
  245. for (int i = 0; i < collection.Count; i++)
  246. {
  247. TreeNode node = new TreeNode(/*collection[i].Text*/);
  248. if (collection[i].GetType() != typeof(ToolStripSeparator))
  249. {
  250. PdnMenuItem item = (PdnMenuItem)collection[i];
  251. if (!item.CanShowInSenseShield)
  252. continue;
  253. node.Tag = item.MenuId;
  254. if (menuIdArr != null && menuIdArr.Length > 0)
  255. {
  256. if (Array.IndexOf(menuIdArr, item.MenuId.ToString()) != -1)
  257. {
  258. node.Name = collection[i].Name;
  259. node.Text = collection[i].Text;
  260. node.Tag = ((PdnMenuItem)collection[i]).MenuId;
  261. node.Checked = true;
  262. }
  263. else
  264. {
  265. node.Checked = false;
  266. }
  267. }
  268. if (node.Checked)
  269. {
  270. anime.Nodes.Add(node);
  271. }
  272. if (collection[i].Name.Equals("OpenRecent") || collection[i].Name.Equals("CameraSelection"))
  273. continue;
  274. RecursiveData(((PdnMenuItem)collection[i]).DropDownItems, node);
  275. }
  276. }
  277. }
  278. #region 组件设计器生成的代码
  279. private void InitializeLanguageText()
  280. {
  281. this.buttonDown.Text = PdnResources.GetString("Menu.LabelAction.MoveDownAction.Text");
  282. this.buttonUp.Text = PdnResources.GetString("Menu.LabelAction.MoveUpAction.Text");
  283. this.buttonRemove.Text = PdnResources.GetString("Menu.Moveout.text")+" >";
  284. this.buttonAdd.Text = "< "+ PdnResources.GetString("Menu.Addto.text");
  285. this.groupBoxRight.Text = PdnResources.GetString("Menu.Availablefunctions.text");
  286. this.groupBoxLeft.Text = PdnResources.GetString("Menu.tool.Generateshortcut.Shortcutbarcontent.text");
  287. }
  288. /// <summary>
  289. /// 设计器支持所需的方法 - 不要修改
  290. /// 使用代码编辑器修改此方法的内容。
  291. /// </summary>
  292. private void InitializeComponent()
  293. {
  294. this.components = new System.ComponentModel.Container();
  295. this.groupBoxLeft = new System.Windows.Forms.GroupBox();
  296. this.listViewLeft = new System.Windows.Forms.ListView();
  297. this.imageListLeft = new System.Windows.Forms.ImageList(this.components);
  298. this.groupBoxRight = new System.Windows.Forms.GroupBox();
  299. this.treeViewRight = new System.Windows.Forms.TreeView();
  300. this.buttonAdd = new System.Windows.Forms.Button();
  301. this.buttonRemove = new System.Windows.Forms.Button();
  302. this.buttonUp = new System.Windows.Forms.Button();
  303. this.buttonDown = new System.Windows.Forms.Button();
  304. this.groupBoxLeft.SuspendLayout();
  305. this.groupBoxRight.SuspendLayout();
  306. this.SuspendLayout();
  307. //
  308. // groupBoxLeft
  309. //
  310. this.groupBoxLeft.Controls.Add(this.listViewLeft);
  311. this.groupBoxLeft.Location = new System.Drawing.Point(0, 0);
  312. this.groupBoxLeft.Name = "groupBoxLeft";
  313. this.groupBoxLeft.Size = new System.Drawing.Size(256, 398);
  314. this.groupBoxLeft.TabIndex = 0;
  315. this.groupBoxLeft.TabStop = false;
  316. this.groupBoxLeft.Text = "快捷栏内容";
  317. //
  318. // listViewLeft
  319. //
  320. this.listViewLeft.HideSelection = false;
  321. this.listViewLeft.Location = new System.Drawing.Point(7, 20);
  322. this.listViewLeft.MultiSelect = false;
  323. this.listViewLeft.Name = "listViewLeft";
  324. this.listViewLeft.Size = new System.Drawing.Size(243, 372);
  325. this.listViewLeft.SmallImageList = this.imageListLeft;
  326. this.listViewLeft.TabIndex = 0;
  327. this.listViewLeft.UseCompatibleStateImageBehavior = false;
  328. this.listViewLeft.MouseDown += ListViewLeft_MouseDown;
  329. this.listViewLeft.DrawItem += new System.Windows.Forms.DrawListViewItemEventHandler(this.listViewLeft_DrawItem);
  330. this.listViewLeft.Invalidated += new InvalidateEventHandler(this.treeViewLeft_InvalidateEvent);
  331. //
  332. // imageListLeft
  333. //
  334. this.imageListLeft.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
  335. this.imageListLeft.ImageSize = new System.Drawing.Size(16, 16);
  336. this.imageListLeft.TransparentColor = System.Drawing.Color.Transparent;
  337. //
  338. // groupBoxRight
  339. //
  340. this.groupBoxRight.Controls.Add(this.treeViewRight);
  341. this.groupBoxRight.Location = new System.Drawing.Point(367, 0);
  342. this.groupBoxRight.Name = "groupBoxRight";
  343. this.groupBoxRight.Size = new System.Drawing.Size(256, 398);
  344. this.groupBoxRight.TabIndex = 1;
  345. this.groupBoxRight.TabStop = false;
  346. this.groupBoxRight.Text = "可用功能";
  347. //
  348. // treeViewRight
  349. //
  350. this.treeViewRight.Location = new System.Drawing.Point(7, 20);
  351. this.treeViewRight.Name = "treeViewRight";
  352. this.treeViewRight.Size = new System.Drawing.Size(243, 372);
  353. this.treeViewRight.TabIndex = 0;
  354. //
  355. // buttonAdd
  356. //
  357. this.buttonAdd.Location = new System.Drawing.Point(277, 20);
  358. this.buttonAdd.Name = "buttonAdd";
  359. this.buttonAdd.Size = new System.Drawing.Size(75, 23);
  360. this.buttonAdd.TabIndex = 3;
  361. this.buttonAdd.Text = "< 添加";
  362. this.buttonAdd.UseVisualStyleBackColor = true;
  363. this.buttonAdd.Click += new System.EventHandler(this.buttonAddClick);
  364. //
  365. // buttonRemove
  366. //
  367. this.buttonRemove.Location = new System.Drawing.Point(277, 49);
  368. this.buttonRemove.Name = "buttonRemove";
  369. this.buttonRemove.Size = new System.Drawing.Size(75, 23);
  370. this.buttonRemove.TabIndex = 4;
  371. this.buttonRemove.Text = "移出 >";
  372. this.buttonRemove.UseVisualStyleBackColor = true;
  373. this.buttonRemove.Click += new System.EventHandler(this.buttonRemoveClick);
  374. //
  375. // buttonUp
  376. //
  377. this.buttonUp.Location = new System.Drawing.Point(277, 78);
  378. this.buttonUp.Name = "buttonUp";
  379. this.buttonUp.Size = new System.Drawing.Size(75, 23);
  380. this.buttonUp.TabIndex = 5;
  381. this.buttonUp.Text = "向上移动";
  382. this.buttonUp.UseVisualStyleBackColor = true;
  383. this.buttonUp.Click += new System.EventHandler(this.buttonUpClick);
  384. //
  385. // buttonDown
  386. //
  387. this.buttonDown.Location = new System.Drawing.Point(277, 107);
  388. this.buttonDown.Name = "buttonDown";
  389. this.buttonDown.Size = new System.Drawing.Size(75, 23);
  390. this.buttonDown.TabIndex = 6;
  391. this.buttonDown.Text = "向下移动";
  392. this.buttonDown.UseVisualStyleBackColor = true;
  393. this.buttonDown.Click += new System.EventHandler(this.buttonDownClick);
  394. //
  395. // FastTools
  396. //
  397. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  398. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  399. this.Controls.Add(this.buttonDown);
  400. this.Controls.Add(this.buttonUp);
  401. this.Controls.Add(this.buttonRemove);
  402. this.Controls.Add(this.buttonAdd);
  403. this.Controls.Add(this.groupBoxRight);
  404. this.Controls.Add(this.groupBoxLeft);
  405. this.Name = "FastTools";
  406. this.Size = new System.Drawing.Size(623, 398);
  407. this.groupBoxLeft.ResumeLayout(false);
  408. this.groupBoxRight.ResumeLayout(false);
  409. this.ResumeLayout(false);
  410. }
  411. private void ListViewLeft_MouseDown(object sender, MouseEventArgs e)
  412. {
  413. if ((sender as ListView) != null)
  414. {
  415. listViewLeftDown = this.listViewLeft.GetItemAt(e.X, e.Y);
  416. this.listViewLeft.Refresh();
  417. }
  418. //this.listViewLeft.Refresh();
  419. }
  420. #endregion
  421. /// <summary>
  422. /// 添加
  423. /// </summary>
  424. /// <param name="sender"></param>
  425. /// <param name="e"></param>
  426. private void buttonAddClick(object sender, EventArgs e)
  427. {
  428. if (this.treeViewRight.SelectedNode!=null && this.treeViewRight.SelectedNode.Nodes.Count==0)
  429. {
  430. ToolStripItem[] items = this.appWorkspace.ToolBar.MainMenu.Items.Find(this.treeViewRight.SelectedNode.Name, true);
  431. if (items != null && items.Length > 0)
  432. {
  433. if (((PdnMenuItem)(items[0])).Image != null)
  434. this.listViewLeft.SmallImageList.Images.Add(this.treeViewRight.SelectedNode.Name, ((PdnMenuItem)(items[0])).Image);
  435. else
  436. this.listViewLeft.SmallImageList.Images.Add(this.treeViewRight.SelectedNode.Name, new Bitmap(16, 16));
  437. }
  438. else
  439. {
  440. this.listViewLeft.SmallImageList.Images.Add(this.treeViewRight.SelectedNode.Name, new Bitmap(16, 16));
  441. }
  442. ListViewItem item = new ListViewItem();
  443. item.Text = this.treeViewRight.SelectedNode.Text;
  444. item.Tag = this.treeViewRight.SelectedNode.Tag;
  445. item.Name = this.treeViewRight.SelectedNode.Name;
  446. item.ImageIndex = this.listViewLeft.SmallImageList.Images.Count-1;
  447. this.listViewLeft.Items.Add(item);
  448. }
  449. }
  450. /// <summary>
  451. /// 移出
  452. /// </summary>
  453. /// <param name="sender"></param>
  454. /// <param name="e"></param>
  455. private void buttonRemoveClick(object sender, EventArgs e)
  456. {
  457. if (this.listViewLeft.SelectedItems.Count > 0)
  458. {
  459. foreach(ListViewItem item in this.listViewLeft.SelectedItems)
  460. this.listViewLeft.Items.Remove(item);
  461. }
  462. }
  463. /// <summary>
  464. /// 向上移动
  465. /// </summary>
  466. /// <param name="sender"></param>
  467. /// <param name="e"></param>
  468. private void buttonUpClick(object sender, EventArgs e)
  469. {
  470. if (this.listViewLeft.SelectedItems.Count == 1)
  471. {
  472. int index = this.listViewLeft.Items.IndexOf(this.listViewLeft.SelectedItems[0]);
  473. if (index > 0)
  474. {
  475. ListViewItem prenode = (ListViewItem)this.listViewLeft.Items[index-1].Clone();
  476. prenode.Name = this.listViewLeft.Items[index - 1].Name;
  477. this.listViewLeft.Items.Insert(index + 1, prenode);
  478. this.listViewLeft.Items.Remove(this.listViewLeft.Items[index - 1]);
  479. this.listViewLeft.Refresh();
  480. }
  481. }
  482. }
  483. /// <summary>
  484. /// 向下移动
  485. /// </summary>
  486. /// <param name="sender"></param>
  487. /// <param name="e"></param>
  488. private void buttonDownClick(object sender, EventArgs e)
  489. {
  490. if (this.listViewLeft.SelectedItems.Count == 1)
  491. {
  492. int index = this.listViewLeft.Items.IndexOf(this.listViewLeft.SelectedItems[0]);
  493. if (index < this.listViewLeft.Items.Count-1)
  494. {
  495. ListViewItem nextnode = (ListViewItem)this.listViewLeft.Items[index + 1].Clone();
  496. nextnode.Name = this.listViewLeft.Items[index + 1].Name;
  497. this.listViewLeft.Items.Insert(index, nextnode);
  498. this.listViewLeft.Items.Remove(this.listViewLeft.Items[index + 2]);
  499. this.listViewLeft.Refresh();
  500. }
  501. }
  502. }
  503. private void listViewLeft_DrawItem(object sender, DrawListViewItemEventArgs e)
  504. {
  505. e.DrawDefault = true;
  506. return;
  507. }
  508. /// <summary>
  509. /// treeViewLeft的重绘事件
  510. /// </summary>
  511. /// <param name="sender"></param>
  512. /// <param name="e"></param>
  513. private void treeViewLeft_InvalidateEvent(object sender, InvalidateEventArgs e)
  514. {
  515. //1009###17142
  516. if (listViewLeftDown == null)
  517. {
  518. //this.buttonUp.Enabled = false;
  519. //this.buttonDown.Enabled = false;
  520. if (this.listViewLeft.SelectedItems.Count == 1)
  521. {
  522. int index = this.listViewLeft.Items.IndexOf(this.listViewLeft.SelectedItems[0]);
  523. if (index > 0)
  524. this.buttonUp.Enabled = true;
  525. else
  526. this.buttonUp.Enabled = false;
  527. }
  528. else
  529. this.buttonUp.Enabled = false;
  530. if (this.listViewLeft.SelectedItems.Count == 1)
  531. {
  532. int index = this.listViewLeft.Items.IndexOf(this.listViewLeft.SelectedItems[0]);
  533. if (index < this.listViewLeft.Items.Count - 1)
  534. this.buttonDown.Enabled = true;
  535. else
  536. this.buttonDown.Enabled = false;
  537. }
  538. else
  539. this.buttonDown.Enabled = false;
  540. }
  541. else
  542. {
  543. int index = this.listViewLeft.Items.IndexOf(listViewLeftDown);
  544. if (index > 0)
  545. this.buttonUp.Enabled = true;
  546. else
  547. this.buttonUp.Enabled = false;
  548. index = this.listViewLeft.Items.IndexOf(listViewLeftDown);
  549. if (index < this.listViewLeft.Items.Count - 1)
  550. this.buttonDown.Enabled = true;
  551. else
  552. this.buttonDown.Enabled = false;
  553. listViewLeftDown = null;
  554. }
  555. }
  556. /// <summary>
  557. /// 保存数据到xml
  558. /// </summary>
  559. public void SaveToolbar()
  560. {
  561. shortcutbar_Model.Menus.Clear();
  562. for (int i = 0; i < this.listViewLeft.Items.Count; i++)
  563. {
  564. ShortcutbarModel.Item flow = new ShortcutbarModel.Item();
  565. flow.Id = (int)(this.listViewLeft.Items[i].Tag);
  566. flow.Name = this.listViewLeft.Items[i].Text;
  567. //################
  568. flow.Description = this.listViewLeft.Items[i].Name;
  569. shortcutbar_Model.Menus.Add(flow);
  570. }
  571. string userInfoXml = XmlSerializeHelper.XmlSerialize<ShortcutbarModel>(shortcutbar_Model);
  572. string filePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\Shortcutbar.xml";
  573. if (FileOperationHelper.WriteStringToFile(userInfoXml, filePath, FileMode.Create))
  574. {
  575. //更新全局数据
  576. Startup.instance.shortcutbarModel = shortcutbar_Model;
  577. //this.appWorkspace.mainToolBarForm.setCilentSize();
  578. //this.appWorkspace.mainToolBarForm.ToolsControl.Refresh();
  579. this.appWorkspace.toolsPanel.RefreshTools();
  580. }
  581. else
  582. {
  583. MessageBox.Show(PdnResources.GetString("Menu.Shortcuttoolbarsavefailed.text"));
  584. }
  585. }
  586. /// <summary>
  587. /// 导出配置
  588. /// </summary>
  589. /// <returns></returns>
  590. public void ExportToolbarXml()
  591. {
  592. using (SaveFileDialog saveFileDialog = new SaveFileDialog())
  593. {
  594. saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  595. saveFileDialog.Filter = "Xml(*.xml)|*.xml";
  596. saveFileDialog.DefaultExt = "xml";
  597. saveFileDialog.FilterIndex = 1;
  598. saveFileDialog.CheckFileExists = false;
  599. saveFileDialog.AddExtension = true;
  600. saveFileDialog.FileName = "Shortcutbar";
  601. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  602. {
  603. string filePath = saveFileDialog.FileName;
  604. string toolbarXml = XmlSerializeHelper.XmlSerialize<ShortcutbarModel>(Startup.instance.shortcutbarModel);
  605. if (FileOperationHelper.WriteStringToFile(toolbarXml, filePath, FileMode.Create))
  606. MessageBox.Show(PdnResources.GetString("Menu.lbarconfigurationfilewassavedsuccessfu.text"));
  607. else
  608. MessageBox.Show(PdnResources.GetString("Menu.Theshortcuttoolbarsave.text"));
  609. }
  610. }
  611. }
  612. /// <summary>
  613. /// 导入配置
  614. /// </summary>
  615. /// <returns></returns>
  616. public void ImportToolbarXml()
  617. {
  618. using (var openFileDialog = new OpenFileDialog { Filter = "*.xml|*.xml" })
  619. {
  620. if (openFileDialog.ShowDialog() == DialogResult.OK)
  621. {
  622. using (Stream shortbarStream = System.IO.File.OpenRead(openFileDialog.FileName))
  623. {
  624. StreamReader sr = new StreamReader(shortbarStream);
  625. string xmlNotes = sr.ReadToEnd();
  626. ShortcutbarModel toolbarXml = XmlSerializeHelper.DESerializer<ShortcutbarModel>(xmlNotes);
  627. if (toolbarXml.Menus.Count > 0)
  628. {
  629. try
  630. {
  631. this.listViewLeft.Items.Clear();
  632. this.listViewLeft.SmallImageList = new ImageList();
  633. this.listViewLeft.BeginUpdate();
  634. for (int i = 0; i < toolbarXml.Menus.Count; i++)
  635. {
  636. ToolStripItem[] items = this.appWorkspace.ToolBar.MainMenu.Items.Find(toolbarXml.Menus[i].Description, true);
  637. ListViewItem listItem = new ListViewItem();
  638. if (items != null && items.Length > 0)
  639. {
  640. if (((PdnMenuItem)(items[0])).Image != null)
  641. this.listViewLeft.SmallImageList.Images.Add(((PdnMenuItem)(items[0])).Text, ((PdnMenuItem)(items[0])).Image);
  642. else
  643. this.listViewLeft.SmallImageList.Images.Add(((PdnMenuItem)(items[0])).Text, new Bitmap(16, 16));
  644. listItem.Text = ((PdnMenuItem)(items[0])).Text;
  645. }
  646. else
  647. {
  648. this.listViewLeft.SmallImageList.Images.Add(toolbarXml.Menus[i].Name/*getShowName(Startup.instance.configModel.Language)*/, new Bitmap(16, 16));
  649. listItem.Text = toolbarXml.Menus[i].Name;// getShowName(Startup.instance.configModel.Language);
  650. }
  651. listItem.Tag = toolbarXml.Menus[i].Id;
  652. listItem.Name = toolbarXml.Menus[i].Description;
  653. listItem.ImageIndex = i;
  654. this.listViewLeft.Items.Add(listItem);
  655. }
  656. this.listViewLeft.EndUpdate();
  657. MessageBox.Show(PdnResources.GetString("Menu.Shortcuttoolbasuccessfully.text"));
  658. }
  659. catch(Exception)
  660. {
  661. this.listViewLeft.Items.Clear();
  662. this.listViewLeft.SmallImageList = new ImageList();
  663. MessageBox.Show(PdnResources.GetString("Menu.Theshortcutconfigurationfileimportailed.text"));
  664. }
  665. }
  666. else
  667. {
  668. MessageBox.Show(PdnResources.GetString("Menu.onfiguratiofilesavedconfiguratio.text"));
  669. }
  670. }
  671. }
  672. }
  673. }
  674. }
  675. }