コード例 #1
0
        // Creates a control from a double-clicked toolbox item and adds
        // it to the parent form.
        private void CreateComponent(object sender, EventArgs e)
        {
            // Obtains an IDesignerHost service from design environment.
            IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));

            // Get the project components container (Windows Forms control
            // containment depends on controls collections).
            IContainer container = host.Container;

            // Identifies the parent Form.
            System.Windows.Forms.Form parentForm = this.FindForm();

            // Retrieves the parent Form's designer host.
            IDesignerHost parentHost = (IDesignerHost)parentForm.Site.GetService(typeof(IDesignerHost));

            // Create the components.
            IComponent[] comps = null;
            try
            {
                comps = toolboxService.GetSelectedToolboxItem().CreateComponents(parentHost);
            }
            catch (Exception ex)
            {
                // Catch and show any exceptions to prevent disabling
                // the control's UI.
                MessageBox.Show(ex.ToString(), "Exception message");
            }
            if (comps == null)
            {
                return;
            }

            // Add any created controls to the parent form's controls
            // collection. Note: components are added from the
            // ToolboxItem.CreateComponents(IDesignerHost) method.
            for (int i = 0; i < comps.Length; i++)
            {
                if (parentForm != null && comps[i].GetType().IsSubclassOf(typeof(System.Windows.Forms.Control)))
                {
                    ((System.Windows.Forms.Control)comps[i]).Location = new Point(20 * controlSpacingMultiplier, 20 * controlSpacingMultiplier);
                    if (controlSpacingMultiplier > 10)
                    {
                        controlSpacingMultiplier = 0;
                    }
                    else
                    {
                        controlSpacingMultiplier++;
                    }
                    parentForm.Controls.Add((System.Windows.Forms.Control)comps[i]);
                }
            }
        }
コード例 #2
0
        protected override void OnMouseDragEnd(bool cancel)
        {
            IUISelectionService selectionServ = this.GetService(typeof(IUISelectionService)) as IUISelectionService;

            if (selectionServ != null)
            {
                // If there is a Toolbox component seleted then create it instead of finishing the selection
                IToolboxService toolBoxService = this.GetService(typeof(IToolboxService)) as IToolboxService;
                if (!cancel && toolBoxService != null && toolBoxService.GetSelectedToolboxItem() != null)
                {
                    if (selectionServ.SelectionInProgress)
                    {
                        bool hasSize = selectionServ.SelectionBounds.Width > 0 &&
                                       selectionServ.SelectionBounds.Height > 0;
                        CreateToolCore(toolBoxService.GetSelectedToolboxItem(),
                                       selectionServ.SelectionBounds.X,
                                       selectionServ.SelectionBounds.Y,
                                       selectionServ.SelectionBounds.Width,
                                       selectionServ.SelectionBounds.Height,
                                       true, hasSize);
                        toolBoxService.SelectedToolboxItemUsed();
                        cancel = true;
                    }
                    else if (!selectionServ.SelectionInProgress &&
                             !selectionServ.ResizeInProgress && !selectionServ.DragDropInProgress)
                    {
                        CreateTool(toolBoxService.GetSelectedToolboxItem(), _mouseDownPoint);
                        toolBoxService.SelectedToolboxItemUsed();
                        cancel = true;
                    }
                }

                if (selectionServ.SelectionInProgress || selectionServ.ResizeInProgress)
                {
                    selectionServ.MouseDragEnd(cancel);
                }
            }
        }
