ContextMenuStrip.PopupWrapper.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Windows.Controls.Primitives;
  2. namespace System.Windows.Forms
  3. {
  4. public partial class ContextMenuStrip
  5. {
  6. private class PopupWrapper : IPopupWrapper
  7. {
  8. private CustomControls.ContextMenuStrip menu;
  9. private ContextMenuStrip owner;
  10. public bool IsOpen
  11. {
  12. get => menu.IsOpen;
  13. set
  14. {
  15. menu.EnableQuickAccessToolbar = owner.EnableQuickAccessToolbar;
  16. menu.SetIsOpen(value);
  17. }
  18. }
  19. public PlacementMode Placement { get => menu.Placement; set => menu.Placement = value; }
  20. public UIElement PlacementTarget { get => menu.PlacementTarget; set => menu.PlacementTarget = value; }
  21. public double HorizontalOffset { get => menu.HorizontalOffset; set => menu.HorizontalOffset = value; }
  22. public double VerticalOffset { get => menu.VerticalOffset; set => menu.VerticalOffset = value; }
  23. public event EventHandler Opened;
  24. public event EventHandler Closed;
  25. public void AddChild(Control child) => menu.Items.Add(child.control);
  26. public void RemoveChild(Control child) => menu.Items.Remove(child.control);
  27. public void SetChildIndex(Control child, int index) => menu.Items.Insert(index, child.control);
  28. public void ApplyStyle(ToolStripProfessionalRenderer r)
  29. {
  30. menu.Resources["Menu.Static.Background"] = r.MenuStaticBackground;
  31. menu.Resources["Menu.Static.Border"] = r.MenuStaticBorder;
  32. menu.Resources["Menu.Static.Foreground"] = r.Foreground;
  33. menu.Resources["Menu.Disabled.Foreground"] = r.DisabledForeground;
  34. menu.Resources["Menu.Margin.Background"] = r.MenuMarginBackground;
  35. menu.Resources["Menu.QAT.Background"] = r.MenuQATBackground;
  36. }
  37. public void FixupDpi()
  38. {
  39. // In case dpi was changed during app run, the menu may not be updated yet.
  40. // It will show odd font size at the first open. Correct that by supplying correct dpi from PlacementTarget object.
  41. if (menu.PlacementTarget != null)
  42. Helper.SetFont(menu, Helper.GetDpiScale(menu.PlacementTarget), owner.Font);
  43. else
  44. Helper.SetFont(menu, owner.Font);
  45. }
  46. public PopupWrapper(ContextMenuStrip owner)
  47. {
  48. menu = new();
  49. this.owner = owner;
  50. menu.Tag = owner;
  51. menu.Opened += (s, e) => Opened?.Invoke(this, EventArgs.Empty);
  52. menu.Closed += (s, e) => Closed?.Invoke(this, EventArgs.Empty);
  53. }
  54. }
  55. }
  56. }