using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Xml; using System.Linq; using System.ComponentModel; using System.ComponentModel.Design; using System.IO; using ICSharpCode.Core; using Mesnac.Gui.Workbench; using Mesnac.Gui.Common; using Mesnac.Gui.Edit.Global; using Mesnac.Gui.Edit.Common; using Mesnac.Gui.Edit.Dialog; using Mesnac.Gui.Edit.Pad; using Mesnac.Gui.Edit.ViewContent; using Mesnac.Core.Service; using Mesnac.Basic; using Host; using Mesnac.PlugIn.View; namespace Mesnac.Gui.Edit.Common { /// /// 窗体设计器辅助类 /// public class FormDesignerHelper { #region 窗体设计器辅助方法 #region 获取设计器中所有组件 /// /// 获取设计器中所有组件 /// /// public static ComponentCollection GetAllComponents() { //获取设计器中所有组件 ComponentCollection allComponents = null; if (MesnacServiceManager.Instance.Host != null) { allComponents = MesnacServiceManager.Instance.Host.Container.Components; } return allComponents; } #endregion #region 获取当前设计器中选中的控件 /// /// 获取设计器中选中的控件 /// /// public static object[] GetSelectComponents() { //获取设计器中选中的控件 ICollection selectedComponents = null; object[] selectedcomp = null; System.ComponentModel.Design.ISelectionService ss = null; //组件选择服务 if (MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface != null) { ss = MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface.GetService(typeof(System.ComponentModel.Design.ISelectionService)) as System.ComponentModel.Design.ISelectionService; selectedComponents = ss.GetSelectedComponents(); selectedcomp = new object[selectedComponents.Count]; selectedComponents.CopyTo(selectedcomp, 0); } return selectedcomp; } #endregion #endregion #region 初始化宿主设计器服务 /// /// 初始化宿主设计器服务 /// public static void Init() { #region 服务、宿主管理器、界面组件 ToolBoxWindow toolBoxWindow = DisplayUtil.PadInstance; PropertyWindow propertyWindow = DisplayUtil.PadInstance; ActionWindow actionWindow = DisplayUtil.PadInstance; EventWindow eventWindow = DisplayUtil.PadInstance; ProjectWindow projectWindow = DisplayUtil.PadInstance; MesnacServiceManager.Instance.LoggingService.Info("初始化服务、宿主管理器、界面组件..."); //向宿主管理器中添加服务、工具箱和属性服务 MesnacServiceManager.Instance.LoggingService.Info("向宿主管理器中添加服务、工具箱和属性服务..."); MesnacServiceManager.Instance.HostSurfaceManager.AddService(typeof(System.Drawing.Design.IToolboxService), MesnacServiceManager.Instance.CustomToolboxService); MesnacServiceManager.Instance.HostSurfaceManager.AddService(typeof(System.Windows.Forms.PropertyGrid), propertyWindow.PropertyGrid); MesnacServiceManager.Instance.HostSurfaceManager.AddService(typeof(ActionWindow), actionWindow); MesnacServiceManager.Instance.HostSurfaceManager.AddService(typeof(EventWindow), eventWindow); MesnacServiceManager.Instance.HostSurfaceManager.AddService(typeof(System.Windows.Forms.TreeView), projectWindow.Tree); #endregion #region 以下代码实现把控件从工具箱中放置到宿主设计器中的初始化事件处理 MesnacServiceManager.Instance.LoggingService.Info("初始化事件处理..."); // 选中不同的工具条项目 toolBoxWindow.ToolBox.SelectedItemChanged += delegate(object sender, Mesnac.UI.ToolBox.ToolBoxItem newItem) { System.Drawing.Design.ToolboxItem toolboxItem = newItem.Tag as System.Drawing.Design.ToolboxItem; bool IsSptring = false; if (toolboxItem != null && toolboxItem.DisplayName.ToLower() == "mctoolstripbutton") { System.ComponentModel.Design.IDesignerHost host = (System.ComponentModel.Design.IDesignerHost)MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface.GetService(typeof(System.ComponentModel.Design.IDesignerHost)); foreach (Component cmp in host.Container.Components) { if (cmp is Mesnac.Controls.Default.MCtoolStrip) { IsSptring = true; } } if (!IsSptring) { return; } } MesnacServiceManager.Instance.CustomToolboxService.SetSelectedToolboxItem(newItem.Tag as System.Drawing.Design.ToolboxItem); }; // 双击工具栏项目时增加到设计器中 toolBoxWindow.ToolBox.ItemDoubleClicked += delegate(object sender, Mesnac.UI.ToolBox.ToolBoxItem newItem) { System.Drawing.Design.ToolboxItem toolboxItem = newItem.Tag as System.Drawing.Design.ToolboxItem; if (null != toolboxItem && null != MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface) { MesnacServiceManager.Instance.Host = (System.ComponentModel.Design.IDesignerHost)MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface.GetService(typeof(System.ComponentModel.Design.IDesignerHost)); if (MesnacServiceManager.Instance.Host != null) { System.Drawing.Design.IToolboxUser toolboxUser = MesnacServiceManager.Instance.Host.GetDesigner(MesnacServiceManager.Instance.Host.RootComponent as IComponent) as System.Drawing.Design.IToolboxUser; //------------ bool IsSptring = false; Mesnac.Controls.Default.MCToolStripButton mc1 = new Controls.Default.MCToolStripButton(); Mesnac.Controls.Default.MCtoolStrip mc = new Controls.Default.MCtoolStrip(); if (toolboxItem.DisplayName.ToLower() == "mctoolstripbutton") { System.ComponentModel.Design.IDesignerHost host = (System.ComponentModel.Design.IDesignerHost)MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface.GetService(typeof(System.ComponentModel.Design.IDesignerHost)); foreach (Component cmp in host.Container.Components) { if (cmp is Mesnac.Controls.Default.MCtoolStrip) { mc = cmp as Mesnac.Controls.Default.MCtoolStrip; IsSptring = true; } } if (!IsSptring) { MessageBox.Show("请加入ToolBar控件。"); return; } } if (null != toolboxUser) toolboxUser.ToolPicked(toolboxItem); if (IsSptring && toolboxItem.DisplayName.ToLower() == "mctoolstripbutton") { System.ComponentModel.Design.IDesignerHost host = (System.ComponentModel.Design.IDesignerHost)MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface.GetService(typeof(System.ComponentModel.Design.IDesignerHost)); foreach (Component cmp in host.Container.Components) { if (cmp is Mesnac.Controls.Default.MCToolStripButton) { mc1 = cmp as Mesnac.Controls.Default.MCToolStripButton; mc.Items.Add(mc1); } } } } } SetFocusContent(); //聚焦设置 }; // 拖动工具栏项目的支持代码 toolBoxWindow.ToolBox.ItemDragStart += delegate(object sender, Mesnac.UI.ToolBox.ToolBoxItem newItem) { System.Drawing.Design.ToolboxItem toolboxItem = newItem.Tag as System.Drawing.Design.ToolboxItem; //------------ bool IsSptring = false; Mesnac.Controls.Default.MCToolStripButton mc1 = new Controls.Default.MCToolStripButton(); Mesnac.Controls.Default.MCtoolStrip mc = new Controls.Default.MCtoolStrip(); if (toolboxItem != null && toolboxItem.DisplayName.ToLower() == "mctoolstripbutton") { System.ComponentModel.Design.IDesignerHost host = (System.ComponentModel.Design.IDesignerHost)MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface.GetService(typeof(System.ComponentModel.Design.IDesignerHost)); foreach (Component cmp in host.Container.Components) { if (cmp is Mesnac.Controls.Default.MCtoolStrip) { mc = cmp as Mesnac.Controls.Default.MCtoolStrip; IsSptring = true; } } if (!IsSptring) { toolboxItem = null; return; } } if (null != toolboxItem) { DataObject dataObject = ((System.Drawing.Design.IToolboxService)MesnacServiceManager.Instance.CustomToolboxService).SerializeToolboxItem(toolboxItem) as DataObject; toolBoxWindow.ToolBox.DoDragDrop(dataObject, DragDropEffects.Copy); } if (IsSptring && toolboxItem.DisplayName.ToLower() == "mctoolstripbutton") { System.ComponentModel.Design.IDesignerHost host = (System.ComponentModel.Design.IDesignerHost)MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface.GetService(typeof(System.ComponentModel.Design.IDesignerHost)); foreach (Component cmp in host.Container.Components) { if (cmp is Mesnac.Controls.Default.MCToolStripButton) { mc1 = cmp as Mesnac.Controls.Default.MCToolStripButton; mc.Items.Add(mc1); } } } SetFocusContent(); //聚焦设置 }; MesnacServiceManager.Instance.CustomToolboxService.ResetToolboxItem += delegate { toolBoxWindow.ToolBox.ResetSelection(); }; //处理更改对象名称时,对应的更新属性组合框的值 propertyWindow.PropertyGrid.PropertyValueChanged += delegate(object s, PropertyValueChangedEventArgs e) { if (e.ChangedItem != null) { if (e.ChangedItem.PropertyDescriptor.DisplayName == "Name" || e.ChangedItem.PropertyDescriptor.DisplayName == "名称") { FormDesignerHelper.UpdateComboBox(); SelectedObjectsChanged(); } ((MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface as HostSurface).Loader as Loader.MesnacHostLoader).IsUnSave = true; } }; //处理更改动画属性值 actionWindow.PropertyGrid.PropertyValueChanged += new PropertyValueChangedEventHandler(ActionPropertyGrid_PropertyValueChanged); //处理更改事件属性值 eventWindow.PropertyGrid.PropertyValueChanged += new PropertyValueChangedEventHandler(EventPropertyGrid_PropertyValueChanged); //通过组合框的选择实现组件的选择 propertyWindow.CmbControls.SelectedIndexChanged += new EventHandler(CmbControls_SelectedIndexChanged); actionWindow.CmbControls.SelectedIndexChanged += new EventHandler(CmbControls_SelectedIndexChanged); eventWindow.CmbControls.SelectedIndexChanged += new EventHandler(CmbControls_SelectedIndexChanged); #endregion #region 键盘改变控件位置和大小 FormKeyHandler.Insert(MesnacServiceManager.Instance.HostSurfaceManager); #endregion #region 订阅工作台界面刷新事件,用于多语言处理属性、动画、事件 Workbench.WorkbenchSingleton.Workbench.RefreshUI += delegate(object sender, EventArgs e) { PublicConfig.Instance.RefreshConfig(); //刷新组件属性 if (MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface != null) { System.ComponentModel.Design.ISelectionService ss = MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface.GetService(typeof(System.ComponentModel.Design.ISelectionService)) as System.ComponentModel.Design.ISelectionService; RefreshComponentProperties(ss); } //更新组合框选中的组件 //SelectedObjectsChanged(); }; #endregion } #region 聚焦设置 /// /// 当拖拽完控件,或双击添加完控件,把焦点聚焦到设计器视图上。 /// public static void SetFocusContent() { IViewContent viewContent = WorkbenchSingleton.Workbench.CurrentViewContent as IViewContent; if (viewContent != null) { WorkspaceWindow ww = (viewContent.MyOwner as WorkspaceWindow); if (ww != null) { ww.Activate(); } } } #endregion #endregion #region 选中设计器中不同对象时对应的更新属性窗口的关联对象 /// /// 选中设计器中不同对象时对应的更新属性窗口的关联对象 /// /// /// public static void _selectionService_SelectionChanged(object sender, EventArgs e) { //刷新组件属性 RefreshComponentProperties(sender as System.ComponentModel.Design.ISelectionService); //更新组合框选中的组件 SelectedObjectsChanged(); } #region 刷新组件属性方法 /// /// 刷新组件属性方法 /// /// 组件选择服务对象 public static void RefreshComponentProperties(System.ComponentModel.Design.ISelectionService ss) { #region 获取功能面板对象 PropertyWindow propertyWindow = DisplayUtil.PadInstance; ActionWindow actionWindow = DisplayUtil.PadInstance; EventWindow eventWindow = DisplayUtil.PadInstance; #endregion if (ss != null) { System.Collections.ICollection ic = ss.GetSelectedComponents(); object[] selectedcomp = new object[ic.Count]; ic.CopyTo(selectedcomp, 0); List comps = new List(); List actionAttributes = null; List eventAttributes = null; foreach (object obj in selectedcomp) { #region 组件属性处理 XmlNode tmpXPropertyNode = PublicConfig.Instance.PropertyXmlDoc.SelectSingleNode(String.Format("Components/Component[@Name=\"{0}\"]", obj.GetType().Name)); if (tmpXPropertyNode != null) { XmlNodeList tmpXPropLst = tmpXPropertyNode.SelectNodes("Propertys/Property"); comps.Add(new CustomProperty(obj, tmpXPropLst)); } #endregion #region 组件动画处理 XmlNode tmpXActionNode = PublicConfig.Instance.ActionXmlDoc.SelectSingleNode(String.Format("Components/Component[@Name=\"{0}\"]", obj.GetType().Name)); if (tmpXActionNode != null) { XmlNodeList tmpXActionLst = tmpXActionNode.SelectNodes("Propertys/Property"); if (actionAttributes == null) { actionAttributes = XmlHelper.ConvertListFromNodeList(tmpXActionLst); } else { actionAttributes = actionAttributes.IntersectNode(XmlHelper.ConvertListFromNodeList(tmpXActionLst)); } } else { if (actionAttributes == null) { actionAttributes = new List(); } else { actionAttributes.Clear(); } } #endregion #region 组件事件处理 XmlNode tmpXEventNode = PublicConfig.Instance.EventXmlDoc.SelectSingleNode(String.Format("Components/Component[@Name=\"{0}\"]", obj.GetType().Name)); if (tmpXEventNode != null) { XmlNodeList tmpXEventLst = tmpXEventNode.SelectNodes("Propertys/Property"); if (eventAttributes == null) { eventAttributes = XmlHelper.ConvertListFromNodeList(tmpXEventLst); } else { eventAttributes = eventAttributes.IntersectNode(XmlHelper.ConvertListFromNodeList(tmpXEventLst)); } } else { if (eventAttributes == null) { eventAttributes = new List(); } else { eventAttributes.Clear(); } } #endregion } #region 更新属性网格控件的值 actionWindow.PropertyGrid.Item.Clear(); if (actionAttributes != null) { foreach (XmlNode node in actionAttributes) { object value = XmlHelper.GetPropertyValue(selectedcomp, node.Attributes["Name"].Value); value = value == null ? String.Empty : value; actionWindow.PropertyGrid.Item.Add(node.Attributes["Caption"].Value, value, false, node.Attributes["Group"].Value, node.Attributes["Name"].Value, true); actionWindow.PropertyGrid.Item[actionWindow.PropertyGrid.Item.Count - 1].OnClick += CustomActionItem_OnClick; } } eventWindow.PropertyGrid.Item.Clear(); if (eventAttributes != null) { foreach (XmlNode node in eventAttributes) { object value = XmlHelper.GetPropertyValue(selectedcomp, node.Attributes["Name"].Value); value = value == null ? String.Empty : value; eventWindow.PropertyGrid.Item.Add(node.Attributes["Caption"].Value, value, false, node.Attributes["Group"].Value, node.Attributes["Name"].Value, true); eventWindow.PropertyGrid.Item[eventWindow.PropertyGrid.Item.Count - 1].OnClick += CustomEventItem_OnClick; } } propertyWindow.PropertyGrid.SelectedObjects = comps.ToArray(); actionWindow.PropertyGrid.SelectedObjects = selectedcomp; eventWindow.PropertyGrid.SelectedObjects = selectedcomp; actionWindow.PropertyGrid.ShowCustomProperties = true; eventWindow.PropertyGrid.ShowCustomProperties = true; propertyWindow.PropertyGrid.CommandsVisibleIfAvailable = false; #endregion } } #endregion #region 更新下拉框中的组件列表 /// /// 更新下拉框中的组件列表 /// public static void UpdateComboBox() { PropertyWindow propertyWindow = DisplayUtil.PadInstance; ActionWindow actionWindow = DisplayUtil.PadInstance; EventWindow eventWindow = DisplayUtil.PadInstance; propertyWindow.CmbControls.Items.Clear(); actionWindow.CmbControls.Items.Clear(); eventWindow.CmbControls.Items.Clear(); ICollection collect = MesnacServiceManager.Instance.Host.Container.Components; if (null != collect) { foreach (object obj in collect) { if (obj is Component) //如果是组件 { propertyWindow.CmbControls.Items.Add(obj); actionWindow.CmbControls.Items.Add(obj); eventWindow.CmbControls.Items.Add(obj); } } } } #endregion #region 宿主设计器中选中的对象更改时,同步组合框选中项 /// /// 宿主设计器中选中的对象更改时,同步组合框选中项 /// public static void SelectedObjectsChanged() { PropertyWindow propertyWindow = DisplayUtil.PadInstance; ActionWindow actionWindow = DisplayUtil.PadInstance; EventWindow eventWindow = DisplayUtil.PadInstance; object[] objs = FormDesignerHelper.GetSelectComponents(); if (null != objs && objs.Length == 1) { for (int i = 0; i < propertyWindow.CmbControls.Items.Count; i++) if (objs[0] == propertyWindow.CmbControls.Items[i]) { //先取消事件订阅,防止控件复制时产生死循环 propertyWindow.CmbControls.SelectedIndexChanged -= new EventHandler(CmbControls_SelectedIndexChanged); actionWindow.CmbControls.SelectedIndexChanged -= new EventHandler(CmbControls_SelectedIndexChanged); eventWindow.CmbControls.SelectedIndexChanged -= new EventHandler(CmbControls_SelectedIndexChanged); propertyWindow.CmbControls.SelectedIndex = i; actionWindow.CmbControls.SelectedIndex = i; eventWindow.CmbControls.SelectedIndex = i; propertyWindow.CmbControls.SelectedIndexChanged += new EventHandler(CmbControls_SelectedIndexChanged); actionWindow.CmbControls.SelectedIndexChanged += new EventHandler(CmbControls_SelectedIndexChanged); eventWindow.CmbControls.SelectedIndexChanged += new EventHandler(CmbControls_SelectedIndexChanged); break; } } else { propertyWindow.CmbControls.SelectedIndex = -1; actionWindow.CmbControls.SelectedIndex = -1; eventWindow.CmbControls.SelectedIndex = -1; } } #endregion #region 通过组合框的选择实现组件的选择 /// /// 通过组合框的选择实现组件的选择 /// /// /// public static void CmbControls_SelectedIndexChanged(object sender, EventArgs e) { System.ComponentModel.Design.ISelectionService ss = MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface.GetService(typeof(System.ComponentModel.Design.ISelectionService)) as System.ComponentModel.Design.ISelectionService; if (ss != null) { ComboBox cmb = sender as ComboBox; if (cmb.SelectedItem != null) { List lst = new List(); lst.Add(cmb.SelectedItem as System.ComponentModel.Component); ss.SetSelectedComponents(lst); //设置选择器服务选中的组件 } } } #endregion #endregion #region 设置属性值 /// /// 处理更改动画属性值的事件处理程序 /// /// /// private static void ActionPropertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e) { ICollection selectedComponents = (MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface as HostSurface)._selectionService.GetSelectedComponents(); object[] selectedComp = new object[selectedComponents.Count]; selectedComponents.CopyTo(selectedComp, 0); XmlHelper.SetPropertyValue(selectedComp, e.ChangedItem.PropertyDescriptor.Description, e.ChangedItem.Value); ((MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface as HostSurface).Loader as Loader.MesnacHostLoader).IsUnSave = true; } /// /// 处理更改事件属性值的事件处理程序 /// /// /// private static void EventPropertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e) { ICollection selectedComponents = (MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface as HostSurface)._selectionService.GetSelectedComponents(); object[] selectedComp = new object[selectedComponents.Count]; selectedComponents.CopyTo(selectedComp, 0); XmlHelper.SetPropertyValue(selectedComp, e.ChangedItem.PropertyDescriptor.Description, e.ChangedItem.Value); ((MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface as HostSurface).Loader as Loader.MesnacHostLoader).IsUnSave = true; } //动画视图窗口鼠标单击响应 private static object CustomActionItem_OnClick(object sender, EventArgs e) { PropertyGridEx.CustomProperty prop = (PropertyGridEx.CustomProperty)((PropertyGridEx.CustomProperty.CustomPropertyDescriptor)sender).CustomProperty; if (prop.Description == "MCKey") { //如果是数据源属性 FrmDataItem frmDataItem = new FrmDataItem(); frmDataItem.ShowDialog(); if (frmDataItem.DialogResult == DialogResult.OK) { prop.Value = frmDataItem.DataItem; SetValueForSelectedComp(prop.Description, frmDataItem.DataItem); ((MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface as HostSurface).Loader as Loader.MesnacHostLoader).IsUnSave = true; } frmDataItem.Dispose(); return prop.Value; } //如果是动画属性 FrmVar form = new FrmVar(); form.VarExpress = prop.Value.ToString(); form.SetFillColorVisible(false); form.ShowDialog(); if (form.DialogResult == DialogResult.OK) { prop.Value = form.VarExpress; SetValueForSelectedComp(prop.Description, form.VarExpress); ((MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface as HostSurface).Loader as Loader.MesnacHostLoader).IsUnSave = true; } form.Dispose(); return prop.Value; } //事件视图窗口鼠标单击响应 private static object CustomEventItem_OnClick(object sender, EventArgs e) { PropertyGridEx.CustomProperty prop = (PropertyGridEx.CustomProperty)((PropertyGridEx.CustomProperty.CustomPropertyDescriptor)sender).CustomProperty; FrmEventAction form = new FrmEventAction(); if (prop.Value is List) { form.ActionList = prop.Value as List; } form.ShowDialog(); if (form.DialogResult == DialogResult.OK) { prop.Value = form.ActionList; SetValueForSelectedComp(prop.Description, form.ActionList); ((MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface as HostSurface).Loader as Loader.MesnacHostLoader).IsUnSave = true; } form.Dispose(); return prop.Value; } /// /// 为选中的一组控件的指定属性赋值 /// /// 属性名称 /// 值 private static void SetValueForSelectedComp(string propertyName, object value) { System.ComponentModel.Design.ISelectionService ss = MesnacServiceManager.Instance.HostSurfaceManager.ActiveDesignSurface.GetService(typeof(System.ComponentModel.Design.ISelectionService)) as System.ComponentModel.Design.ISelectionService; if (ss != null) { System.Collections.ICollection ic = ss.GetSelectedComponents(); object[] selectedcomp = new object[ic.Count]; ic.CopyTo(selectedcomp, 0); XmlHelper.SetPropertyValue(selectedcomp, propertyName, value); } } #endregion } }