Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / WinForms / System / WinForms / Design / BaseContextMenu.cs / 1 / BaseContextMenu.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- /* */ namespace System.Windows.Forms.Design { using System.Design; using System.Runtime.InteropServices; using System.ComponentModel; using System.Diagnostics; using System; using System.Security; using System.Security.Permissions; using System.Collections; using System.ComponentModel.Design; using System.Windows.Forms; using System.Drawing; using System.Drawing.Design; using System.Windows.Forms.Design; using System.Windows.Forms.Design.Behavior; ////// This class is going to replace the shell contextMenu and uses the ContextMenuStrip. /// The ContextMenuStrip contains groups and groupOrder which it uses to add items to itself. /// ControlDesigners can add custom items to the contextMenu, using the new member to the /// group and add the groupOrder to the ContextMenu. /// internal class BaseContextMenuStrip : GroupedContextMenuStrip { private IServiceProvider serviceProvider; private Component component; private ToolStripMenuItem selectionMenuItem; ////// Constructor. /// public BaseContextMenuStrip(IServiceProvider provider, Component component) : base() { this.serviceProvider = provider; this.component = component; // Now initialiaze the contextMenu InitializeContextMenu(); } ////// Helper function to add the "View Code" menuItem. /// private void AddCodeMenuItem() { StandardCommandToolStripMenuItem codeMenuItem = new StandardCommandToolStripMenuItem(StandardCommands.ViewCode, SR.GetString(SR.ContextMenuViewCode), "viewcode", serviceProvider); this.Groups[StandardGroups.Code].Items.Add(codeMenuItem); } ////// Helper function to add the "SendToBack/BringToFront" menuItem. /// private void AddZorderMenuItem() { StandardCommandToolStripMenuItem ZOrderMenuItem = new StandardCommandToolStripMenuItem(MenuCommands.BringToFront, SR.GetString(SR.ContextMenuBringToFront), "bringToFront", serviceProvider); this.Groups[StandardGroups.ZORder].Items.Add(ZOrderMenuItem); ZOrderMenuItem = new StandardCommandToolStripMenuItem(MenuCommands.SendToBack, SR.GetString(SR.ContextMenuSendToBack), "sendToBack", serviceProvider); this.Groups[StandardGroups.ZORder].Items.Add(ZOrderMenuItem); } ////// Helper function to add the "Alignment" menuItem. /// private void AddGridMenuItem() { StandardCommandToolStripMenuItem gridMenuItem = new StandardCommandToolStripMenuItem(MenuCommands.AlignToGrid, SR.GetString(SR.ContextMenuAlignToGrid), "alignToGrid", serviceProvider); this.Groups[StandardGroups.Grid].Items.Add(gridMenuItem); } ////// Helper function to add the "Locked" menuItem. /// private void AddLockMenuItem() { StandardCommandToolStripMenuItem lockMenuItem = new StandardCommandToolStripMenuItem(MenuCommands.LockControls, SR.GetString(SR.ContextMenuLockControls), "lockControls", serviceProvider); this.Groups[StandardGroups.Lock].Items.Add(lockMenuItem); } ////// Helper function to add the Select Parent menuItem. /// private void RefreshSelectionMenuItem() { int index = -1; if (selectionMenuItem != null) { index = this.Items.IndexOf(selectionMenuItem); this.Groups[StandardGroups.Selection].Items.Remove(selectionMenuItem); this.Items.Remove(selectionMenuItem); } ArrayList parentControls = new ArrayList(); int nParentControls = 0; // // Get the currently selected Control ISelectionService selectionService = serviceProvider.GetService(typeof(ISelectionService)) as ISelectionService; IDesignerHost host = serviceProvider.GetService(typeof(IDesignerHost)) as IDesignerHost; if (selectionService != null && host != null) { IComponent root = host.RootComponent; Debug.Assert(root != null, "Null root component. Will be unable to build selection menu"); Control selectedControl = selectionService.PrimarySelection as Control; if (selectedControl != null && root != null && selectedControl != root) { Control parentControl = selectedControl.Parent; while (parentControl != null) { if (parentControl.Site != null) { parentControls.Add(parentControl); nParentControls++; } if (parentControl == root) { break; } parentControl = parentControl.Parent; } } else if (selectionService.PrimarySelection is ToolStripItem) { ToolStripItem selectedItem = selectionService.PrimarySelection as ToolStripItem; ToolStripItemDesigner itemDesigner = host.GetDesigner(selectedItem) as ToolStripItemDesigner; if (itemDesigner != null) { parentControls = itemDesigner.AddParentTree(); nParentControls = parentControls.Count; } } } if (nParentControls > 0) { selectionMenuItem = new ToolStripMenuItem(); IUIService uis = serviceProvider.GetService(typeof(IUIService)) as IUIService; if (uis != null) { selectionMenuItem.DropDown.Renderer = (ToolStripProfessionalRenderer)uis.Styles["VsRenderer"]; //Set the right Font selectionMenuItem.DropDown.Font = (Font)uis.Styles["DialogFont"]; } selectionMenuItem.Text = SR.GetString(SR.ContextMenuSelect); foreach (Component parent in parentControls) { ToolStripMenuItem selectListItem = new SelectToolStripMenuItem(parent, serviceProvider); selectionMenuItem.DropDownItems.Add(selectListItem); } this.Groups[StandardGroups.Selection].Items.Add(selectionMenuItem); // Re add the newly refreshed item.. if (index != -1) { this.Items.Insert(index, selectionMenuItem); } } } ////// Helper function to add the Verbs. /// private void AddVerbMenuItem() { //Add Designer Verbs.. IMenuCommandService menuCommandService = (IMenuCommandService)serviceProvider.GetService(typeof(IMenuCommandService)); if (menuCommandService != null) { DesignerVerbCollection verbCollection = menuCommandService.Verbs; foreach (DesignerVerb verb in verbCollection) { DesignerVerbToolStripMenuItem verbItem = new DesignerVerbToolStripMenuItem(verb); this.Groups[StandardGroups.Verbs].Items.Add(verbItem); } } } ////// Helper function to add the "Cut/Copy/Paste/Delete" menuItem. /// private void AddEditMenuItem() { StandardCommandToolStripMenuItem stdMenuItem = new StandardCommandToolStripMenuItem(StandardCommands.Cut, SR.GetString(SR.ContextMenuCut), "cut", serviceProvider); this.Groups[StandardGroups.Edit].Items.Add(stdMenuItem); stdMenuItem = new StandardCommandToolStripMenuItem(StandardCommands.Copy, SR.GetString(SR.ContextMenuCopy), "copy", serviceProvider); this.Groups[StandardGroups.Edit].Items.Add(stdMenuItem); stdMenuItem = new StandardCommandToolStripMenuItem(StandardCommands.Paste, SR.GetString(SR.ContextMenuPaste), "paste", serviceProvider); this.Groups[StandardGroups.Edit].Items.Add(stdMenuItem); stdMenuItem = new StandardCommandToolStripMenuItem(StandardCommands.Delete, SR.GetString(SR.ContextMenuDelete), "delete", serviceProvider); this.Groups[StandardGroups.Edit].Items.Add(stdMenuItem); } ////// Helper function to add the "Properties" menuItem. /// private void AddPropertiesMenuItem() { StandardCommandToolStripMenuItem stdMenuItem = new StandardCommandToolStripMenuItem(StandardCommands.DocumentOutline, SR.GetString(SR.ContextMenuDocumentOutline), "", serviceProvider); this.Groups[StandardGroups.Properties].Items.Add(stdMenuItem); stdMenuItem = new StandardCommandToolStripMenuItem(MenuCommands.DesignerProperties, SR.GetString(SR.ContextMenuProperties), "properties", serviceProvider); this.Groups[StandardGroups.Properties].Items.Add(stdMenuItem); } ////// Basic Initialize method. /// private void InitializeContextMenu() { //this.Opening += new CancelEventHandler(OnContextMenuOpening); this.Name = "designerContextMenuStrip"; IUIService uis = serviceProvider.GetService(typeof(IUIService)) as IUIService; if (uis != null) { this.Renderer = (ToolStripProfessionalRenderer)uis.Styles["VsRenderer"]; } GroupOrdering.AddRange(new string[] { StandardGroups.Code, StandardGroups.ZORder, StandardGroups.Grid, StandardGroups.Lock, StandardGroups.Verbs, StandardGroups.Custom, StandardGroups.Selection, StandardGroups.Edit, StandardGroups.Properties}); // ADD MENUITEMS AddCodeMenuItem(); AddZorderMenuItem(); AddGridMenuItem(); AddLockMenuItem(); AddVerbMenuItem(); RefreshSelectionMenuItem(); AddEditMenuItem(); AddPropertiesMenuItem(); } ////// Public function that allows the individual MenuItems to get refreshed each time the ContextMenu is opened. /// public override void RefreshItems() { //Set the right Font IUIService uis = serviceProvider.GetService(typeof(IUIService)) as IUIService; if (uis != null) { this.Font = (Font)uis.Styles["DialogFont"]; } foreach (ToolStripItem item in this.Items) { StandardCommandToolStripMenuItem stdItem = item as StandardCommandToolStripMenuItem; if (stdItem != null) { stdItem.RefreshItem(); } } RefreshSelectionMenuItem(); } ////// A ToolStripMenuItem that gets added for the "Select" menuitem. /// private class SelectToolStripMenuItem : ToolStripMenuItem { private Component comp; private IServiceProvider serviceProvider; private Type _itemType; private bool _cachedImage = false; private Image _image = null; private static string systemWindowsFormsNamespace = typeof(System.Windows.Forms.ToolStripItem).Namespace; public SelectToolStripMenuItem(Component c, IServiceProvider provider) { comp = c; serviceProvider = provider; // Get NestedSiteName... string compName = null; if (comp != null) { ISite site = comp.Site; if (site != null) { INestedSite nestedSite = site as INestedSite; if (nestedSite != null && !string.IsNullOrEmpty(nestedSite.FullName)) { compName = nestedSite.FullName; } else if (!string.IsNullOrEmpty(site.Name)) { compName = site.Name; } } } this.Text = SR.GetString(SR.ToolStripSelectMenuItem, compName); this._itemType = c.GetType(); } public override Image Image { get { // Defer loading the image until we're sure we need it if (!_cachedImage) { _cachedImage = true; // give the toolbox item attribute the first shot ToolboxItem tbxItem = ToolboxService.GetToolboxItem(_itemType); if (tbxItem != null) { _image = tbxItem.Bitmap; } else { // else attempt to get the resource from a known place in the manifest // if and only if the namespace of the type is System.Windows.Forms. // else attempt to get the resource from a known place in the manifest if (_itemType.Namespace == systemWindowsFormsNamespace) { _image = ToolboxBitmapAttribute.GetImageFromResource(_itemType, null, false); } } // if all else fails, throw up a default image. if (_image == null) { _image = ToolboxBitmapAttribute.GetImageFromResource(comp.GetType(), null, false); } } return _image; } set { _image = value; _cachedImage = true; } } ////// Items OnClick event, to select the Parent Control. /// protected override void OnClick(System.EventArgs e) { ISelectionService selectionService = serviceProvider.GetService(typeof(ISelectionService)) as ISelectionService; if (selectionService != null) { selectionService.SetSelectedComponents(new object[] { comp }, SelectionTypes.Replace); } } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- VScrollBar.cs
- AppDomainFactory.cs
- diagnosticsswitches.cs
- WebBrowsableAttribute.cs
- XDRSchema.cs
- SerializationObjectManager.cs
- LabelInfo.cs
- ImageListStreamer.cs
- WsatAdminException.cs
- PackageRelationship.cs
- MethodAccessException.cs
- NotFiniteNumberException.cs
- UriExt.cs
- MappingModelBuildProvider.cs
- _HeaderInfo.cs
- SqlDataReaderSmi.cs
- LocalFileSettingsProvider.cs
- GridViewSortEventArgs.cs
- NullableLongMinMaxAggregationOperator.cs
- TableItemStyle.cs
- SqlConnectionStringBuilder.cs
- RenameRuleObjectDialog.cs
- NativeMethods.cs
- HealthMonitoringSectionHelper.cs
- CssStyleCollection.cs
- RelationshipEnd.cs
- PasswordBoxAutomationPeer.cs
- DesignTimeSiteMapProvider.cs
- Help.cs
- ClassImporter.cs
- WebControlsSection.cs
- Model3D.cs
- HebrewCalendar.cs
- MarshalDirectiveException.cs
- FormViewCommandEventArgs.cs
- RichTextBoxAutomationPeer.cs
- VScrollProperties.cs
- TextTrailingWordEllipsis.cs
- XmlByteStreamReader.cs
- OperationValidationEventArgs.cs
- HttpWriter.cs
- CacheOutputQuery.cs
- TextEditorTables.cs
- RegexFCD.cs
- InheritanceContextHelper.cs
- TextServicesLoader.cs
- EntityContainerAssociationSet.cs
- PermissionSet.cs
- Errors.cs
- SelectionWordBreaker.cs
- InlineUIContainer.cs
- RelOps.cs
- SharedHttpsTransportManager.cs
- AnnotationMap.cs
- DefinitionProperties.cs
- DataObjectFieldAttribute.cs
- UriScheme.cs
- BaseValidator.cs
- TdsParserSafeHandles.cs
- ReceiveParametersContent.cs
- SingleObjectCollection.cs
- HitTestParameters.cs
- FixedTextView.cs
- DataGridRowHeader.cs
- MLangCodePageEncoding.cs
- NamespaceTable.cs
- ExpandedProjectionNode.cs
- RootBuilder.cs
- FontDialog.cs
- WsatServiceAddress.cs
- NegatedConstant.cs
- PinnedBufferMemoryStream.cs
- MailFileEditor.cs
- EntityCommand.cs
- FlowNode.cs
- LineSegment.cs
- InheritablePropertyChangeInfo.cs
- PassportIdentity.cs
- DataServicePagingProviderWrapper.cs
- TableParaClient.cs
- XmlJsonWriter.cs
- DataGridViewRowConverter.cs
- ClientScriptManagerWrapper.cs
- WindowsStatusBar.cs
- ChannelBinding.cs
- XmlSerializerNamespaces.cs
- CompilationUnit.cs
- ValueSerializerAttribute.cs
- SortedSet.cs
- ConfigurationValidatorBase.cs
- WebPartTransformerAttribute.cs
- QilLiteral.cs
- PerfCounterSection.cs
- WebHttpBindingElement.cs
- Attributes.cs
- FileDialogCustomPlace.cs
- Object.cs
- GeometryModel3D.cs
- GlyphManager.cs
- DataGridViewButtonColumn.cs