Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / WinForms / Managed / System / WinForms / MDIControlStrip.cs / 1305376 / MDIControlStrip.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Windows.Forms { using System; using System.Security; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Diagnostics; using System.Runtime.InteropServices; using System.Runtime.Versioning; ///this is the toolstrip used for merging the [:)] [_][#][X] buttons onto an /// mdi parent when an MDI child is maximized. /// internal class MdiControlStrip : MenuStrip { private ToolStripMenuItem system; private ToolStripMenuItem close; private ToolStripMenuItem minimize; private ToolStripMenuItem restore; private MenuStrip mergedMenu; private IWin32Window target; ///target is ideally the MDI Child to send the system commands to. /// although there's nothing MDI child specific to it... you could have this /// a toplevel window. /// public MdiControlStrip(IWin32Window target) { IntPtr hMenu= UnsafeNativeMethods.GetSystemMenu(new HandleRef(this, Control.GetSafeHandle(target)), /*bRevert=*/false); this.target = target; // The menu item itself takes care of enabledness and sending WM_SYSCOMMAND messages to the target. minimize = new ControlBoxMenuItem(hMenu, NativeMethods.SC_MINIMIZE, target); close = new ControlBoxMenuItem(hMenu, NativeMethods.SC_CLOSE, target); restore = new ControlBoxMenuItem(hMenu, NativeMethods.SC_RESTORE, target); // The dropDown of the system menu is the one that talks to native. system = new SystemMenuItem(); // However in the event that the target handle changes we have to push the new handle into everyone. Control controlTarget = target as Control; if (controlTarget != null) { controlTarget.HandleCreated += new EventHandler(OnTargetWindowHandleRecreated); controlTarget.Disposed += new EventHandler(OnTargetWindowDisposed); } // add in opposite order to how you want it merged this.Items.AddRange(new ToolStripItem[] { minimize, restore,close, system }); this.SuspendLayout(); foreach (ToolStripItem item in this.Items) { item.DisplayStyle = ToolStripItemDisplayStyle.Image; item.MergeIndex = 0; item.MergeAction = MergeAction.Insert; item.Overflow = ToolStripItemOverflow.Never; item.Alignment = ToolStripItemAlignment.Right; item.Padding = Padding.Empty; } // set up the sytem menu system.Image = GetTargetWindowIcon(); system.Alignment = ToolStripItemAlignment.Left; system.DropDownOpening += new EventHandler(OnSystemMenuDropDownOpening); system.ImageScaling = ToolStripItemImageScaling.None; system.DoubleClickEnabled = true; system.DoubleClick += new EventHandler(OnSystemMenuDoubleClick); system.Padding = Padding.Empty; system.ShortcutKeys = Keys.Alt | Keys.OemMinus; this.ResumeLayout(false); } #region Buttons /* Unused public ToolStripMenuItem System { get { return system; } } */ public ToolStripMenuItem Close { get { return close; } } /* Unused public ToolStripMenuItem Minimize { get { return minimize; } } public ToolStripMenuItem Restore { get { return restore; } } */ #endregion internal MenuStrip MergedMenu { get { return mergedMenu; } set { mergedMenu = value; } } /* PERF: consider shutting off layout #region ShutOffLayout protected override void OnLayout(LayoutEventArgs e) { return; // if someone attempts } protected override Size GetPreferredSize(Size proposedSize) { return Size.Empty; } #endregion */ [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2106:SecureAsserts")] [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] private Image GetTargetWindowIcon() { Image systemIcon = null; IntPtr hIcon = UnsafeNativeMethods.SendMessage(new HandleRef(this, Control.GetSafeHandle(target)), NativeMethods.WM_GETICON, NativeMethods.ICON_SMALL, 0); IntSecurity.ObjectFromWin32Handle.Assert(); try { Icon icon = (hIcon != IntPtr.Zero) ? Icon.FromHandle(hIcon) : Form.DefaultIcon; Icon smallIcon = new Icon(icon, SystemInformation.SmallIconSize); systemIcon = smallIcon.ToBitmap(); smallIcon.Dispose(); } finally { CodeAccessPermission.RevertAssert(); } return systemIcon; } protected internal override void OnItemAdded(ToolStripItemEventArgs e) { base.OnItemAdded(e); Debug.Assert(Items.Count <= 4, "Too many items in the MDIControlStrip. How did we get into this situation?"); } private void OnTargetWindowDisposed(object sender, EventArgs e) { UnhookTarget(); target = null; } private void OnTargetWindowHandleRecreated(object sender, EventArgs e) { // in the case that the handle for the form is recreated we need to set // up the handles to point to the new window handle for the form. system.SetNativeTargetWindow(target); minimize.SetNativeTargetWindow(target); close.SetNativeTargetWindow(target); restore.SetNativeTargetWindow(target); IntPtr hMenu= UnsafeNativeMethods.GetSystemMenu(new HandleRef(this, Control.GetSafeHandle(target)), /*bRevert=*/false); system.SetNativeTargetMenu(hMenu); minimize.SetNativeTargetMenu(hMenu); close.SetNativeTargetMenu(hMenu); restore.SetNativeTargetMenu(hMenu); // clear off the System DropDown. if (system.HasDropDownItems) { // next time we need one we'll just fetch it fresh. system.DropDown.Items.Clear(); system.DropDown.Dispose(); } system.Image = GetTargetWindowIcon(); } private void OnSystemMenuDropDownOpening(object sender, EventArgs e) { if (!system.HasDropDownItems && (target != null)) { system.DropDown = ToolStripDropDownMenu.FromHMenu(UnsafeNativeMethods.GetSystemMenu(new HandleRef(this, Control.GetSafeHandle(target)), /*bRevert=*/false), target); } else if (MergedMenu == null) { system.DropDown.Dispose(); } } private void OnSystemMenuDoubleClick(object sender, EventArgs e) { Close.PerformClick(); } protected override void Dispose(bool disposing) { if (disposing) { UnhookTarget(); target = null; } base.Dispose(disposing); } private void UnhookTarget() { if (target != null) { Control controlTarget = target as Control; if (controlTarget != null) { controlTarget.HandleCreated -= new EventHandler(OnTargetWindowHandleRecreated); controlTarget.Disposed -= new EventHandler(OnTargetWindowDisposed); } target = null; } } // when the system menu item shortcut is evaluated - pop the dropdown internal class ControlBoxMenuItem : ToolStripMenuItem { internal ControlBoxMenuItem(IntPtr hMenu, int nativeMenuCommandId, IWin32Window targetWindow) : base(hMenu, nativeMenuCommandId, targetWindow) { } internal override bool CanKeyboardSelect { get { return false; } } } // when the system menu item shortcut is evaluated - pop the dropdown internal class SystemMenuItem : ToolStripMenuItem { public SystemMenuItem(){ } protected internal override bool ProcessCmdKey(ref Message m, Keys keyData) { if (Visible && ShortcutKeys == keyData) { ShowDropDown(); this.DropDown.SelectNextToolStripItem(null, true); return true; } return base.ProcessCmdKey(ref m, keyData); } protected override void OnOwnerChanged(EventArgs e) { if (HasDropDownItems && DropDown.Visible) { HideDropDown(); } base.OnOwnerChanged(e); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Windows.Forms { using System; using System.Security; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Diagnostics; using System.Runtime.InteropServices; using System.Runtime.Versioning; ///this is the toolstrip used for merging the [:)] [_][#][X] buttons onto an /// mdi parent when an MDI child is maximized. /// internal class MdiControlStrip : MenuStrip { private ToolStripMenuItem system; private ToolStripMenuItem close; private ToolStripMenuItem minimize; private ToolStripMenuItem restore; private MenuStrip mergedMenu; private IWin32Window target; ///target is ideally the MDI Child to send the system commands to. /// although there's nothing MDI child specific to it... you could have this /// a toplevel window. /// public MdiControlStrip(IWin32Window target) { IntPtr hMenu= UnsafeNativeMethods.GetSystemMenu(new HandleRef(this, Control.GetSafeHandle(target)), /*bRevert=*/false); this.target = target; // The menu item itself takes care of enabledness and sending WM_SYSCOMMAND messages to the target. minimize = new ControlBoxMenuItem(hMenu, NativeMethods.SC_MINIMIZE, target); close = new ControlBoxMenuItem(hMenu, NativeMethods.SC_CLOSE, target); restore = new ControlBoxMenuItem(hMenu, NativeMethods.SC_RESTORE, target); // The dropDown of the system menu is the one that talks to native. system = new SystemMenuItem(); // However in the event that the target handle changes we have to push the new handle into everyone. Control controlTarget = target as Control; if (controlTarget != null) { controlTarget.HandleCreated += new EventHandler(OnTargetWindowHandleRecreated); controlTarget.Disposed += new EventHandler(OnTargetWindowDisposed); } // add in opposite order to how you want it merged this.Items.AddRange(new ToolStripItem[] { minimize, restore,close, system }); this.SuspendLayout(); foreach (ToolStripItem item in this.Items) { item.DisplayStyle = ToolStripItemDisplayStyle.Image; item.MergeIndex = 0; item.MergeAction = MergeAction.Insert; item.Overflow = ToolStripItemOverflow.Never; item.Alignment = ToolStripItemAlignment.Right; item.Padding = Padding.Empty; } // set up the sytem menu system.Image = GetTargetWindowIcon(); system.Alignment = ToolStripItemAlignment.Left; system.DropDownOpening += new EventHandler(OnSystemMenuDropDownOpening); system.ImageScaling = ToolStripItemImageScaling.None; system.DoubleClickEnabled = true; system.DoubleClick += new EventHandler(OnSystemMenuDoubleClick); system.Padding = Padding.Empty; system.ShortcutKeys = Keys.Alt | Keys.OemMinus; this.ResumeLayout(false); } #region Buttons /* Unused public ToolStripMenuItem System { get { return system; } } */ public ToolStripMenuItem Close { get { return close; } } /* Unused public ToolStripMenuItem Minimize { get { return minimize; } } public ToolStripMenuItem Restore { get { return restore; } } */ #endregion internal MenuStrip MergedMenu { get { return mergedMenu; } set { mergedMenu = value; } } /* PERF: consider shutting off layout #region ShutOffLayout protected override void OnLayout(LayoutEventArgs e) { return; // if someone attempts } protected override Size GetPreferredSize(Size proposedSize) { return Size.Empty; } #endregion */ [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2106:SecureAsserts")] [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] private Image GetTargetWindowIcon() { Image systemIcon = null; IntPtr hIcon = UnsafeNativeMethods.SendMessage(new HandleRef(this, Control.GetSafeHandle(target)), NativeMethods.WM_GETICON, NativeMethods.ICON_SMALL, 0); IntSecurity.ObjectFromWin32Handle.Assert(); try { Icon icon = (hIcon != IntPtr.Zero) ? Icon.FromHandle(hIcon) : Form.DefaultIcon; Icon smallIcon = new Icon(icon, SystemInformation.SmallIconSize); systemIcon = smallIcon.ToBitmap(); smallIcon.Dispose(); } finally { CodeAccessPermission.RevertAssert(); } return systemIcon; } protected internal override void OnItemAdded(ToolStripItemEventArgs e) { base.OnItemAdded(e); Debug.Assert(Items.Count <= 4, "Too many items in the MDIControlStrip. How did we get into this situation?"); } private void OnTargetWindowDisposed(object sender, EventArgs e) { UnhookTarget(); target = null; } private void OnTargetWindowHandleRecreated(object sender, EventArgs e) { // in the case that the handle for the form is recreated we need to set // up the handles to point to the new window handle for the form. system.SetNativeTargetWindow(target); minimize.SetNativeTargetWindow(target); close.SetNativeTargetWindow(target); restore.SetNativeTargetWindow(target); IntPtr hMenu= UnsafeNativeMethods.GetSystemMenu(new HandleRef(this, Control.GetSafeHandle(target)), /*bRevert=*/false); system.SetNativeTargetMenu(hMenu); minimize.SetNativeTargetMenu(hMenu); close.SetNativeTargetMenu(hMenu); restore.SetNativeTargetMenu(hMenu); // clear off the System DropDown. if (system.HasDropDownItems) { // next time we need one we'll just fetch it fresh. system.DropDown.Items.Clear(); system.DropDown.Dispose(); } system.Image = GetTargetWindowIcon(); } private void OnSystemMenuDropDownOpening(object sender, EventArgs e) { if (!system.HasDropDownItems && (target != null)) { system.DropDown = ToolStripDropDownMenu.FromHMenu(UnsafeNativeMethods.GetSystemMenu(new HandleRef(this, Control.GetSafeHandle(target)), /*bRevert=*/false), target); } else if (MergedMenu == null) { system.DropDown.Dispose(); } } private void OnSystemMenuDoubleClick(object sender, EventArgs e) { Close.PerformClick(); } protected override void Dispose(bool disposing) { if (disposing) { UnhookTarget(); target = null; } base.Dispose(disposing); } private void UnhookTarget() { if (target != null) { Control controlTarget = target as Control; if (controlTarget != null) { controlTarget.HandleCreated -= new EventHandler(OnTargetWindowHandleRecreated); controlTarget.Disposed -= new EventHandler(OnTargetWindowDisposed); } target = null; } } // when the system menu item shortcut is evaluated - pop the dropdown internal class ControlBoxMenuItem : ToolStripMenuItem { internal ControlBoxMenuItem(IntPtr hMenu, int nativeMenuCommandId, IWin32Window targetWindow) : base(hMenu, nativeMenuCommandId, targetWindow) { } internal override bool CanKeyboardSelect { get { return false; } } } // when the system menu item shortcut is evaluated - pop the dropdown internal class SystemMenuItem : ToolStripMenuItem { public SystemMenuItem(){ } protected internal override bool ProcessCmdKey(ref Message m, Keys keyData) { if (Visible && ShortcutKeys == keyData) { ShowDropDown(); this.DropDown.SelectNextToolStripItem(null, true); return true; } return base.ProcessCmdKey(ref m, keyData); } protected override void OnOwnerChanged(EventArgs e) { if (HasDropDownItems && DropDown.Visible) { HideDropDown(); } base.OnOwnerChanged(e); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Error.cs
- OleDbMetaDataFactory.cs
- HasCopySemanticsAttribute.cs
- Propagator.JoinPropagator.cs
- RegexMatchCollection.cs
- RequestResizeEvent.cs
- AppearanceEditorPart.cs
- MetadataWorkspace.cs
- BindingWorker.cs
- ArrayTypeMismatchException.cs
- Substitution.cs
- ExpressionNormalizer.cs
- DataGridViewRowConverter.cs
- FamilyCollection.cs
- EntityDataSourceEntityTypeFilterConverter.cs
- StyleModeStack.cs
- TemplateContentLoader.cs
- BitmapEffectCollection.cs
- MessageFormatterConverter.cs
- FormattedTextSymbols.cs
- StylusShape.cs
- EntityParameterCollection.cs
- TextElement.cs
- ProfilePropertySettingsCollection.cs
- MessageSecurityTokenVersion.cs
- DependencyPropertyAttribute.cs
- BindableAttribute.cs
- Clock.cs
- ModuleElement.cs
- Timeline.cs
- GatewayDefinition.cs
- Currency.cs
- MemoryMappedFile.cs
- ConnectionPoint.cs
- TraceHwndHost.cs
- Hyperlink.cs
- nulltextnavigator.cs
- TypedRowHandler.cs
- BlurBitmapEffect.cs
- SystemUnicastIPAddressInformation.cs
- SerializationTrace.cs
- DynamicRouteExpression.cs
- PolyBezierSegmentFigureLogic.cs
- NamedPipeAppDomainProtocolHandler.cs
- StorageEntityTypeMapping.cs
- SeekStoryboard.cs
- PlanCompiler.cs
- Math.cs
- XmlArrayItemAttribute.cs
- DataGridClipboardCellContent.cs
- AppearanceEditorPart.cs
- DataSourceCache.cs
- XmlCharType.cs
- MethodRental.cs
- LocalIdKeyIdentifierClause.cs
- ClientSponsor.cs
- Select.cs
- DrawItemEvent.cs
- Vector3DValueSerializer.cs
- MessageDesigner.cs
- ParseHttpDate.cs
- Viewport3DAutomationPeer.cs
- Path.cs
- CollectionView.cs
- TransformGroup.cs
- AuthorizationRuleCollection.cs
- SqlDataSourceStatusEventArgs.cs
- GACMembershipCondition.cs
- ExpandedProjectionNode.cs
- XslAstAnalyzer.cs
- Canvas.cs
- DataGridPagingPage.cs
- SoapServerProtocol.cs
- listitem.cs
- CornerRadiusConverter.cs
- FullTrustAssemblyCollection.cs
- NullEntityWrapper.cs
- SqlClientMetaDataCollectionNames.cs
- EmptyStringExpandableObjectConverter.cs
- WhitespaceSignificantCollectionAttribute.cs
- RunClient.cs
- StreamSecurityUpgradeProvider.cs
- Transform3DGroup.cs
- ModifierKeysValueSerializer.cs
- FontWeight.cs
- BaseProcessor.cs
- DateTimeConverter.cs
- WorkflowServiceBuildProvider.cs
- CachedFontFace.cs
- ArrangedElementCollection.cs
- IndicFontClient.cs
- RuntimeEnvironment.cs
- JapaneseLunisolarCalendar.cs
- SurrogateSelector.cs
- ImageListImageEditor.cs
- CapabilitiesPattern.cs
- DesignerAttribute.cs
- BaseCodeDomTreeGenerator.cs
- RecordBuilder.cs
- Vector.cs