123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 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));
- };
- }
- }
- }
|