コード例 #3
0
        public IComponent[] CreateTool(ToolboxItem tool, Control parent, int x, int y, int width, int height, bool hasLocation, bool hasSize, ToolboxSnapDragDropEventArgs e)
        {
            // Services we will need
            //
            IToolboxService   toolboxSvc = (IToolboxService)GetService(typeof(IToolboxService));
            ISelectionService selSvc     = (ISelectionService)GetService(typeof(ISelectionService));
            IDesignerHost     host       = (IDesignerHost)GetService(typeof(IDesignerHost));

            IComponent[] comps = Array.Empty <IComponent>();

            Cursor oldCursor = Cursor.Current;

            Cursor.Current = Cursors.WaitCursor;
            DesignerTransaction trans = null;

            try
            {
                try
                {
                    if (host != null)
                    {
                        trans = host.CreateTransaction(string.Format(SR.DesignerBatchCreateTool, tool.ToString()));
                    }
                }
                catch (CheckoutException cxe)
                {
                    if (cxe == CheckoutException.Canceled)
                    {
                        return(comps);
                    }

                    throw;
                }

                try
                {
                    try
                    {
                        // First check if we are currently in localization mode (i.e., language is non-default).
                        // If so, we should not permit addition of new components. This is an intentional
                        // change from Everett - see VSWhidbey #292249.
                        if (host != null && CurrentlyLocalizing(host.RootComponent))
                        {
                            IUIService uiService = (IUIService)GetService(typeof(IUIService));
                            if (uiService != null)
                            {
                                uiService.ShowMessage(SR.LocalizingCannotAdd);
                            }

                            comps = Array.Empty <IComponent>();
                            return(comps);
                        }

                        // Create a dictionary of default values that the designer can
                        // use to initialize a control with.
                        Hashtable defaultValues = new Hashtable();
                        if (parent != null)
                        {
                            defaultValues["Parent"] = parent;
                        }

                        // adjust the location if we are in a mirrored parent. That is because the origin
                        // will then be in the upper right rather than upper left.
                        if (parent != null && parent.IsMirrored)
                        {
                            x += width;
                        }

                        if (hasLocation)
                        {
                            defaultValues["Location"] = new Point(x, y);
                        }
                        if (hasSize)
                        {
                            defaultValues["Size"] = new Size(width, height);
                        }
                        //store off extra behavior drag/drop information
                        if (e != null)
                        {
                            defaultValues["ToolboxSnapDragDropEventArgs"] = e;
                        }

                        comps = tool.CreateComponents(host, defaultValues);
                    }
                    catch (CheckoutException checkoutEx)
                    {
                        if (checkoutEx == CheckoutException.Canceled)
                        {
                            comps = Array.Empty <IComponent>();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    catch (ArgumentException argumentEx)
                    {
                        IUIService uiService = (IUIService)GetService(typeof(IUIService));
                        if (uiService != null)
                        {
                            uiService.ShowError(argumentEx);
                        }
                    }
                    catch (Exception ex)
                    {
                        IUIService uiService = (IUIService)GetService(typeof(IUIService));

                        string exceptionMessage = string.Empty;
                        if (ex.InnerException != null)
                        {
                            exceptionMessage = ex.InnerException.ToString();
                        }

                        if (string.IsNullOrEmpty(exceptionMessage))
                        {
                            exceptionMessage = ex.ToString();
                        }

                        if (ex is InvalidOperationException)
                        {
                            exceptionMessage = ex.Message;
                        }

                        if (uiService != null)
                        {
                            uiService.ShowError(ex, string.Format(SR.FailedToCreateComponent, tool.DisplayName, exceptionMessage));
                        }
                        else
                        {
                            throw;
                        }
                    }

                    if (comps == null)
                    {
                        comps = Array.Empty <IComponent>();
                    }
                }
                finally
                {
                    if (toolboxSvc != null && tool.Equals(toolboxSvc.GetSelectedToolboxItem(host)))
                    {
                        toolboxSvc.SelectedToolboxItemUsed();
                    }
                }
            }
            finally
            {
                if (trans != null)
                {
                    trans.Commit();
                }

                Cursor.Current = oldCursor;
            }

            // Finally, select the newly created components.
            //
            if (selSvc != null && comps.Length > 0)
            {
                if (host != null)
                {
                    host.Activate();
                }

                ArrayList selectComps = new ArrayList(comps);

                for (int i = 0; i < comps.Length; i++)
                {
                    if (!TypeDescriptor.GetAttributes(comps[i]).Contains(DesignTimeVisibleAttribute.Yes))
                    {
                        selectComps.Remove(comps[i]);
                    }
                }

                selSvc.SetSelectedComponents(selectComps.ToArray(), SelectionTypes.Replace);
            }

            codemarkers.CodeMarker((int)CodeMarkerEvent.perfFXDesignCreateComponentEnd);
            return(comps);
        }
コード例 #4
0
        private void Toolbox_MouseDoubleClick(object sender, EventArgs e)
        {
            IDesignerHost   host = this.serviceContainer.GetService(typeof(IDesignerHost)) as IDesignerHost;
            IToolboxService tb   = this.serviceContainer.GetService(typeof(IToolboxService)) as IToolboxService;

            ((IToolboxUser)(DocumentDesigner)host.GetDesigner(host.RootComponent)).ToolPicked(tb.GetSelectedToolboxItem());
        }
コード例 #5
0
 private void OnToolbox_ToolPicked(object sender, EventArgs args)
 {
     if (_workspace != null && _workspace.ActiveDocument != null)
     {
         IDesignerHost   host = _workspace.ActiveDocument.DesignSurface.GetService(typeof(IDesignerHost)) as IDesignerHost;
         IToolboxService tb   = _workspace.ActiveDocument.DesignSurface.GetService(typeof(IToolboxService)) as IToolboxService;
         if (host != null && tb != null)
         {
             ((IToolboxUser)(DocumentDesigner)host.GetDesigner(host.RootComponent)).ToolPicked(tb.GetSelectedToolboxItem());
         }
     }
 }