Пример #1
0
        public void ToolBoxItemButtonKeyDown(System.Windows.Forms.KeyEventArgs e, System.Drawing.Design.ToolboxItem toolboxItem)
        {
            if (toolboxItem == null)
            {
                return;
            }


            switch (e.KeyCode)
            {
            case Keys.Enter:
                if (DesignerHost == null)
                {
                    MessageBox.Show("idh Null");
                }

                IToolboxUser tbu = DesignerHost.GetDesigner(DesignerHost.RootComponent as IComponent) as IToolboxUser;

                if (tbu != null)
                {
                    // Enter means place the tool with default location and default size.
                    tbu.ToolPicked(toolboxItem);
                }
                break;

            default:
            {
                Console.WriteLine("Error: Not able to add");
                break;
            }
            }
        }
Пример #2
0
        /// The toolbox can also be navigated using the keyboard commands Up, Down, and Enter.
        private void list_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            ListBox   lbSender           = sender as ListBox;
            Rectangle lastSelectedBounds = lbSender.GetItemRectangle(selectedIndex);

            switch (e.KeyCode)
            {
            case Keys.Up: if (selectedIndex > 0)
                {
                    selectedIndex--;                                               // change selection
                    lbSender.SelectedIndex = selectedIndex;
                    lbSender.Invalidate(lastSelectedBounds);                       // clear old highlight
                    lbSender.Invalidate(lbSender.GetItemRectangle(selectedIndex)); // add new one
                }
                break;

            case Keys.Down: if (selectedIndex + 1 < lbSender.Items.Count)
                {
                    selectedIndex++;                                               // change selection
                    lbSender.SelectedIndex = selectedIndex;
                    lbSender.Invalidate(lastSelectedBounds);                       // clear old highlight
                    lbSender.Invalidate(lbSender.GetItemRectangle(selectedIndex)); // add new one
                }
                break;

            case Keys.Enter: IToolboxUser tbu = host.GetDesigner(host.RootComponent) as IToolboxUser;
                if (tbu != null)
                {
                    // Enter means place the tool with default location and default size.
                    tbu.ToolPicked((ToolboxItem)(lbSender.Items[selectedIndex]));
                }
                break;
            }
        }
