using System.ComponentModel; using System.Drawing; namespace System.Windows.Forms { public class ToolStripMenuItem : ToolStripDropDownItem { private CustomControls.ToolStripMenuItem menuItem; private System.Windows.Controls.Image image; private Window inputBindingWindow; public override string Text { get => menuItem.Header?.ToString(); set => menuItem.Header = value; } public bool Checked { get => menuItem.IsChecked; set => menuItem.IsChecked = value; } public bool CheckOnClick { get => menuItem.IsCheckable; set => menuItem.IsCheckable = value; } private Keys shortcutKeys; public Keys ShortcutKeys { get => shortcutKeys; set { shortcutKeys = value; var typeConverter = TypeDescriptor.GetConverter(value); menuItem.InputGestureText = typeConverter.ConvertToString(value); SetInputBinding(true); } } // in WPF, used to indicate that an item must be placed in the quick commands toolbar public bool QatItem { get; set; } private void MenuItem_PreviewKeyDown(object sender, Input.KeyEventArgs e) { var args = Helper.GetKeyEventArgs(e); if (args.KeyData == ShortcutKeys && Enabled) { // prevent keys pressed in the TextBox (such as Ctrl+C/V/X) from handling by the menu if (e.OriginalSource is System.Windows.Controls.TextBox) return; e.Handled = true; OnItemClicked(); } } internal void SetInputBinding(bool set) { if (set && ShortcutKeys != Keys.None) { if (inputBindingWindow == null) { inputBindingWindow = System.Windows.Window.GetWindow(menuItem); if (inputBindingWindow != null) inputBindingWindow.PreviewKeyDown += MenuItem_PreviewKeyDown; } } else if (!set) { if (inputBindingWindow != null) { inputBindingWindow.PreviewKeyDown -= MenuItem_PreviewKeyDown; inputBindingWindow = null; } } } internal override void AddChild(Control child) { menuItem.Items.Add(child.control); } internal override void RemoveChild(Control child) { menuItem.Items.Remove(child.control); } internal override void SetChildIndex(Control child, int index) { menuItem.Items.Insert(index, child.control); } internal void OnItemClicked() { OnClick(EventArgs.Empty); (GetToolStrip() as ContextMenuStrip)?.DoItemClicked(this); } public override void ApplyStyle(ToolStripProfessionalRenderer r) { base.ApplyStyle(r); menuItem.Resources["Menu.Static.Background"] = r.MenuStaticBackground; menuItem.Resources["Menu.Static.Border"] = r.MenuStaticBorder; menuItem.Resources["Menu.Static.Foreground"] = r.Foreground; menuItem.Resources["Menu.Disabled.Foreground"] = r.DisabledForeground; menuItem.Resources["Menu.Margin.Background"] = r.MenuMarginBackground; menuItem.Resources["TopLevelItem.Static.Foreground"] = r.TopLevelMenuItemForeground; menuItem.Resources["TopLevelItem.Disabled.Foreground"] = r.TopLevelMenuItemDisabledForeground; menuItem.Resources["MenuItem.Check.Background"] = r.MenuItemCheckBackground; menuItem.Resources["MenuItem.Check.Border"] = r.MenuItemCheckBorder; menuItem.Resources["MenuItem.Highlight.Background"] = r.MenuItemHighlightBackground; menuItem.Resources["MenuItem.Highlight.Border"] = r.MenuItemHighlightBorder; } internal override void ResetImage() { base.ResetImage(); if (ImageSource != null) { if (image == null) { image = new(); menuItem.Icon = image; } image.Source = ImageSource; } } public ToolStripMenuItem(string text) : this() { Text = text; } public ToolStripMenuItem(string text, Image image) : this() { Text = text; Image = image; } public ToolStripMenuItem() { menuItem = new(); SetControl(menuItem); menuItem.HorizontalAlignment = Windows.HorizontalAlignment.Stretch; menuItem.VerticalAlignment = VerticalAlignment.Stretch; menuItem.Loaded += (s, e) => SetInputBinding(true); menuItem.Unloaded += (s, e) => SetInputBinding(false); menuItem.Click += (s, e) => OnItemClicked(); menuItem.SubmenuOpened += (s, e) => { e.Handled = true; OnDropDownOpening(new CancelEventArgs()); OnDropDownOpened(e); }; menuItem.SubmenuClosed += (s, e) => { e.Handled = true; OnDropDownClosed(new ToolStripDropDownClosedEventArgs(ToolStripDropDownCloseReason.ItemClicked)); }; } } }