123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- using PaintDotNet.Base.CommTool;
- using PaintDotNet.Base.SettingModel;
- using PaintDotNet.CustomControl;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Windows.Forms;
- using System.Windows.Forms.VisualStyles;
- namespace PaintDotNet.Setting
- {
- /// <summary>
- /// 设置->模块管理
- /// </summary>
- internal class ModuleManageDialog : PdnBaseForm
- {
- private AppWorkspace appWorkspace;
- private ToolStripItemCollection collectionLeft;
- private TreeNode animeLeft;
- private string txtPath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\ModuleConfig.txt";
- private string[] menuIdArr;
- public ModuleManageDialog(AppWorkspace appWorkspace)
- {
- this.appWorkspace = appWorkspace;
- this.ShowInTaskbar = false;
- InitializeComponent();
- InitializeLanguageText();
- this.treeView1.CheckBoxes = true;
- InitVisibleMenuId();
- InitLeftTreeViewData();
- this.Text = PdnResources.GetString("Menu.Setting.ModuleSetting.Text");
- }
- private void InitializeLanguageText()
- {
- this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
- this.button2.Text = PdnResources.GetString("ConfirmLanguageDialog.CancelTB.ActionText");
- this.button1.Text = PdnResources.GetString("Menu.application.text");
- this.groupBox2.Text = PdnResources.GetString("Menu.Set.Focusparams.Availablemodules.text");
- this.label1.Text = PdnResources.GetString("Menu.Set.Modulemanagement.Restartthesngs.text");
- }
- /// <summary>
- /// 获取txt文件中已保存的菜单可用id
- /// </summary>
- private void InitVisibleMenuId()
- {
- if (System.IO.File.Exists(txtPath))
- {
- string str = System.IO.File.ReadAllText(txtPath);
- if (str.IndexOf(',') != -1)
- {
- menuIdArr = str.Split(',');
- }
- else
- {
- if (!string.IsNullOrEmpty(str))
- {
- menuIdArr = new string[] { str };
- }
- }
- }
- }
- /// <summary>
- /// 初始化左侧treeview数据
- /// </summary>
- private void InitLeftTreeViewData()
- {
- this.collectionLeft = this.appWorkspace.ToolBar.MainMenu.Items;
- this.animeLeft = new TreeNode(PdnResources.GetString("Menu.menu.Text"));
- this.animeLeft.Checked = true;
- this.RecursiveDataLeft(collectionLeft, animeLeft);
- this.animeLeft.Expand();
- this.treeView1.Nodes.Add(animeLeft);
- }
- /// <summary>
- /// 左侧递归进行数据组织
- /// </summary>
- private void RecursiveDataLeft(ToolStripItemCollection collection, TreeNode anime)
- {
- for (int i = 0; i < collection.Count; i++)
- {
- TreeNode node = new TreeNode(collection[i].Text);
- if (collection[i] is PdnMenuItem)
- {
- if (!(collection[i] is PdnMenuItem)) continue;
- PdnMenuItem item = (PdnMenuItem)collection[i];
- if (!item.CanShowInSenseShield)
- continue;
- node.Tag = item.MenuId;
- if (menuIdArr != null && menuIdArr.Count() > 0)
- {
- if (Array.IndexOf(menuIdArr, item.MenuId.ToString()) != -1)
- {
- node.Checked = true;
- }
- else
- {
- node.Checked = false;
- //兄弟节点只要有一个没选,其父节点也不选//#20756
- TreeNode nodeParent = anime;// node.Parent;
- while (nodeParent != null)
- {
- nodeParent.Checked = false;
- nodeParent = nodeParent.Parent;
- }
- }
- }
- //if (Startup.instance.moduleConfigModel.items.Find(a => a.ParentId == item.MenuId) != null
- // || Startup.instance.moduleConfigModel.items.Find(a => a.ChildIds.Split(',').Contains(item.MenuId.ToString())) != null)
- //{
- // node.Checked = true;
- //}
- anime.Nodes.Add(node);
- if (collection[i].Name.Equals("OpenRecent") || collection[i].Name.Equals("CameraSelection"))
- continue;
- RecursiveDataLeft(((PdnMenuItem)collection[i]).DropDownItems, node);
- }
- }
- }
- /// <summary>
- /// 确定
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button1_Click(object sender, EventArgs e)
- {
- TreeNodeCollection nodes = this.animeLeft.Nodes;
- string str = "";
- foreach (TreeNode node in nodes)
- {
- if (node.Checked)
- {
- if (str == "")
- {
- str += node.Tag.ToString();
- }
- else
- {
- str += "," + node.Tag.ToString();
- }
- }
- if (node.Nodes.Count > 0)
- {
- bool nodeHasChildChecked;
- str = RecursionMenuId(node, str, out nodeHasChildChecked);
- if (!node.Checked && nodeHasChildChecked)
- {
- if (str == "")
- {
- str += node.Tag.ToString();
- }
- else
- {
- str += "," + node.Tag.ToString();
- }
- }
- }
- }
- //FileStream fs = new FileStream(txtPath, FileMode.Create);
- //StreamWriter sw = new StreamWriter(fs);
- StreamWriter sw = new StreamWriter(txtPath, false);
- sw.Write(str);
- sw.Close();
- //fs.Close();
- sw.Dispose();
- //fs.Dispose();
- this.Close();
- }
- /// <summary>
- /// 递归获取所有选中的菜单id
- /// </summary>
- /// <param name="node"></param>
- /// <param name="str"></param>
- /// <returns></returns>
- private string RecursionMenuId(TreeNode node, string str, out bool hasChildChecked)
- {
- hasChildChecked = false;
- if (node.Nodes.Count > 0)
- {
- foreach (TreeNode tn in node.Nodes)
- {
- if (tn.Checked)
- {
- if (str == "")
- {
- str += tn.Tag.ToString();
- }
- else
- {
- str += "," + tn.Tag.ToString();
- }
- hasChildChecked = true;
- }
- if (tn.Nodes.Count > 0)
- {
- bool tnHasChildChecked;
- str = RecursionMenuId(tn, str, out tnHasChildChecked);
- if (!tn.Checked && tnHasChildChecked)
- {
- if (str == "")
- {
- str += tn.Tag.ToString();
- }
- else
- {
- str += "," + tn.Tag.ToString();
- }
- hasChildChecked = true;
- }
- }
- }
- }
- return str;
- }
- /// <summary>
- /// 取消
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button2_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- //递归父节点跟随其全选或全不选
- private void ChangeParent(TreeNode node)
- {
- if (node.Parent != null)
- {
- //兄弟节点被选中的个数
- int brotherNodeCheckedCount = 0;
- //遍历该节点的兄弟节点
- foreach (TreeNode tn in node.Parent.Nodes)
- {
- if (tn.Checked == true)
- brotherNodeCheckedCount++;
- }
- //兄弟节点全没选,其父节点也不选
- if (brotherNodeCheckedCount == 0)
- {
- node.Parent.Checked = false;
- ChangeParent(node.Parent);
- }
- //兄弟节点只要有一个被选,其父节点也被选
- if (brotherNodeCheckedCount >= 1)
- {
- node.Parent.Checked = true;
- ChangeParent(node.Parent);
- }
- }
- }
- /*private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
- {
- if (e.Node != null)
- {
- this.treeView1.AfterCheck -= treeView1_AfterCheck;
- ChangeChild(e.Node, e.Node.Checked);//影响子节点
- ChangeParent(e.Node);//影响父节点
- this.treeView1.AfterCheck += treeView1_AfterCheck;
- }
- }*/
- private bool nextCheck(TreeNode n) //判断同级的节点是否全选
- {
- foreach (TreeNode tn in n.Parent.Nodes)
- {
- if (tn.Checked == false) return false;
- }
- return true;
- }
- private bool nextNotCheck(TreeNode n) //判断同级的节点是否全不选
- {
- if (n.Checked == true)
- {
- return false;
- }
- if (n.NextNode == null)
- {
- return true;
- }
- return this.nextNotCheck(n.NextNode);
- }
- private void cycleChild(TreeNode tn, bool check) //遍历节点下的子节点
- {
- if (tn.Nodes.Count != 0)
- {
- foreach (TreeNode child in tn.Nodes)
- {
- child.Checked = check;
- if (child.Nodes.Count != 0)
- {
- cycleChild(child, check);
- }
- }
- }
- else
- return;
- }
- private void cycleParent(TreeNode tn, bool check) //遍历节点上的父节点
- {
- if (tn.Parent != null)
- {
- if (nextCheck(tn))
- {
- tn.Parent.Checked = true;
- }
- else
- {
- tn.Parent.Checked = false;
- }
- cycleParent(tn.Parent, check);
- }
- return;
- }
- // afterCheck
- private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) //当选中或取消选中树节点上的复选框时发生
- {
- //要求父节点被勾选,则子节点全部被勾选;父节点不被勾选,则子节点不全不被勾选
- if (e.Node.Checked == true)
- {
- if (e.Action != TreeViewAction.Unknown)
- {
- cycleChild(e.Node, true);
- }
- if (e.Node.Parent != null)
- {
- if (nextCheck(e.Node))
- {
- cycleParent(e.Node, true);
- }
- else
- {
- cycleParent(e.Node, false);
- }
- }
- }
- if (e.Node.Checked == false)
- {
- if (e.Action != TreeViewAction.Unknown)
- {
- cycleChild(e.Node, false); //中间节点不选中则子节点全部不选中
- cycleParent(e.Node, false); //父节点不选中
- }
- // bCheck = false;
- }
- return;
- }
- int nodeClicks = 0;
- TreeViewHitTestInfo hitInfo = null;
- private void treeView1_MouseDown(object sender, MouseEventArgs e)
- {
- hitInfo = treeView1.HitTest(e.Location);
- nodeClicks = e.Clicks;
- }
- private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
- {
- if (nodeClicks > 1 && hitInfo.Location == TreeViewHitTestLocations.Label)
- {
- e.Cancel = true;
- }
- }
- #region 控件
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.button2 = new System.Windows.Forms.Button();
- this.button1 = new System.Windows.Forms.Button();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.treeView1 = new TreeViewEnhanced();
- this.label1 = new System.Windows.Forms.Label();
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- this.SuspendLayout();
- //
- // groupBox1
- //
- this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.groupBox1.Controls.Add(this.button2);
- this.groupBox1.Controls.Add(this.button1);
- this.groupBox1.Location = new System.Drawing.Point(13, 13);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(301, 58);
- this.groupBox1.TabIndex = 1;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "操作";
- //
- // button2
- //
- this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
- this.button2.Location = new System.Drawing.Point(220, 20);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(75, 23);
- this.button2.TabIndex = 1;
- this.button2.Text = "取消";
- this.button2.UseVisualStyleBackColor = true;
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // button1
- //
- this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
- this.button1.Location = new System.Drawing.Point(138, 20);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(75, 23);
- this.button1.TabIndex = 0;
- this.button1.Text = "确定";
- this.button1.UseVisualStyleBackColor = true;
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.treeView1);
- this.groupBox2.Location = new System.Drawing.Point(13, 78);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(301, 467);
- this.groupBox2.TabIndex = 2;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "可用模块";
- //
- // treeView1
- //
- this.treeView1.Location = new System.Drawing.Point(7, 21);
- this.treeView1.Name = "treeView1";
- this.treeView1.Size = new System.Drawing.Size(288, 440);
- this.treeView1.TabIndex = 0;
- this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);
- this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
- this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(13, 551);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(125, 12);
- this.label1.TabIndex = 3;
- this.label1.Text = "更改模块后请重启软件";
- //
- // ModuleManageDialog
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(326, 577);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.groupBox1);
- this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
- this.MaximizeBox = false;
- this.Name = "ModuleManageDialog";
- this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
- this.Text = "模块管理";
- this.Controls.SetChildIndex(this.groupBox1, 0);
- this.Controls.SetChildIndex(this.groupBox2, 0);
- this.Controls.SetChildIndex(this.label1, 0);
- this.groupBox1.ResumeLayout(false);
- this.groupBox2.ResumeLayout(false);
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.Button button2;
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.GroupBox groupBox2;
- private TreeViewEnhanced treeView1;
- private System.Windows.Forms.Label label1;
- #endregion
- }
- }
|