예제 #1
0
 private void FloatWindow_KeyUp(object sender, KeyEventArgs e)
 {
     if (ToolStripManager.IsShortcutDefined(e.KeyData))
     {
         MessageBox.Show("Shortcut entered: " + e.KeyData.ToString());
     }
 }
        /// <summary>
        ///  Here is where all the fun stuff starts.  We create the structure and apply the naming here.
        /// </summary>
        private void CreateStandardMenuStrip(IDesignerHost host, MenuStrip tool)
        {
            // build the static menu items structure.
            string[][] menuItemNames = new string[][]
            {
                new string[] { SR.StandardMenuFile, SR.StandardMenuNew, SR.StandardMenuOpen, "-", SR.StandardMenuSave, SR.StandardMenuSaveAs, "-", SR.StandardMenuPrint, SR.StandardMenuPrintPreview, "-", SR.StandardMenuExit },
                new string[] { SR.StandardMenuEdit, SR.StandardMenuUndo, SR.StandardMenuRedo, "-", SR.StandardMenuCut, SR.StandardMenuCopy, SR.StandardMenuPaste, "-", SR.StandardMenuSelectAll },
                new string[] { SR.StandardMenuTools, SR.StandardMenuCustomize, SR.StandardMenuOptions },
                new string[] { SR.StandardMenuHelp, SR.StandardMenuContents, SR.StandardMenuIndex, SR.StandardMenuSearch, "-", SR.StandardMenuAbout }
            };

            // build the static menu items image list that maps one-one with above menuItems structure. this is required so that the in LOCALIZED build we dont use the Localized item string.
            string[][] menuItemImageNames = new string[][]
            {
                new string[] { "", "new", "open", "-", "save", "", "-", "print", "printPreview", "-", "" },
                new string[] { "", "", "", "-", "cut", "copy", "paste", "-", "" },
                new string[] { "", "", "" },
                new string[] { "", "", "", "", "-", "" }
            };

            Keys[][] menuItemShortcuts = new Keys[][]
            {
                new Keys[] { /*File*/ Keys.None, /*New*/ Keys.Control | Keys.N, /*Open*/ Keys.Control | Keys.O, /*Separator*/ Keys.None, /*Save*/ Keys.Control | Keys.S, /*SaveAs*/ Keys.None, Keys.None, /*Print*/ Keys.Control | Keys.P, /*PrintPreview*/ Keys.None, /*Separator*/ Keys.None, /*Exit*/ Keys.None },
                new Keys[] { /*Edit*/ Keys.None, /*Undo*/ Keys.Control | Keys.Z, /*Redo*/ Keys.Control | Keys.Y, /*Separator*/ Keys.None, /*Cut*/ Keys.Control | Keys.X, /*Copy*/ Keys.Control | Keys.C, /*Paste*/ Keys.Control | Keys.V, /*Separator*/ Keys.None, /*SelectAll*/ Keys.None },
                new Keys[] { /*Tools*/ Keys.None, /*Customize*/ Keys.None, /*Options*/ Keys.None },
                new Keys[] { /*Help*/ Keys.None, /*Contents*/ Keys.None, /*Index*/ Keys.None, /*Search*/ Keys.None, /*Separator*/ Keys.None, /*About*/ Keys.None }
            };

            Debug.Assert(host != null, "can't create standard menu without designer _host.");
            if (host is null)
            {
                return;
            }

            tool.SuspendLayout();
            ToolStripDesigner.s_autoAddNewItems = false;
            // create a transaction so this happens as an atomic unit.
            DesignerTransaction createMenu = _host.CreateTransaction(SR.StandardMenuCreateDesc);

            try
            {
                INameCreationService nameCreationService = (INameCreationService)_provider.GetService(typeof(INameCreationService));
                string defaultName = "standardMainMenuStrip";
                string name        = defaultName;
                int    index       = 1;

                if (host != null)
                {
                    while (_host.Container.Components[name] != null)
                    {
                        name = defaultName + (index++).ToString(CultureInfo.InvariantCulture);
                    }
                }

                // now build the menu items themselves.
                for (int j = 0; j < menuItemNames.Length; j++)
                {
                    string[]          menuArray = menuItemNames[j];
                    ToolStripMenuItem rootItem  = null;
                    for (int i = 0; i < menuArray.Length; i++)
                    {
                        name = null;
                        // for separators, just use the default name.  Otherwise, remove any non-characters and  get the name from the text.
                        string itemText = menuArray[i];
                        name = NameFromText(itemText, typeof(ToolStripMenuItem), nameCreationService, true);
                        ToolStripItem item = null;
                        if (name.Contains("Separator"))
                        {
                            // create the componennt.
                            item = (ToolStripSeparator)_host.CreateComponent(typeof(ToolStripSeparator), name);
                            IDesigner designer = _host.GetDesigner(item);
                            if (designer is ComponentDesigner)
                            {
                                ((ComponentDesigner)designer).InitializeNewComponent(null);
                            }

                            item.Text = itemText;
                        }
                        else
                        {
                            // create the componennt.
                            item = (ToolStripMenuItem)_host.CreateComponent(typeof(ToolStripMenuItem), name);
                            IDesigner designer = _host.GetDesigner(item);
                            if (designer is ComponentDesigner)
                            {
                                ((ComponentDesigner)designer).InitializeNewComponent(null);
                            }

                            item.Text = itemText;
                            Keys shortcut = menuItemShortcuts[j][i];
                            if ((item is ToolStripMenuItem) && shortcut != Keys.None)
                            {
                                if (!ToolStripManager.IsShortcutDefined(shortcut) && ToolStripManager.IsValidShortcut(shortcut))
                                {
                                    ((ToolStripMenuItem)item).ShortcutKeys = shortcut;
                                }
                            }

                            Bitmap image = null;
                            try
                            {
                                image = GetImage(menuItemImageNames[j][i]);
                            }
                            catch
                            {
                                // eat the exception.. as you may not find image for all MenuItems.
                            }

                            if (image != null)
                            {
                                PropertyDescriptor imageProperty = TypeDescriptor.GetProperties(item)["Image"];
                                Debug.Assert(imageProperty != null, "Could not find 'Image' property in ToolStripItem.");
                                if (imageProperty != null)
                                {
                                    imageProperty.SetValue(item, image);
                                }

                                item.ImageTransparentColor = Color.Magenta;
                            }
                        }

                        // the first item in each array is the root item.
                        if (i == 0)
                        {
                            rootItem = (ToolStripMenuItem)item;
                            rootItem.DropDown.SuspendLayout();
                        }
                        else
                        {
                            rootItem.DropDownItems.Add(item);
                        }

                        //If Last SubItem Added the Raise the Events
                        if (i == menuArray.Length - 1)
                        {
                            // member is OK to be null...
                            MemberDescriptor member = TypeDescriptor.GetProperties(rootItem)["DropDownItems"];
                            _componentChangeSvc.OnComponentChanging(rootItem, member);
                            _componentChangeSvc.OnComponentChanged(rootItem, member, null, null);
                        }
                    }

                    // finally, add it to the MainMenu.
                    rootItem.DropDown.ResumeLayout(false);
                    tool.Items.Add(rootItem);
                    //If Last SubItem Added the Raise the Events
                    if (j == menuItemNames.Length - 1)
                    {
                        // member is OK to be null...
                        MemberDescriptor topMember = TypeDescriptor.GetProperties(tool)["Items"];
                        _componentChangeSvc.OnComponentChanging(tool, topMember);
                        _componentChangeSvc.OnComponentChanged(tool, topMember, null, null);
                    }
                }
            }
            catch (Exception e)
            {
                if (e is InvalidOperationException)
                {
                    IUIService uiService = (IUIService)_provider.GetService(typeof(IUIService));
                    uiService.ShowError(e.Message);
                }

                if (createMenu != null)
                {
                    createMenu.Cancel();
                    createMenu = null;
                }
            }
            finally
            {
                ToolStripDesigner.s_autoAddNewItems = true;
                if (createMenu != null)
                {
                    createMenu.Commit();
                    createMenu = null;
                }

                tool.ResumeLayout();
                // Select the Main Menu...
                ISelectionService selSvc = (ISelectionService)_provider.GetService(typeof(ISelectionService));
                if (selSvc != null)
                {
                    selSvc.SetSelectedComponents(new object[] { _designer.Component });
                }

                //Refresh the Glyph
                DesignerActionUIService actionUIService = (DesignerActionUIService)_provider.GetService(typeof(DesignerActionUIService));
                if (actionUIService != null)
                {
                    actionUIService.Refresh(_designer.Component);
                }

                // this will invalidate the Selection Glyphs.
                SelectionManager selMgr = (SelectionManager)_provider.GetService(typeof(SelectionManager));
                selMgr.Refresh();
            }
        }
 private void CreateStandardMenuStrip(IDesignerHost host, MenuStrip tool)
 {
     string[][] strArray   = new string[][] { new string[] { System.Design.SR.GetString("StandardMenuFile"), System.Design.SR.GetString("StandardMenuNew"), System.Design.SR.GetString("StandardMenuOpen"), "-", System.Design.SR.GetString("StandardMenuSave"), System.Design.SR.GetString("StandardMenuSaveAs"), "-", System.Design.SR.GetString("StandardMenuPrint"), System.Design.SR.GetString("StandardMenuPrintPreview"), "-", System.Design.SR.GetString("StandardMenuExit") }, new string[] { System.Design.SR.GetString("StandardMenuEdit"), System.Design.SR.GetString("StandardMenuUndo"), System.Design.SR.GetString("StandardMenuRedo"), "-", System.Design.SR.GetString("StandardMenuCut"), System.Design.SR.GetString("StandardMenuCopy"), System.Design.SR.GetString("StandardMenuPaste"), "-", System.Design.SR.GetString("StandardMenuSelectAll") }, new string[] { System.Design.SR.GetString("StandardMenuTools"), System.Design.SR.GetString("StandardMenuCustomize"), System.Design.SR.GetString("StandardMenuOptions") }, new string[] { System.Design.SR.GetString("StandardMenuHelp"), System.Design.SR.GetString("StandardMenuContents"), System.Design.SR.GetString("StandardMenuIndex"), System.Design.SR.GetString("StandardMenuSearch"), "-", System.Design.SR.GetString("StandardMenuAbout") } };
     string[][] strArray2  = new string[][] { new string[] { "", "new", "open", "-", "save", "", "-", "print", "printPreview", "-", "" }, new string[] { "", "", "", "-", "cut", "copy", "paste", "-", "" }, new string[] { "", "", "" }, new string[] { "", "", "", "", "-", "" } };
     Keys[][]   keysArray2 = new Keys[4][];
     Keys[]     keysArray3 = new Keys[11];
     keysArray3[1] = Keys.Control | Keys.N;
     keysArray3[2] = Keys.Control | Keys.O;
     keysArray3[4] = Keys.Control | Keys.S;
     keysArray3[7] = Keys.Control | Keys.P;
     keysArray2[0] = keysArray3;
     Keys[] keysArray4 = new Keys[9];
     keysArray4[1] = Keys.Control | Keys.Z;
     keysArray4[2] = Keys.Control | Keys.Y;
     keysArray4[4] = Keys.Control | Keys.X;
     keysArray4[5] = Keys.Control | Keys.C;
     keysArray4[6] = Keys.Control | Keys.V;
     keysArray2[1] = keysArray4;
     keysArray2[2] = new Keys[3];
     keysArray2[3] = new Keys[6];
     Keys[][] keysArray = keysArray2;
     if (host != null)
     {
         tool.SuspendLayout();
         ToolStripDesigner._autoAddNewItems = false;
         DesignerTransaction transaction = this._host.CreateTransaction(System.Design.SR.GetString("StandardMenuCreateDesc"));
         try
         {
             INameCreationService nameCreationService = (INameCreationService)this._provider.GetService(typeof(INameCreationService));
             string str  = "standardMainMenuStrip";
             string name = str;
             int    num  = 1;
             if (host != null)
             {
                 while (this._host.Container.Components[name] != null)
                 {
                     name = str + num++.ToString(CultureInfo.InvariantCulture);
                 }
             }
             for (int i = 0; i < strArray.Length; i++)
             {
                 string[]          strArray3 = strArray[i];
                 ToolStripMenuItem component = null;
                 for (int j = 0; j < strArray3.Length; j++)
                 {
                     name = null;
                     string text = strArray3[j];
                     name = this.NameFromText(text, typeof(ToolStripMenuItem), nameCreationService, true);
                     ToolStripItem item2 = null;
                     if (name.Contains("Separator"))
                     {
                         item2 = (ToolStripSeparator)this._host.CreateComponent(typeof(ToolStripSeparator), name);
                         IDesigner designer = this._host.GetDesigner(item2);
                         if (designer is ComponentDesigner)
                         {
                             ((ComponentDesigner)designer).InitializeNewComponent(null);
                         }
                         item2.Text = text;
                     }
                     else
                     {
                         item2 = (ToolStripMenuItem)this._host.CreateComponent(typeof(ToolStripMenuItem), name);
                         IDesigner designer2 = this._host.GetDesigner(item2);
                         if (designer2 is ComponentDesigner)
                         {
                             ((ComponentDesigner)designer2).InitializeNewComponent(null);
                         }
                         item2.Text = text;
                         Keys shortcut = keysArray[i][j];
                         if (((item2 is ToolStripMenuItem) && (shortcut != Keys.None)) && (!ToolStripManager.IsShortcutDefined(shortcut) && ToolStripManager.IsValidShortcut(shortcut)))
                         {
                             ((ToolStripMenuItem)item2).ShortcutKeys = shortcut;
                         }
                         Bitmap image = null;
                         try
                         {
                             image = this.GetImage(strArray2[i][j]);
                         }
                         catch
                         {
                         }
                         if (image != null)
                         {
                             PropertyDescriptor descriptor = TypeDescriptor.GetProperties(item2)["Image"];
                             if (descriptor != null)
                             {
                                 descriptor.SetValue(item2, image);
                             }
                             item2.ImageTransparentColor = Color.Magenta;
                         }
                     }
                     if (j == 0)
                     {
                         component = (ToolStripMenuItem)item2;
                         component.DropDown.SuspendLayout();
                     }
                     else
                     {
                         component.DropDownItems.Add(item2);
                     }
                     if (j == (strArray3.Length - 1))
                     {
                         MemberDescriptor member = TypeDescriptor.GetProperties(component)["DropDownItems"];
                         this.componentChangeSvc.OnComponentChanging(component, member);
                         this.componentChangeSvc.OnComponentChanged(component, member, null, null);
                     }
                 }
                 component.DropDown.ResumeLayout(false);
                 tool.Items.Add(component);
                 if (i == (strArray.Length - 1))
                 {
                     MemberDescriptor descriptor3 = TypeDescriptor.GetProperties(tool)["Items"];
                     this.componentChangeSvc.OnComponentChanging(tool, descriptor3);
                     this.componentChangeSvc.OnComponentChanged(tool, descriptor3, null, null);
                 }
             }
         }
         catch (Exception exception)
         {
             if (exception is InvalidOperationException)
             {
                 ((IUIService)this._provider.GetService(typeof(IUIService))).ShowError(exception.Message);
             }
             if (transaction != null)
             {
                 transaction.Cancel();
                 transaction = null;
             }
         }
         finally
         {
             ToolStripDesigner._autoAddNewItems = true;
             if (transaction != null)
             {
                 transaction.Commit();
                 transaction = null;
             }
             tool.ResumeLayout();
             ISelectionService service = (ISelectionService)this._provider.GetService(typeof(ISelectionService));
             if (service != null)
             {
                 service.SetSelectedComponents(new object[] { this._designer.Component });
             }
             DesignerActionUIService service4 = (DesignerActionUIService)this._provider.GetService(typeof(DesignerActionUIService));
             if (service4 != null)
             {
                 service4.Refresh(this._designer.Component);
             }
             ((SelectionManager)this._provider.GetService(typeof(SelectionManager))).Refresh();
         }
     }
 }