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(); } }
/// <devdoc> 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. /// </devdoc> 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; // image is not scaled well on high dpi devices. Setting property to fit to size. item.ImageScaling = ToolStripItemImageScaling.SizeToFit; } // 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); }