public void CanParentTest() { ParentControlDesigner rootPanel = (ParentControlDesigner)_helper.IDesignerHost.GetDesigner(_helper.CreateControl(typeof(TestPanel), null)); ParentControlDesigner childPanel = (ParentControlDesigner)_helper.IDesignerHost.GetDesigner(_helper.CreateControl(typeof(TestPanel), rootPanel.Control)); ControlDesigner childPanelButton = (ControlDesigner)_helper.IDesignerHost.GetDesigner(_helper.CreateControl(typeof(TestButton), childPanel.Control)); ControlDesigner rootPanelButton = (ControlDesigner)_helper.IDesignerHost.GetDesigner(_helper.CreateControl(typeof(TestButton), rootPanel.Control)); Assert.IsFalse(childPanel.CanParent((Control)_helper.IDesignerHost.RootComponent), "#1"); Assert.IsTrue(childPanel.CanParent(childPanel), "#2"); Assert.IsTrue(childPanel.CanParent(childPanelButton), "#3"); Assert.IsTrue(childPanel.CanParent(rootPanelButton), "#4"); Assert.IsFalse(childPanel.CanParent(rootPanel), "#5"); Assert.IsTrue(rootPanel.CanParent(childPanel), "#6"); Assert.IsTrue(rootPanel.CanParent(rootPanelButton), "#7"); }
public override void OnDragDrop(Glyph g, DragEventArgs e) { System.Windows.Forms.Design.Behavior.ControlBodyGlyph cbg = g as System.Windows.Forms.Design.Behavior.ControlBodyGlyph; IToolboxService tbsvc = designerHost.GetService(typeof(IToolboxService)) as IToolboxService; ISelectionService selsvc = designerHost.GetService(typeof(ISelectionService)) as ISelectionService; if (cbg != null && cbg.RelatedComponent != null && cbg.RelatedComponent is Control) { ToolboxItem item = e.Data.GetData(typeof(ToolboxItem)) as ToolboxItem; IToolboxUser tbu = designerHost.GetDesigner(designerHost.RootComponent) as IToolboxUser; (designerHost.RootComponent as Control).SuspendLayout(); tbu.ToolPicked(item); if (selsvc.PrimarySelection != null && selsvc.PrimarySelection is Control) { Control createdControl = selsvc.PrimarySelection as Control; PropertyDescriptor parentProperty = TypeDescriptor.GetProperties(createdControl)["Parent"]; //获取粘贴到的父容器 ParentControlDesigner parentDesigner = null; parentDesigner = designerHost.GetDesigner(cbg.RelatedComponent) as ParentControlDesigner; if (parentDesigner == null) { parentDesigner = designerHost.GetDesigner(designerHost.RootComponent) as DocumentDesigner; } if (parentDesigner != null && parentDesigner.CanParent(createdControl)) { parentProperty.SetValue(createdControl, parentDesigner.Control); Point p1 = bhsvc.AdornerWindowToScreen(); } tbsvc.SelectedToolboxItemUsed(); Point adroP = bhsvc.AdornerWindowToScreen(); createdControl.Left = e.X - adroP.X - (cbg.RelatedComponent as Control).Left; createdControl.Top = e.Y - adroP.Y - (cbg.RelatedComponent as Control).Top; } (designerHost.RootComponent as Control).ResumeLayout(); } bhsvc.PopBehavior(this); }
// Reminder: We set control.Parent so that it gets serialized for Undo/Redo // private void Paste(object sender, EventArgs args) { IDesignerSerializationService stateSerializer = GetService(typeof(IDesignerSerializationService)) as IDesignerSerializationService; ISelectionService selection = GetService(typeof(ISelectionService)) as ISelectionService; IDesignerHost host = GetService(typeof(IDesignerHost)) as IDesignerHost; IComponentChangeService changeService = GetService(typeof(IComponentChangeService)) as IComponentChangeService; if (host == null || stateSerializer == null) { return; } // // TODO: MWF X11 doesn't seem to support custom clipboard formats - bug #357642 // // IDataObject dataObject = Clipboard.GetDataObject (); // byte[] data = dataObject == null ? null : dataObject.GetData (DT_DATA_FORMAT) as byte[]; // if (data != null) { // MemoryStream stream = new MemoryStream (data); // stateSerializer.Deserialize (new BinaryFormatter().Deserialize (stream)); // ..... // } // if (_clipboard == null) { return; } DesignerTransaction transaction = host.CreateTransaction("Paste"); ICollection components = stateSerializer.Deserialize(_clipboard); // Console.WriteLine ("Pasted components: "); // foreach (object c in components) // Console.WriteLine (((IComponent)c).Site.Name); System.Collections.Generic.List <object> sel = new System.Collections.Generic.List <object>(components.Count); foreach (object component in components) { sel.Add(component); Control control = component as Control; if (control == null) { continue; // pure Components are added to the ComponentTray by the DocumentDesigner } PropertyDescriptor parentProperty = TypeDescriptor.GetProperties(control)["Parent"]; if (control.Parent != null) { // Already parented during deserialization? // In that case explicitly raise component changing/ed for the Parent property, // so it get's cought by the UndoEngine if (changeService != null) { changeService.OnComponentChanging(control, parentProperty); changeService.OnComponentChanged(control, parentProperty, null, control.Parent); } } else { ParentControlDesigner parentDesigner = null; if (selection != null && selection.PrimarySelection != null) { parentDesigner = host.GetDesigner((IComponent)selection.PrimarySelection) as ParentControlDesigner; } if (parentDesigner == null) { parentDesigner = host.GetDesigner(host.RootComponent) as DocumentDesigner; } if (parentDesigner != null && parentDesigner.CanParent(control)) { parentProperty.SetValue(control, parentDesigner.Control); control.Name = null; (host as IContainer).Add(control); (host as SampleDesignerHost).AddDesignedComponent(control); //parentDesigner.Control.Controls.Add(control); } } } //_clipboard = null; //(host as SampleDesignerHost).SetSelectedComponents(components.); selection.SetSelectedComponents(sel.ToArray(), SelectionTypes.Primary); transaction.Commit(); ((IDisposable)transaction).Dispose(); }
/// <summary> /// 粘贴 /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void Paste(object sender, EventArgs args) { IDesignerSerializationService serialService = this.GetService(typeof(IDesignerSerializationService)) as IDesignerSerializationService; IDesignerHost host = GetService(typeof(IDesignerHost)) as IDesignerHost; ISelectionService selection = GetService(typeof(ISelectionService)) as ISelectionService; IComponentChangeService changeService = GetService(typeof(IComponentChangeService)) as IComponentChangeService; if (host == null || serialService == null || clipboardData == null) { return; } //从获取上次剪切/复制的数据并反序列化 ICollection components = serialService.Deserialize(clipboardData); //如果当前选中的控件是容器控件则添加到容器中 //否则添加到根设计器中 if (components != null && components.Count > 0) { DesignerTransaction transaction = host.CreateTransaction("Paste"); foreach (Component item in components) { Control control = item as Control; if (control == null) { continue; } //if (control is IDBColProperty) //{ // (control as IDBColProperty).DBColName = string.Empty; //} PropertyDescriptor parentProperty = TypeDescriptor.GetProperties(control)["Parent"]; //获取粘贴到的父容器 ParentControlDesigner parentDesigner = null; if (selection != null && selection.PrimarySelection != null) { parentDesigner = host.GetDesigner((IComponent)selection.PrimarySelection) as ParentControlDesigner; } if (parentDesigner == null) { parentDesigner = host.GetDesigner(host.RootComponent) as DocumentDesigner; } if (parentDesigner != null && parentDesigner.CanParent(control)) { //粘贴时粘贴到父容器中间 //control.Location = new Point(parentDesigner.Control.Width / 2 - control.Width / 2, parentDesigner.Control.Height / 2 - control.Height / 2); parentProperty.SetValue(control, parentDesigner.Control); } } clipboardData = null; transaction.Commit(); ((IDisposable)transaction).Dispose(); selection.SetSelectedComponents(components); this.GlobalInvoke(StandardCommands.BringToFront); } }