using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PaintDotNet.Base.SettingModel;
using PaintDotNet.Base.CommTool;
using System.IO;
namespace PaintDotNet.Instrument.CustomInterface
{
internal class FastTools : UserControl
{
///
/// 右侧树形菜单
///
private ToolStripItemCollection collectionRight;
///
/// 工作空间
///
private AppWorkspace appWorkspace;
///
/// 快捷菜单的数据
///
private ShortcutbarModel shortcutbar_Model = Startup.instance.shortcutbarModel;
private string txtPath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\ModuleConfig.txt";
private string[] menuIdArr;
#region 控件
private GroupBox groupBoxLeft;
private ListView listViewLeft;
private ListViewItem listViewLeftDown = null;
private ImageList imageListLeft;
private GroupBox groupBoxRight;
private TreeView treeViewRight;
private Button buttonAdd;
private Button buttonRemove;
private Button buttonUp;
private Button buttonDown;
private IContainer components;
#endregion
public FastTools(AppWorkspace appWorkspace)
{
this.appWorkspace = appWorkspace;
InitializeComponent();
InitializeLanguageText();
InitVisibleMenuId();
InitializeLeftTreeData();
InitializeRightTreeData();
InitializeTreeEvent();
}
///
/// 获取txt文件中已保存的菜单可用id
///
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 };
}
}
}
}
///
/// 初始化treeview的事件
///
private void InitializeTreeEvent()
{
this.treeViewRight.HideSelection = false;
this.treeViewRight.DrawMode = TreeViewDrawMode.OwnerDrawText;
this.treeViewRight.DrawNode += new DrawTreeNodeEventHandler(this.treeViewRight_DrawNode);
this.treeViewRight.Invalidated += new InvalidateEventHandler(this.treeViewRight_Invalidated);
this.treeViewRight.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeViewRight_MouseDown);
}
///
/// 右侧功能列表鼠标按下事件
///
///
///
private void treeViewRight_MouseDown(object sender, MouseEventArgs e)
{
if ((sender as TreeView) != null && e.Clicks == 2 && this.buttonAdd.Enabled)
{//自定义软件界面,双击可添加
if (this.treeViewRight.GetNodeAt(e.X, e.Y) == null)
return;
}
else
return;
//自定义软件界面,双击可添加
TreeNode downNode = this.treeViewRight.GetNodeAt(e.X, e.Y);
//if ((sender as TreeView) != null)
//{
// this.treeViewRight.SelectedNode = this.treeViewRight.GetNodeAt(e.X, e.Y);
// this.treeViewRight.Refresh();
//}
if (/*this.treeViewRight.SelectedNode != null && */downNode.Nodes.Count == 0)
{
ToolStripItem[] items = this.appWorkspace.ToolBar.MainMenu.Items.Find(downNode.Name, true);
if (items != null && items.Length > 0)
{
if (((PdnMenuItem)(items[0])).Image != null)
this.listViewLeft.SmallImageList.Images.Add(downNode.Name, ((PdnMenuItem)(items[0])).Image);
else
this.listViewLeft.SmallImageList.Images.Add(downNode.Name, new Bitmap(16, 16));
}
else
{
this.listViewLeft.SmallImageList.Images.Add(downNode.Name, new Bitmap(16, 16));
}
ListViewItem item = new ListViewItem();
item.Text = downNode.Text;
item.Tag = downNode.Tag;
item.Name = downNode.Name;
item.ImageIndex = this.listViewLeft.SmallImageList.Images.Count - 1;
this.listViewLeft.Items.Add(item);
}
}
///
/// treeView1的重绘事件
///
///
///
private void treeViewRight_Invalidated(object sender, InvalidateEventArgs e)
{
}
///
/// 自定义绘制
/// 参考https://www.cnblogs.com/JiYF/p/6693503.html
///
///
///
private void treeViewRight_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
/**用默认颜色
e.DrawDefault = true;
return;
**/
//以下是自定义颜色
if ((e.State & TreeNodeStates.Selected) != 0)
{
//演示为蓝(绿)底白字
e.Graphics.FillRectangle(SystemBrushes.Highlight/*Brushes.Green*/, e.Node.Bounds);
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, Rectangle.Inflate(e.Bounds, 2, 0));
}
else
{
e.DrawDefault = true;
}
if ((e.State & TreeNodeStates.Focused) != 0)
{
using (Pen focusPen = new Pen(Color.Black))
{
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle focusBounds = e.Node.Bounds;
focusBounds.Size = new Size(focusBounds.Width - 1,
focusBounds.Height - 1);
e.Graphics.DrawRectangle(focusPen, focusBounds);
}
}
}
///
/// 初始化左侧listview菜单
///
private void InitializeLeftTreeData()
{
this.listViewLeft.View = View.Details;
ColumnHeader header = new ColumnHeader();
header.Text = PdnResources.GetString("Menu.tool.Generateshortcut.functionlist.text");
header.Width = 240;
this.listViewLeft.Columns.Add(header);
this.listViewLeft.Items.Clear();
if (shortcutbar_Model!=null) {
if (shortcutbar_Model.Menus != null)
{
for (int i=0; i< shortcutbar_Model.Menus.Count; i++)
{
ToolStripItem[] items = this.appWorkspace.ToolBar.MainMenu.Items.Find(shortcutbar_Model.Menus[i].Description, true);
ListViewItem item = new ListViewItem();
if (items != null && items.Length > 0)
{
if (((PdnMenuItem)(items[0])).Image != null)
this.listViewLeft.SmallImageList.Images.Add(shortcutbar_Model.Menus[i].Description, ((PdnMenuItem)(items[0])).Image);
else
this.listViewLeft.SmallImageList.Images.Add(shortcutbar_Model.Menus[i].Description, new Bitmap(16, 16));
item.Text = ((PdnMenuItem)(items[0])).Text;
}
else
{
this.listViewLeft.SmallImageList.Images.Add(shortcutbar_Model.Menus[i].Description, new Bitmap(16, 16));
item.Text = shortcutbar_Model.Menus[i].Name;// getShowName(Startup.instance.configModel.Language);
}
item.Tag = shortcutbar_Model.Menus[i].Id;
item.Name = shortcutbar_Model.Menus[i].Description;
item.ImageIndex = i;
this.listViewLeft.Items.Add(item);
}
}
}
}
///
/// 初始化右侧树形菜单数据
///
private void InitializeRightTreeData()
{
this.collectionRight = this.appWorkspace.ToolBar.MainMenu.Items;
TreeNode anime = new TreeNode(PdnResources.GetString("Menu.menu.Text"));
this.RecursiveData(collectionRight, anime);
anime.Expand();
this.treeViewRight.Nodes.Add(anime);
}
///
/// 递归进行数据组织
///
private void RecursiveData(ToolStripItemCollection collection, TreeNode anime)
{
for (int i = 0; i < collection.Count; i++)
{
TreeNode node = new TreeNode(/*collection[i].Text*/);
if (collection[i] is PdnMenuItem)
{
PdnMenuItem item = (PdnMenuItem)collection[i];
if (!item.CanShowInSenseShield)
continue;
node.Tag = item.MenuId;
if (menuIdArr != null && menuIdArr.Length > 0)
{
if (Array.IndexOf(menuIdArr, item.MenuId.ToString()) != -1)
{
node.Name = collection[i].Name;
node.Text = collection[i].Text;
node.Tag = ((PdnMenuItem)collection[i]).MenuId;
node.Checked = true;
}
else
{
node.Checked = false;
}
}
if (node.Checked)
{
anime.Nodes.Add(node);
}
if (collection[i].Name.Equals("OpenRecent") || collection[i].Name.Equals("CameraSelection"))
continue;
RecursiveData(((PdnMenuItem)collection[i]).DropDownItems, node);
}
}
}
#region 组件设计器生成的代码
private void InitializeLanguageText()
{
this.buttonDown.Text = PdnResources.GetString("Menu.LabelAction.MoveDownAction.Text");
this.buttonUp.Text = PdnResources.GetString("Menu.LabelAction.MoveUpAction.Text");
this.buttonRemove.Text = PdnResources.GetString("Menu.Moveout.text")+" >";
this.buttonAdd.Text = "< "+ PdnResources.GetString("Menu.Addto.text");
this.groupBoxRight.Text = PdnResources.GetString("Menu.Availablefunctions.text");
this.groupBoxLeft.Text = PdnResources.GetString("Menu.tool.Generateshortcut.Shortcutbarcontent.text");
}
///
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.groupBoxLeft = new System.Windows.Forms.GroupBox();
this.listViewLeft = new System.Windows.Forms.ListView();
this.imageListLeft = new System.Windows.Forms.ImageList(this.components);
this.groupBoxRight = new System.Windows.Forms.GroupBox();
this.treeViewRight = new System.Windows.Forms.TreeView();
this.buttonAdd = new System.Windows.Forms.Button();
this.buttonRemove = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.groupBoxLeft.SuspendLayout();
this.groupBoxRight.SuspendLayout();
this.SuspendLayout();
//
// groupBoxLeft
//
this.groupBoxLeft.Controls.Add(this.listViewLeft);
this.groupBoxLeft.Location = new System.Drawing.Point(0, 0);
this.groupBoxLeft.Name = "groupBoxLeft";
this.groupBoxLeft.Size = new System.Drawing.Size(256, 398);
this.groupBoxLeft.TabIndex = 0;
this.groupBoxLeft.TabStop = false;
this.groupBoxLeft.Text = "快捷栏内容";
//
// listViewLeft
//
this.listViewLeft.HideSelection = false;
this.listViewLeft.Location = new System.Drawing.Point(7, 20);
this.listViewLeft.MultiSelect = false;
this.listViewLeft.Name = "listViewLeft";
this.listViewLeft.Size = new System.Drawing.Size(243, 372);
this.listViewLeft.SmallImageList = this.imageListLeft;
this.listViewLeft.TabIndex = 0;
this.listViewLeft.UseCompatibleStateImageBehavior = false;
this.listViewLeft.MouseDown += ListViewLeft_MouseDown;
this.listViewLeft.DrawItem += new System.Windows.Forms.DrawListViewItemEventHandler(this.listViewLeft_DrawItem);
this.listViewLeft.Invalidated += new InvalidateEventHandler(this.treeViewLeft_InvalidateEvent);
//
// imageListLeft
//
this.imageListLeft.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imageListLeft.ImageSize = new System.Drawing.Size(16, 16);
this.imageListLeft.TransparentColor = System.Drawing.Color.Transparent;
//
// groupBoxRight
//
this.groupBoxRight.Controls.Add(this.treeViewRight);
this.groupBoxRight.Location = new System.Drawing.Point(367, 0);
this.groupBoxRight.Name = "groupBoxRight";
this.groupBoxRight.Size = new System.Drawing.Size(256, 398);
this.groupBoxRight.TabIndex = 1;
this.groupBoxRight.TabStop = false;
this.groupBoxRight.Text = "可用功能";
//
// treeViewRight
//
this.treeViewRight.Location = new System.Drawing.Point(7, 20);
this.treeViewRight.Name = "treeViewRight";
this.treeViewRight.Size = new System.Drawing.Size(243, 372);
this.treeViewRight.TabIndex = 0;
//
// buttonAdd
//
this.buttonAdd.Location = new System.Drawing.Point(277, 20);
this.buttonAdd.Name = "buttonAdd";
this.buttonAdd.Size = new System.Drawing.Size(75, 23);
this.buttonAdd.TabIndex = 3;
this.buttonAdd.Text = "< 添加";
this.buttonAdd.UseVisualStyleBackColor = true;
this.buttonAdd.Click += new System.EventHandler(this.buttonAddClick);
//
// buttonRemove
//
this.buttonRemove.Location = new System.Drawing.Point(277, 49);
this.buttonRemove.Name = "buttonRemove";
this.buttonRemove.Size = new System.Drawing.Size(75, 23);
this.buttonRemove.TabIndex = 4;
this.buttonRemove.Text = "移出 >";
this.buttonRemove.UseVisualStyleBackColor = true;
this.buttonRemove.Click += new System.EventHandler(this.buttonRemoveClick);
//
// buttonUp
//
this.buttonUp.Location = new System.Drawing.Point(277, 78);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(75, 23);
this.buttonUp.TabIndex = 5;
this.buttonUp.Text = "向上移动";
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.buttonUpClick);
//
// buttonDown
//
this.buttonDown.Location = new System.Drawing.Point(277, 107);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(75, 23);
this.buttonDown.TabIndex = 6;
this.buttonDown.Text = "向下移动";
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.buttonDownClick);
//
// FastTools
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonRemove);
this.Controls.Add(this.buttonAdd);
this.Controls.Add(this.groupBoxRight);
this.Controls.Add(this.groupBoxLeft);
this.Name = "FastTools";
this.Size = new System.Drawing.Size(623, 398);
this.groupBoxLeft.ResumeLayout(false);
this.groupBoxRight.ResumeLayout(false);
this.ResumeLayout(false);
}
private void ListViewLeft_MouseDown(object sender, MouseEventArgs e)
{
if ((sender as ListView) != null)
{
listViewLeftDown = this.listViewLeft.GetItemAt(e.X, e.Y);
this.listViewLeft.Refresh();
}
//this.listViewLeft.Refresh();
}
#endregion
///
/// 添加
///
///
///
private void buttonAddClick(object sender, EventArgs e)
{
if (this.treeViewRight.SelectedNode!=null && this.treeViewRight.SelectedNode.Nodes.Count==0)
{
ToolStripItem[] items = this.appWorkspace.ToolBar.MainMenu.Items.Find(this.treeViewRight.SelectedNode.Name, true);
if (items != null && items.Length > 0)
{
if (((PdnMenuItem)(items[0])).Image != null)
this.listViewLeft.SmallImageList.Images.Add(this.treeViewRight.SelectedNode.Name, ((PdnMenuItem)(items[0])).Image);
else
this.listViewLeft.SmallImageList.Images.Add(this.treeViewRight.SelectedNode.Name, new Bitmap(16, 16));
}
else
{
this.listViewLeft.SmallImageList.Images.Add(this.treeViewRight.SelectedNode.Name, new Bitmap(16, 16));
}
ListViewItem item = new ListViewItem();
item.Text = this.treeViewRight.SelectedNode.Text;
item.Tag = this.treeViewRight.SelectedNode.Tag;
item.Name = this.treeViewRight.SelectedNode.Name;
item.ImageIndex = this.listViewLeft.SmallImageList.Images.Count-1;
this.listViewLeft.Items.Add(item);
}
}
///
/// 移出
///
///
///
private void buttonRemoveClick(object sender, EventArgs e)
{
if (this.listViewLeft.SelectedItems.Count > 0)
{
foreach(ListViewItem item in this.listViewLeft.SelectedItems)
this.listViewLeft.Items.Remove(item);
}
}
///
/// 向上移动
///
///
///
private void buttonUpClick(object sender, EventArgs e)
{
if (this.listViewLeft.SelectedItems.Count == 1)
{
int index = this.listViewLeft.Items.IndexOf(this.listViewLeft.SelectedItems[0]);
if (index > 0)
{
ListViewItem prenode = (ListViewItem)this.listViewLeft.Items[index-1].Clone();
prenode.Name = this.listViewLeft.Items[index - 1].Name;
this.listViewLeft.Items.Insert(index + 1, prenode);
this.listViewLeft.Items.Remove(this.listViewLeft.Items[index - 1]);
this.listViewLeft.Refresh();
}
}
}
///
/// 向下移动
///
///
///
private void buttonDownClick(object sender, EventArgs e)
{
if (this.listViewLeft.SelectedItems.Count == 1)
{
int index = this.listViewLeft.Items.IndexOf(this.listViewLeft.SelectedItems[0]);
if (index < this.listViewLeft.Items.Count-1)
{
ListViewItem nextnode = (ListViewItem)this.listViewLeft.Items[index + 1].Clone();
nextnode.Name = this.listViewLeft.Items[index + 1].Name;
this.listViewLeft.Items.Insert(index, nextnode);
this.listViewLeft.Items.Remove(this.listViewLeft.Items[index + 2]);
this.listViewLeft.Refresh();
}
}
}
private void listViewLeft_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
return;
}
///
/// treeViewLeft的重绘事件
///
///
///
private void treeViewLeft_InvalidateEvent(object sender, InvalidateEventArgs e)
{
//1009###17142
if (listViewLeftDown == null)
{
//this.buttonUp.Enabled = false;
//this.buttonDown.Enabled = false;
if (this.listViewLeft.SelectedItems.Count == 1)
{
int index = this.listViewLeft.Items.IndexOf(this.listViewLeft.SelectedItems[0]);
if (index > 0)
this.buttonUp.Enabled = true;
else
this.buttonUp.Enabled = false;
}
else
this.buttonUp.Enabled = false;
if (this.listViewLeft.SelectedItems.Count == 1)
{
int index = this.listViewLeft.Items.IndexOf(this.listViewLeft.SelectedItems[0]);
if (index < this.listViewLeft.Items.Count - 1)
this.buttonDown.Enabled = true;
else
this.buttonDown.Enabled = false;
}
else
this.buttonDown.Enabled = false;
}
else
{
int index = this.listViewLeft.Items.IndexOf(listViewLeftDown);
if (index > 0)
this.buttonUp.Enabled = true;
else
this.buttonUp.Enabled = false;
index = this.listViewLeft.Items.IndexOf(listViewLeftDown);
if (index < this.listViewLeft.Items.Count - 1)
this.buttonDown.Enabled = true;
else
this.buttonDown.Enabled = false;
listViewLeftDown = null;
}
}
///
/// 保存数据到xml
///
public void SaveToolbar()
{
shortcutbar_Model.Menus.Clear();
for (int i = 0; i < this.listViewLeft.Items.Count; i++)
{
ShortcutbarModel.Item flow = new ShortcutbarModel.Item();
flow.Id = (int)(this.listViewLeft.Items[i].Tag);
flow.Name = this.listViewLeft.Items[i].Text;
//################
flow.Description = this.listViewLeft.Items[i].Name;
shortcutbar_Model.Menus.Add(flow);
}
string userInfoXml = XmlSerializeHelper.XmlSerialize(shortcutbar_Model);
string filePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\Shortcutbar.xml";
if (FileOperationHelper.WriteStringToFile(userInfoXml, filePath, FileMode.Create))
{
//更新全局数据
Startup.instance.shortcutbarModel = shortcutbar_Model;
//this.appWorkspace.mainToolBarForm.setCilentSize();
//this.appWorkspace.mainToolBarForm.ToolsControl.Refresh();
this.appWorkspace.toolsPanel.RefreshTools();
}
else
{
MessageBox.Show(PdnResources.GetString("Menu.Shortcuttoolbarsavefailed.text"));
}
}
///
/// 导出配置
///
///
public void ExportToolbarXml()
{
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
saveFileDialog.Filter = "Xml(*.xml)|*.xml";
saveFileDialog.DefaultExt = "xml";
saveFileDialog.FilterIndex = 1;
saveFileDialog.CheckFileExists = false;
saveFileDialog.AddExtension = true;
saveFileDialog.FileName = "Shortcutbar";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = saveFileDialog.FileName;
string toolbarXml = XmlSerializeHelper.XmlSerialize(Startup.instance.shortcutbarModel);
if (FileOperationHelper.WriteStringToFile(toolbarXml, filePath, FileMode.Create))
MessageBox.Show(PdnResources.GetString("Menu.lbarconfigurationfilewassavedsuccessfu.text"));
else
MessageBox.Show(PdnResources.GetString("Menu.Theshortcuttoolbarsave.text"));
}
}
}
///
/// 导入配置
///
///
public void ImportToolbarXml()
{
using (var openFileDialog = new OpenFileDialog { Filter = "*.xml|*.xml" })
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
using (Stream shortbarStream = System.IO.File.OpenRead(openFileDialog.FileName))
{
StreamReader sr = new StreamReader(shortbarStream);
string xmlNotes = sr.ReadToEnd();
ShortcutbarModel toolbarXml = XmlSerializeHelper.DESerializer(xmlNotes);
if (toolbarXml.Menus.Count > 0)
{
try
{
this.listViewLeft.Items.Clear();
this.listViewLeft.SmallImageList = new ImageList();
this.listViewLeft.BeginUpdate();
for (int i = 0; i < toolbarXml.Menus.Count; i++)
{
ToolStripItem[] items = this.appWorkspace.ToolBar.MainMenu.Items.Find(toolbarXml.Menus[i].Description, true);
ListViewItem listItem = new ListViewItem();
if (items != null && items.Length > 0)
{
if (((PdnMenuItem)(items[0])).Image != null)
this.listViewLeft.SmallImageList.Images.Add(((PdnMenuItem)(items[0])).Text, ((PdnMenuItem)(items[0])).Image);
else
this.listViewLeft.SmallImageList.Images.Add(((PdnMenuItem)(items[0])).Text, new Bitmap(16, 16));
listItem.Text = ((PdnMenuItem)(items[0])).Text;
}
else
{
this.listViewLeft.SmallImageList.Images.Add(toolbarXml.Menus[i].Name/*getShowName(Startup.instance.configModel.Language)*/, new Bitmap(16, 16));
listItem.Text = toolbarXml.Menus[i].Name;// getShowName(Startup.instance.configModel.Language);
}
listItem.Tag = toolbarXml.Menus[i].Id;
listItem.Name = toolbarXml.Menus[i].Description;
listItem.ImageIndex = i;
this.listViewLeft.Items.Add(listItem);
}
this.listViewLeft.EndUpdate();
MessageBox.Show(PdnResources.GetString("Menu.Shortcuttoolbasuccessfully.text"));
}
catch(Exception)
{
this.listViewLeft.Items.Clear();
this.listViewLeft.SmallImageList = new ImageList();
MessageBox.Show(PdnResources.GetString("Menu.Theshortcutconfigurationfileimportailed.text"));
}
}
else
{
MessageBox.Show(PdnResources.GetString("Menu.onfiguratiofilesavedconfiguratio.text"));
}
}
}
}
}
}
}