Пример #3
0
        private void list_MouseDown(object sender, MouseEventArgs e)
        {
            ListBox   lbSender           = sender as ListBox;
            Rectangle lastSelectedBounds = lbSender.GetItemRectangle(this.selectedIndex);

            this.selectedIndex     = lbSender.IndexFromPoint(e.X, e.Y);
            lbSender.SelectedIndex = this.selectedIndex;
            lbSender.Invalidate(lastSelectedBounds);
            lbSender.Invalidate(lbSender.GetItemRectangle(this.selectedIndex));
            if (this.selectedIndex != 0)
            {
                if (e.Clicks == 2)
                {
                    IToolboxUser tbu = this.host.GetDesigner(this.host.RootComponent) as IToolboxUser;
                    if (tbu != null)
                    {
                        tbu.ToolPicked((ToolboxItem)lbSender.Items[this.selectedIndex]);
                    }
                }
                else if (e.Clicks < 2)
                {
                    ToolboxItem     tbi = lbSender.Items[this.selectedIndex] as ToolboxItem;
                    IToolboxService tbs = ((IServiceProvider)this.host).GetService(typeof(IToolboxService)) as IToolboxService;
                    DataObject      d   = tbs.SerializeToolboxItem(tbi) as DataObject;
                    try
                    {
                        lbSender.DoDragDrop(d, DragDropEffects.Copy);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                }
            }
        }
Пример #4
0
        private void list_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try
            {
                ListBox   lbSender           = sender as ListBox;
                Rectangle lastSelectedBounds = lbSender.GetItemRectangle(0);
                try
                {
                    lastSelectedBounds = lbSender.GetItemRectangle(selectedIndex);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }

                selectedIndex          = lbSender.IndexFromPoint(e.X, e.Y);    // change our selection
                lbSender.SelectedIndex = selectedIndex;
                lbSender.Invalidate(lastSelectedBounds);                       // clear highlight from last selection
                lbSender.Invalidate(lbSender.GetItemRectangle(selectedIndex)); // highlight new one

                if (selectedIndex != 0)
                {
                    SelectItemChangedFunction((System.Drawing.Design.ToolboxItem)(lbSender.Items[selectedIndex]));

                    if (e.Clicks == 2)
                    {
                        CreateToolboxItemFunction();
                        IDesignerHost idh = (IDesignerHost)this.DesignerHost.GetService(typeof(IDesignerHost));
                        IToolboxUser  tbu = idh.GetDesigner(idh.RootComponent as IComponent) as IToolboxUser;

                        if (tbu != null)
                        {
                            tbu.ToolPicked((System.Drawing.Design.ToolboxItem)(lbSender.Items[selectedIndex]));
                        }
                    }
                    else if (e.Clicks < 2)
                    {
                        System.Drawing.Design.ToolboxItem tbi = lbSender.Items[selectedIndex] as System.Drawing.Design.ToolboxItem;
                        IToolboxService tbs = this;

                        // The IToolboxService serializes ToolboxItems by packaging them in DataObjects.
                        DataObject d = tbs.SerializeToolboxItem(tbi) as DataObject;

                        try
                        {
                            lbSender.DoDragDrop(d, DragDropEffects.Copy);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
Пример #5
0
        private void listBar1_MouseDown(object sender, MouseEventArgs e)
        {
            ListBarItem selected = thisMouseTrack.GetValue(listBar1) as ListBarItem;

            if (selected == null)
            {
                return;
            }
            else
            {
                deselectAll();
                selected.Selected = true;
                selectedIndex     = listBar1.SelectedGroup.Items.IndexOf(selected);
                //deselectAll();
            }
            if (selectedIndex != 0)
            {
                // If this is a double-click, then the user wants to add the selected component
                // to the default location on the designer, with the default size. We call
                // ToolPicked on the current designer (as a IToolboxUser) to place the tool.
                // The IToolboxService calls SelectedToolboxItemUsed(), which calls this control's
                // SelectPointer() method.
                //
                if (e.Clicks == 2)
                {
                    IToolboxUser tbu = host.GetDesigner(host.RootComponent) as IToolboxUser;
                    if (tbu != null)
                    {
                        //tbu.ToolPicked((ToolboxItem)(lbSender.Items[selectedIndex]));
                        tbu.ToolPicked((ToolboxItem)(listBar1.SelectedGroup.SelectedItem.Tag));
                    }
                    SelectPointer();
                }
                // Otherwise this is either a single click or a drag. Either way, we do the same
                // thing: start a drag--if this is just a single click, then the drag will
                // abort as soon as there's a MouseUp event.
                //
                else if (e.Clicks < 2)
                {
                    //ToolboxItem tbi = lbSender.Items[selectedIndex] as ToolboxItem;
                    ToolboxItem     tbi = listBar1.SelectedGroup.SelectedItem.Tag as ToolboxItem;
                    IToolboxService tbs = ((IServiceContainer)host).GetService(typeof(IToolboxService)) as IToolboxService;

                    // The IToolboxService serializes ToolboxItems by packaging them in DataObjects.
                    DataObject d = tbs.SerializeToolboxItem(tbi) as DataObject;
                    try
                    {
                        (sender as Control).DoDragDrop(d, DragDropEffects.Copy);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
Пример #6
0
        private void OnListBoxDoubleClick(object sener, EventArgs e)
        {
            IToolboxUser        docDes       = null;
            SelfHostToolboxItem selectedItem = listBox.SelectedItem as SelfHostToolboxItem;

            if (selectedItem == null)
            {
                return;
            }

            this.currentSelection = selectedItem.ComponentType;
            IDesignerHost host = (IDesignerHost)provider.GetService(typeof(IDesignerHost));            //des.ActiveDesigner;

            if (host != null && this.currentSelection != null)
            {
                IDesigner designer = host.GetDesigner(host.RootComponent);
                if (designer is IToolboxUser)
                {
                    docDes = (IToolboxUser)designer;
                }

                if (docDes != null)
                {
                    ToolboxItem c = ToolboxService.GetToolboxItem(this.currentSelection);
                    Debug.Assert(c != null, "Class " + this.currentSelection.FullName + " does not exist");
                    if (c != null && docDes.GetToolSupported(c))
                    {
                        try
                        {
                            docDes.ToolPicked(c);
                        }
                        catch (Exception ex)
                        {
                            IUIService uis = (IUIService)provider.GetService(typeof(IUIService));

                            if (uis != null)
                            {
                                uis.ShowError(ex);
                            }
                            else
                            {
                                MessageBox.Show("Error: " + ex.ToString());
                            }
                        }
                    }
                }
                else
                {
                    object o = Activator.CreateInstance(this.currentSelection);
                    SequentialWorkflowActivity service = host.RootComponent as SequentialWorkflowActivity;
                    service.Activities.Add(o as Activity);
                    host.RootComponent.Site.Container.Add(o as IComponent);
                }
            }
        }
Пример #7
0
 void navBarControl_DoubleClick(object sender, EventArgs e)
 {
     if (SelectedToolboxItem != null)
     {
         IToolboxUser tbu = this.DesignerHost.GetDesigner(this.DesignerHost.RootComponent as IComponent) as IToolboxUser;
         if (tbu != null)
         {
             tbu.ToolPicked(SelectedToolboxItem);
         }
     }
 }
Пример #8
0
        /// This method handles a MouseDown event, which might be one of three things:
        ///        1) the start of a single click
        ///        2) the start of a drag
        ///        3) the start of a second of two consecutive clicks (double-click)
        private void list_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // Regardless of which kind of click this is, we need to change the selection.
            // First we grab the bounds of the old selected tool so that we can de-higlight it.
            //
            ListBox   lbSender           = sender as ListBox;
            Rectangle lastSelectedBounds = lbSender.GetItemRectangle(selectedIndex);

            selectedIndex          = lbSender.IndexFromPoint(e.X, e.Y); // change our selection
            lbSender.SelectedIndex = selectedIndex;

            lbSender.Invalidate(lastSelectedBounds);                       // clear highlight from last selection
            lbSender.Invalidate(lbSender.GetItemRectangle(selectedIndex)); // highlight new one

            if (selectedIndex != 0)
            {
                // If this is a double-click, then the user wants to add the selected component
                // to the default location on the designer, with the default size. We call
                // ToolPicked on the current designer (as a IToolboxUser) to place the tool.
                // The IToolboxService calls SelectedToolboxItemUsed(), which calls this control's
                // SelectPointer() method.
                //
                if (e.Clicks == 2)
                {
                    IToolboxUser tbu = host.GetDesigner(host.RootComponent) as IToolboxUser;
                    if (tbu != null)
                    {
                        tbu.ToolPicked((ToolboxItem)(lbSender.Items[selectedIndex]));
                    }
                }
                // Otherwise this is either a single click or a drag. Either way, we do the same
                // thing: start a drag--if this is just a single click, then the drag will
                // abort as soon as there's a MouseUp event.
                //
                else if (e.Clicks < 2)
                {
                    ToolboxItem     tbi = lbSender.Items[selectedIndex] as ToolboxItem;
                    IToolboxService tbs = ((IServiceContainer)host).GetService(typeof(IToolboxService)) as IToolboxService;

                    // The IToolboxService serializes ToolboxItems by packaging them in DataObjects.
                    DataObject d = tbs.SerializeToolboxItem(tbi) as DataObject;
                    try
                    {
                        lbSender.DoDragDrop(d, DragDropEffects.Copy);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
Пример #9
0
        public ToolboxItem GetSelectedToolboxItem(System.ComponentModel.Design.IDesignerHost host)
        {
            IToolboxUser toolboxUser = (IToolboxUser)host.GetDesigner(host.RootComponent);

            if (toolboxUser.GetToolSupported(selectedItem))
            {
                return(selectedItem);
            }
            else
            {
                return(null);
            }
        }
Пример #10
0
        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);
        }
Пример #11
0
        public static void Paste()
        {
            List <object> list = PasteList;

            if (list != null)
            {
                if (_designerHost != null)
                {
                    IToolboxUser tbu = _designerHost.GetDesigner(_designerHost.RootComponent as System.ComponentModel.IComponent) as IToolboxUser;

                    if (tbu != null)
                    {
                        tbu.ToolPicked(new System.Drawing.Design.ToolboxItem(list[_pasteIndex].GetType()));
                    }
                }
            }
        }
Пример #12
0
        public ToolboxItemCollection GetToolboxItems(System.ComponentModel.Design.IDesignerHost host)
        {
            ArrayList    tools       = new ArrayList();
            IToolboxUser toolboxUser = (IToolboxUser)host.GetDesigner(host.RootComponent);

            foreach (ArrayList arr in categories.Values)
            {
                foreach (ToolboxItem tool in arr)
                {
                    if (toolboxUser.GetToolSupported(tool))
                    {
                        tools.Add(tool);
                    }
                }
            }

            return(new ToolboxItemCollection((ToolboxItem[])tools.ToArray(typeof(ToolboxItem))));
        }
Пример #13
0
        //evaluate a filter attribute against a list, and check whther permitted
        private bool FilterPermitted(ToolboxItemFilterAttribute desFa, ICollection filterAgainst, object rootDesigner)
        {
            switch (desFa.FilterType)
            {
            case ToolboxItemFilterType.Allow:
                //this is really for matching some other filter string against
                return(true);

            case ToolboxItemFilterType.Custom:
                IToolboxUser tbUser = rootDesigner as IToolboxUser;
                if (tbUser == null)
                {
                    throw new ArgumentException("Host's root designer does not support IToolboxUser interface.");
                }
                return(EvaluateCustomFilter(tbUser));

            case ToolboxItemFilterType.Prevent:
                //if host and toolboxitem have same filterstring, then not permitted
                foreach (ToolboxItemFilterAttribute itemFa in filterAgainst)
                {
                    if (desFa.Match(itemFa))
                    {
                        return(false);
                    }
                }
                return(true);

            case ToolboxItemFilterType.Require:
                //if host and toolboxitem have same filterstring, then permitted, unless one is prevented
                foreach (ToolboxItemFilterAttribute itemFa in filterAgainst)
                {
                    if (desFa.Match(itemFa) && (desFa.FilterType != ToolboxItemFilterType.Prevent))
                    {
                        return(true);
                    }
                }
                return(false);
            }
            throw new InvalidOperationException("Unexpected ToolboxItemFilterType value.");
        }
Пример #14
0
        public void ToolBoxItemButtonMouseDown(System.Windows.Forms.MouseEventArgs e, System.Drawing.Design.ToolboxItem toolboxItem)
        {
            if (toolboxItem == null)
            {
                return;
            }

            if (this.DesignerHost == null)
            {
                return;
            }

            if (e.Clicks == 2)
            {
                IDesignerHost idh = (IDesignerHost)this.DesignerHost.GetService(typeof(IDesignerHost));
                IToolboxUser  tbu = idh.GetDesigner(idh.RootComponent as IComponent) as IToolboxUser;

                if (tbu != null)
                {
                    tbu.ToolPicked(toolboxItem);
                }
            }
            else if (e.Clicks < 2)
            {
                IToolboxService tbs = this;

                // The IToolboxService serializes ToolboxItems by packaging them in DataObjects.
                DataObject d = tbs.SerializeToolboxItem(toolboxItem) as DataObject;

                try
                {
                    this.DoDragDrop(d, DragDropEffects.Copy);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Пример #15
0
        }//end_main

        private static void CreateControlsUsingTheToolbox(IDesignSurfaceExt surface)
        {
            try {
                IDesignerHost idh = (IDesignerHost)surface.GetIDesignerHost();
                IToolboxUser  itu = (IToolboxUser)idh.GetDesigner(idh.RootComponent);
                itu.ToolPicked(new ToolboxItem(typeof(Button)));
                //- the control just deployed on the Designsurface
                //- therefore it is still selected
                //- get the SelectionService
                ISelectionService selectionService  = (ISelectionService)(idh.GetService(typeof(ISelectionService)));
                object            objCtrlJustPlaced = selectionService.PrimarySelection;
                //- we know its a control
                Control ctrlJustPlaced = (Control)objCtrlJustPlaced;
                ctrlJustPlaced.Size      = new Size(((Form)idh.RootComponent).ClientSize.Width - 10, 40);
                ctrlJustPlaced.Location  = new Point(0, ((Form)idh.RootComponent).ClientSize.Height - ctrlJustPlaced.Size.Height);
                ctrlJustPlaced.Text      = "I'm the Button placed via Toolbox";
                ctrlJustPlaced.BackColor = Color.WhiteSmoke;
            }//end_try
            catch (Exception exx) {
                Console.WriteLine(" the DesignSurface has generated an Exception: " + exx.Message);
            }//end_catch
        }
Пример #16
0
        private void ToolBoxSelect()
        {
            _toolboxService = new CustomToolboxService();
            // 选中不同的工具条项目
            m_toolbox.toolBox.SelectedItemChanged += delegate(object sender, ToolBoxItem newItem)
            {
                _toolboxService.SetSelectedToolboxItem(newItem.Tag as System.Drawing.Design.ToolboxItem);
            };

            // 双击工具栏项目时增加到设计器中
            m_toolbox.toolBox.ItemDoubleClicked += delegate(object sender, ToolBoxItem newItem)
            {
                System.Drawing.Design.ToolboxItem toolboxItem = newItem.Tag as System.Drawing.Design.ToolboxItem;
                if (null != toolboxItem && ActivPage.surFace != null)
                {
                    IToolboxUser toolboxUser = ActivPage.host.GetDesigner(ActivPage.host.RootComponent as IComponent) as IToolboxUser;
                    if (null != toolboxUser)
                    {
                        toolboxUser.ToolPicked(toolboxItem);
                    }
                }
            };

            // 拖动工具栏项目的支持代码
            m_toolbox.toolBox.ItemDragStart += delegate(object sender, ToolBoxItem newItem)
            {
                System.Drawing.Design.ToolboxItem toolboxItem = newItem.Tag as System.Drawing.Design.ToolboxItem;
                if (null != toolboxItem)
                {
                    DataObject dataObject = ((IToolboxService)_toolboxService).SerializeToolboxItem(toolboxItem) as DataObject;
                    m_toolbox.toolBox.DoDragDrop(dataObject, DragDropEffects.Copy);
                }
            };
            _toolboxService.ResetToolboxItem += delegate
            {
                m_toolbox.toolBox.ResetSelection();
            };
        }
Пример #17
0
        private void list_KeyDown(object sender, KeyEventArgs e)
        {
            ListBox   lbSender           = sender as ListBox;
            Rectangle lastSelectedBounds = lbSender.GetItemRectangle(this.selectedIndex);

            switch (e.KeyCode)
            {
            case Keys.Up:
                if (this.selectedIndex > 0)
                {
                    this.selectedIndex--;
                    lbSender.SelectedIndex = this.selectedIndex;
                    lbSender.Invalidate(lastSelectedBounds);
                    lbSender.Invalidate(lbSender.GetItemRectangle(this.selectedIndex));
                }
                break;

            case Keys.Down:
                if ((this.selectedIndex + 1) < lbSender.Items.Count)
                {
                    this.selectedIndex++;
                    lbSender.SelectedIndex = this.selectedIndex;
                    lbSender.Invalidate(lastSelectedBounds);
                    lbSender.Invalidate(lbSender.GetItemRectangle(this.selectedIndex));
                }
                break;

            case Keys.Return:
            {
                IToolboxUser tbu = this.host.GetDesigner(this.host.RootComponent) as IToolboxUser;
                if (tbu != null)
                {
                    tbu.ToolPicked((ToolboxItem)lbSender.Items[this.selectedIndex]);
                }
                break;
            }
            }
        }
Пример #18
0
        protected override void OnDragDrop(DragEventArgs drgevent)
        {
            if (drgevent.Effect == DragDropEffects.Copy)
            {
                (designerHost.RootComponent as Control).SuspendLayout();

                IToolboxService   tbsvc  = designerHost.GetService(typeof(IToolboxService)) as IToolboxService;
                IToolboxUser      tbu    = designerHost.GetDesigner(designerHost.RootComponent) as IToolboxUser;
                ISelectionService selsvc = designerHost.GetService(typeof(ISelectionService)) as ISelectionService;

                DesignerTransaction transaction = designerHost.CreateTransaction("DoDragDrop");

                //避免当前有选中控件,会把新创建的控件放到选中控件中
                selsvc.SetSelectedComponents(null);

                ToolboxItem item = drgevent.Data.GetData(typeof(ToolboxItem)) as ToolboxItem;
                tbu.ToolPicked(item);
                ICollection components = item.CreateComponents();
                tbsvc.SelectedToolboxItemUsed();

                Control createdControl = selsvc.PrimarySelection as Control;
                Point   location1      = PointToScreen(this.Location);
                createdControl.Location = new Point(drgevent.X - location1.X + this.Left, drgevent.Y - location1.Y + this.Top);
                selsvc.SetSelectedComponents(null);
                selsvc.SetSelectedComponents(new Control[] { createdControl });

                transaction.Commit();
                ((IDisposable)transaction).Dispose();

                (designerHost.RootComponent as Control).ResumeLayout();
            }
            else
            {
                base.OnDragDrop(drgevent);
            }
        }
Пример #19
0
        public void AddControl(ToolboxItem tbi)
        {
            IToolboxUser tbu = host.GetDesigner(host.RootComponent) as IToolboxUser;

            tbu.ToolPicked(tbi);
        }
Пример #20
0
        private void list_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            try
            {
                ListBox   lbSender           = sender as ListBox;
                Rectangle lastSelectedBounds = lbSender.GetItemRectangle(0);
                try
                {
                    lastSelectedBounds = lbSender.GetItemRectangle(selectedIndex);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }

                switch (e.KeyCode)
                {
                case Keys.Up: if (selectedIndex > 0)
                    {
                        selectedIndex--;                                                 // change selection
                        lbSender.SelectedIndex = selectedIndex;
                        lbSender.Invalidate(lastSelectedBounds);                         // clear old highlight
                        lbSender.Invalidate(lbSender.GetItemRectangle(selectedIndex));   // add new one
                    }
                    break;

                case Keys.Down: if (selectedIndex + 1 < lbSender.Items.Count)
                    {
                        selectedIndex++;                                                         // change selection
                        lbSender.SelectedIndex = selectedIndex;
                        lbSender.Invalidate(lastSelectedBounds);                                 // clear old highlight
                        lbSender.Invalidate(lbSender.GetItemRectangle(selectedIndex));           // add new one
                    }
                    break;

                case Keys.Enter:
                    if (DesignerHost == null)
                    {
                        MessageBox.Show("设计器为空!");
                    }

                    IToolboxUser tbu = DesignerHost.GetDesigner(DesignerHost.RootComponent as IComponent) as IToolboxUser;

                    if (tbu != null)
                    {
                        // Enter means place the tool with default location and default size.
                        tbu.ToolPicked((System.Drawing.Design.ToolboxItem)(lbSender.Items[selectedIndex]));
                        lbSender.Invalidate(lastSelectedBounds);                                  // clear old highlight
                        lbSender.Invalidate(lbSender.GetItemRectangle(selectedIndex));            // add new one
                    }

                    break;

                default:
                {
                    MessageBox.Show("不能正确添加!");
                    break;
                }
                }                 // switch
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void list_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // Regardless of which kind of click this is, we need to change the selection.
            // First we grab the bounds of the old selected tool so that we can de-higlight it.
            //
            ListBox lbSender = sender as ListBox;

            if (selectedIndex < 0)
            {
                selectedIndex = 0;
            }
            bPrepareDrag  = false;
            selectedIndex = lbSender.IndexFromPoint(e.X, e.Y);             // change our selection
            if (selectedIndex < 0)
            {
                selectedIndex = 0;
            }
            lbSender.SelectedIndex = selectedIndex;
            if (selectedIndex >= 0)
            {
                if (e.Button == MouseButtons.Right)
                {
                    ContextMenu mnu = new ContextMenu();
                    MenuItem    mi;
                    if (!_readOnly)
                    {
                        mi        = new MenuItem("Add Toolbox Item");
                        mi.Click += new EventHandler(miAdd_Click);
                        mi.Tag    = selectedIndex;
                        mnu.MenuItems.Add(mi);
                        if (selectedIndex > 0)
                        {
                            ToolboxItem item = lbSender.Items[selectedIndex] as ToolboxItem;
                            mi        = new MenuItem("Delete " + item.DisplayName);
                            mi.Click += new EventHandler(miDel_Click);
                            mi.Tag    = selectedIndex;
                            mnu.MenuItems.Add(mi);
                        }
                        Form f = lbSender.FindForm();
                        if (f != null)
                        {
                            Point p = lbSender.Parent.PointToScreen(new Point(e.X, e.Y));
                            p = f.PointToClient(p);
                            mnu.Show(f, p);
                        }
                    }
                }
                else
                {
                    if (toolbox != null && toolbox.Host != null)
                    {
                        // If this is a double-click, then the user wants to add the selected component
                        // to the default location on the designer, with the default size. We call
                        // ToolPicked on the current designer (as a IToolboxUser) to place the tool.
                        // The IToolboxService calls SelectedToolboxItemUsed(), which calls this control's
                        // SelectPointer() method.
                        //

                        if (e.Clicks == 2)
                        {
                            IToolboxUser tbu = toolbox.Host.GetDesigner(toolbox.Host.RootComponent) as IToolboxUser;
                            if (tbu != null)
                            {
                                ToolboxItemXType.SelectedToolboxTypeKey = string.Empty;
                                ToolboxItem  ti  = (ToolboxItem)(lbSender.Items[selectedIndex]);
                                xToolboxItem xti = ti as xToolboxItem;
                                if (xti != null)
                                {
                                    if (typeof(ToolboxItemXType).IsAssignableFrom(xti.Type))
                                    {
                                        ToolboxItemXType.SelectedToolboxTypeKey = ti.DisplayName;
                                        ToolboxItemXType.SelectedToolboxClassId = 0;
                                        ToolboxItemXType.SelectedToolboxType    = null;
                                    }
                                    else
                                    {
                                        if (xti.Properties.Contains("ClassId"))
                                        {
                                            ToolboxItemXType.SelectedToolboxClassId = (UInt32)xti.Properties["ClassId"];
                                            ToolboxItemXType.SelectedToolboxTypeKey = string.Empty;
                                            ToolboxItemXType.SelectedToolboxType    = null;
                                        }
                                        if (xti.Properties.Contains("DisplayName"))
                                        {
                                            ToolboxItemXType.SelectedToolboxClassName = (string)xti.Properties["DisplayName"];
                                        }
                                    }
                                }

                                tbu.ToolPicked(ti);
                                toolbox.HideToolBox(this, null);
                            }
                        }
                        // Otherwise this is either a single click or a drag. Either way, we do the same
                        // thing: start a drag--if this is just a single click, then the drag will
                        // abort as soon as there's a MouseUp event.
                        //
                        else if (e.Clicks < 2)
                        {
                            x0             = e.X;
                            y0             = e.Y;
                            nSelectedIndex = selectedIndex;
                            bPrepareDrag   = true;
                        }
                    }
                }
            }
        }
Пример #22
0
 //if derived class supports it, we have to load the real toolboxitem
 protected virtual bool EvaluateCustomFilter(IToolboxUser user)
 {
     throw new InvalidOperationException ("Custom filters are only supported with ToolboxItems.");
 }
Пример #23
0
 protected override bool EvaluateCustomFilter(IToolboxUser user)
 {
     return user.GetToolSupported (item);
 }
Пример #24
0
        private void ToolsListBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            try
            {
                ListBox   LbSender           = sender as ListBox;
                Rectangle LastSelectedBounds = LbSender.GetItemRectangle(0);
                try
                {
                    LastSelectedBounds = LbSender.GetItemRectangle(SelectedIndex);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }

                switch (e.KeyCode)
                {
                case Keys.Up: if (SelectedIndex > 0)
                    {
                        SelectedIndex--;                                                 // change selection
                        LbSender.SelectedIndex = SelectedIndex;
                        LbSender.Invalidate(LastSelectedBounds);                         // clear old highlight
                        LbSender.Invalidate(LbSender.GetItemRectangle(SelectedIndex));   // add new one
                    }
                    break;

                case Keys.Down: if (SelectedIndex + 1 < LbSender.Items.Count)
                    {
                        SelectedIndex++;                                                         // change selection
                        LbSender.SelectedIndex = SelectedIndex;
                        LbSender.Invalidate(LastSelectedBounds);                                 // clear old highlight
                        LbSender.Invalidate(LbSender.GetItemRectangle(SelectedIndex));           // add new one
                    }
                    break;

                case Keys.Enter:
                    if (DesignerHost == null)
                    {
                        MessageBox.Show("idh Null");
                    }

                    IToolboxUser TBU = DesignerHost.GetDesigner(DesignerHost.RootComponent as IComponent) as IToolboxUser;

                    if (TBU != null)
                    {
                        // Enter means place the tool with default location and default size.
                        TBU.ToolPicked((System.Drawing.Design.ToolboxItem)(LbSender.Items[SelectedIndex]));
                        LbSender.Invalidate(LastSelectedBounds);                                  // clear old highlight
                        LbSender.Invalidate(LbSender.GetItemRectangle(SelectedIndex));            // add new one
                    }

                    break;

                default:
                {
                    Console.WriteLine("Error: Not able to add");
                    break;
                }
                }                 // switch
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
Пример #25
0
        private bool Update()
        {
            bool       flag             = false;
            IDesigner  designer         = null;
            IComponent primarySelection = this._selectionService.PrimarySelection as IComponent;

            if (primarySelection != null)
            {
                designer = this._host.GetDesigner(primarySelection);
            }
            if (designer == null)
            {
                primarySelection = this._host.RootComponent;
                if (primarySelection != null)
                {
                    designer = this._host.GetDesigner(primarySelection);
                }
            }
            if (designer != this._filterDesigner)
            {
                ArrayList list;
                if (designer != null)
                {
                    AttributeCollection designerAttributes = this.GetDesignerAttributes(designer);
                    list = new ArrayList(designerAttributes.Count);
                    foreach (Attribute attribute in designerAttributes)
                    {
                        if (attribute is ToolboxItemFilterAttribute)
                        {
                            list.Add(attribute);
                        }
                    }
                }
                else
                {
                    list = new ArrayList();
                }
                if (this._filter == null)
                {
                    flag = true;
                }
                else if (this._filter.Count != list.Count)
                {
                    flag = true;
                }
                else
                {
                    IEnumerator enumerator  = this._filter.GetEnumerator();
                    IEnumerator enumerator2 = list.GetEnumerator();
                    while (enumerator2.MoveNext())
                    {
                        enumerator.MoveNext();
                        if (!enumerator2.Current.Equals(enumerator.Current))
                        {
                            flag = true;
                            break;
                        }
                        ToolboxItemFilterAttribute current = (ToolboxItemFilterAttribute)enumerator2.Current;
                        if (current.FilterType == ToolboxItemFilterType.Custom)
                        {
                            flag = true;
                            break;
                        }
                    }
                }
                this._filter         = list;
                this._filterDesigner = designer;
                this._toolboxUser    = this._filterDesigner as IToolboxUser;
                if (this._toolboxUser == null)
                {
                    IDesigner parent;
                    for (ITreeDesigner designer2 = this._filterDesigner as ITreeDesigner; (this._toolboxUser == null) && (designer2 != null); designer2 = parent as ITreeDesigner)
                    {
                        parent            = designer2.Parent;
                        this._toolboxUser = parent as IToolboxUser;
                    }
                }
                if ((this._toolboxUser == null) && (this._host.RootComponent != null))
                {
                    this._toolboxUser = this._host.GetDesigner(this._host.RootComponent) as IToolboxUser;
                }
            }
            if (this._filter == null)
            {
                this._filter = new ArrayList();
            }
            return(flag);
        }
 private bool Update()
 {
     bool flag = false;
     IDesigner designer = null;
     IComponent primarySelection = this._selectionService.PrimarySelection as IComponent;
     if (primarySelection != null)
     {
         designer = this._host.GetDesigner(primarySelection);
     }
     if (designer == null)
     {
         primarySelection = this._host.RootComponent;
         if (primarySelection != null)
         {
             designer = this._host.GetDesigner(primarySelection);
         }
     }
     if (designer != this._filterDesigner)
     {
         ArrayList list;
         if (designer != null)
         {
             AttributeCollection designerAttributes = this.GetDesignerAttributes(designer);
             list = new ArrayList(designerAttributes.Count);
             foreach (Attribute attribute in designerAttributes)
             {
                 if (attribute is ToolboxItemFilterAttribute)
                 {
                     list.Add(attribute);
                 }
             }
         }
         else
         {
             list = new ArrayList();
         }
         if (this._filter == null)
         {
             flag = true;
         }
         else if (this._filter.Count != list.Count)
         {
             flag = true;
         }
         else
         {
             IEnumerator enumerator = this._filter.GetEnumerator();
             IEnumerator enumerator2 = list.GetEnumerator();
             while (enumerator2.MoveNext())
             {
                 enumerator.MoveNext();
                 if (!enumerator2.Current.Equals(enumerator.Current))
                 {
                     flag = true;
                     break;
                 }
                 ToolboxItemFilterAttribute current = (ToolboxItemFilterAttribute) enumerator2.Current;
                 if (current.FilterType == ToolboxItemFilterType.Custom)
                 {
                     flag = true;
                     break;
                 }
             }
         }
         this._filter = list;
         this._filterDesigner = designer;
         this._toolboxUser = this._filterDesigner as IToolboxUser;
         if (this._toolboxUser == null)
         {
             IDesigner parent;
             for (ITreeDesigner designer2 = this._filterDesigner as ITreeDesigner; (this._toolboxUser == null) && (designer2 != null); designer2 = parent as ITreeDesigner)
             {
                 parent = designer2.Parent;
                 this._toolboxUser = parent as IToolboxUser;
             }
         }
         if ((this._toolboxUser == null) && (this._host.RootComponent != null))
         {
             this._toolboxUser = this._host.GetDesigner(this._host.RootComponent) as IToolboxUser;
         }
     }
     if (this._filter == null)
     {
         this._filter = new ArrayList();
     }
     return flag;
 }
Пример #27
0
 //if derived class supports it, we have to load the real toolboxitem
 protected virtual bool EvaluateCustomFilter(IToolboxUser user)
 {
     throw new InvalidOperationException("Custom filters are only supported with ToolboxItems.");
 }
Пример #28
0
        /// <summary>
        /// 窗体初始化
        /// </summary>
        void CustomInitialize()
        {
            #region 添加ToolBox

            string fileName = Application.StartupPath + @"\..\data\SharpDevelopControlLibrary.xml";
            ComponentLibraryLoader componentLibraryLoader = new ComponentLibraryLoader();
            componentLibraryLoader.LoadToolComponentLibrary(fileName);

            Guanjinke.Windows.Forms.ToolBox toolBox = new Guanjinke.Windows.Forms.ToolBox {
                Dock = DockStyle.Fill
            };

            foreach (Category category in componentLibraryLoader.Categories)
            {
                if (category.IsEnabled)
                {
                    Guanjinke.Windows.Forms.ToolBoxCategory cate = new Guanjinke.Windows.Forms.ToolBoxCategory();
                    cate.ImageIndex = -1;
                    cate.IsOpen     = false;
                    cate.Name       = category.Name;
                    cate.Parent     = null;

                    Guanjinke.Windows.Forms.ToolBoxItem item = new Guanjinke.Windows.Forms.ToolBoxItem();
                    item.Tag    = null;
                    item.Name   = "<Pointer>";
                    item.Parent = null;
                    cate.Items.Add(item);

                    foreach (ToolComponent component in category.ToolComponents)
                    {
                        item = new Guanjinke.Windows.Forms.ToolBoxItem();

                        System.Drawing.Design.ToolboxItem toolboxItem = new System.Drawing.Design.ToolboxItem();
                        toolboxItem.TypeName    = component.FullName;
                        toolboxItem.Bitmap      = componentLibraryLoader.GetIcon(component);
                        toolboxItem.DisplayName = component.Name;
                        Assembly asm = component.LoadAssembly();
                        toolboxItem.AssemblyName = asm.GetName();

                        item.Image  = toolboxItem.Bitmap;
                        item.Tag    = toolboxItem;
                        item.Name   = component.Name;
                        item.Parent = null;

                        cate.Items.Add(item);
                    }

                    toolBox.Categories.Add(cate);
                }
            }

            pnlToolBox.Controls.Add(toolBox);//左边panel添加控件

            #endregion

            #region 添加PropertyPad

            _propertyGrid = new PropertyGrid {
                Dock = DockStyle.Fill
            };
            pnlPropertyGrid.Controls.Add(_propertyGrid);//右边属性表

            #endregion


            #region 添加 "服务" 、 "设计器" 及 "Code"窗口

            ServiceContainer serviceContainer = new ServiceContainer();
            serviceContainer.AddService(typeof(System.ComponentModel.Design.IDesignerEventService), new DesignerEventService());
            serviceContainer.AddService(typeof(System.ComponentModel.Design.Serialization.INameCreationService), new NameCreationService());
            _toolboxService = new CustomToolboxService();
            serviceContainer.AddService(typeof(IToolboxService), _toolboxService);

            DesignSurface surface = new DesignSurface(serviceContainer);
            _host = (IDesignerHost)surface.GetService(typeof(IDesignerHost));

            serviceContainer.AddService(typeof(System.ComponentModel.Design.IEventBindingService), new ICSharpCode.FormsDesigner.Services.EventBindingService(surface));

            _menuCommandService = new MenuCommandService(surface);
            serviceContainer.AddService(typeof(IMenuCommandService), _menuCommandService);

            //surface.BeginLoad(typeof(Form));
            _CodeDomHostLoader = new Loader.CodeDomHostLoader();
            surface.BeginLoad(_CodeDomHostLoader);

            Control designerContorl = (Control)surface.View;


            designerContorl.BackColor = Color.Aqua;
            designerContorl.Dock      = DockStyle.Fill;
            //获取root组件
            rootComponent = (Form)((IDesignerHost)this._host).RootComponent;

            rootComponent.FormBorderStyle = FormBorderStyle.None;


            #region 初始化窗体大小

            //- set the Size
            PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(designerContorl);
            //- Sets a PropertyDescriptor to the specific property.
            PropertyDescriptor pdS = pdc.Find("Size", false);
            if (null != pdS)
            {
                pdS.SetValue(_host.RootComponent, new Size(800, 480));
            }
            #endregion

            tpDesign.Controls.Add(designerContorl);//窗体


            _textEditor = new TextEditorControl
            {
                IsReadOnly = true,
                Dock       = DockStyle.Fill,
                Document   = { HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("C#") }
            }; //代码编辑器
            tpCode.Controls.Add(_textEditor);

            #endregion

            _propertyGrid.SelectedObject = surface.ComponentContainer.Components[0];
            SetAlignMenuEnabled(false);


            _propertyGrid.Site = (new IDEContainer(_host)).CreateSite(_propertyGrid);
            _propertyGrid.PropertyTabs.AddTabType(typeof(System.Windows.Forms.Design.EventsTab), PropertyTabScope.Document);

            #region 事件响应

            // 选中项改变时的事件
            _selectionService = surface.GetService(typeof(ISelectionService)) as ISelectionService;//获取surface中的SelectionService
            _selectionService.SelectionChanged += new EventHandler(selectionService_SelectionChanged);

            // 增/删/重命名组件的事件
            IComponentChangeService componentChangeService = (IComponentChangeService)surface.GetService(typeof(IComponentChangeService));
            componentChangeService.ComponentAdded   += ComponentListChanged;
            componentChangeService.ComponentRemoved += ComponentListChanged;
            componentChangeService.ComponentRename  += ComponentListChanged;
            _host.TransactionClosed += new DesignerTransactionCloseEventHandler(TransactionClosed);

            // 选中不同的工具条项目
            toolBox.SelectedItemChanged += delegate(object sender, Guanjinke.Windows.Forms.ToolBoxItem newItem)
            {
                _toolboxService.SetSelectedToolboxItem(newItem.Tag as System.Drawing.Design.ToolboxItem);
            };

            // 双击工具栏项目时增加到设计器中
            toolBox.ItemDoubleClicked += delegate(object sender, Guanjinke.Windows.Forms.ToolBoxItem newItem)
            {
                System.Drawing.Design.ToolboxItem toolboxItem = newItem.Tag as System.Drawing.Design.ToolboxItem;
                if (null != toolboxItem)
                {
                    IToolboxUser toolboxUser = _host.GetDesigner(_host.RootComponent as IComponent) as IToolboxUser;
                    if (null != toolboxUser)
                    {
                        toolboxUser.ToolPicked(toolboxItem);
                    }
                }
            };

            // 拖动工具栏项目的支持代码
            toolBox.ItemDragStart += delegate(object sender, Guanjinke.Windows.Forms.ToolBoxItem newItem)
            {
                System.Drawing.Design.ToolboxItem toolboxItem = newItem.Tag as System.Drawing.Design.ToolboxItem;
                if (null != toolboxItem)
                {
                    DataObject dataObject = ((IToolboxService)_toolboxService).SerializeToolboxItem(toolboxItem) as DataObject;
                    toolBox.DoDragDrop(dataObject, DragDropEffects.Copy);
                }
            };

            _toolboxService.ResetToolboxItem += delegate
            {
                toolBox.ResetSelection();
            };


            #endregion

            tsmiDelete.ShortcutKeys    = Keys.Delete;
            tsmiSelectAll.ShortcutKeys = Keys.Control | Keys.A;

            cmbControls.Sorted    = true;
            cmbControls.DrawMode  = DrawMode.OwnerDrawFixed;
            cmbControls.DrawItem += new DrawItemEventHandler(cmbControls_DrawItem);
            UpdateComboBox();

            #region 串口加载

            foreach (string com in System.IO.Ports.SerialPort.GetPortNames())
            {
                this.comPortName.Items.Add(com);
            }
            comParity.SelectedIndex = 0;
            if (comPortName.Items.Count > 0)
            {
                comPortName.SelectedIndex = 0;
            }

            #endregion
        }
Пример #29
0
 protected override bool EvaluateCustomFilter(IToolboxUser user)
 {
     return(user.GetToolSupported(item));
 }