using System.Windows.Controls.Primitives; using System.Windows.Media.Effects; namespace System.Windows.Forms { public partial class ToolStripDropDown { private class PopupWrapper : IPopupWrapper { private System.Windows.Controls.StackPanel panel; private System.Windows.Controls.Primitives.Popup popup; private System.Windows.Controls.Border border; public bool IsOpen { get => popup.IsOpen; set => popup.IsOpen = value; } public PlacementMode Placement { get => popup.Placement; set => popup.Placement = value; } public UIElement PlacementTarget { get => popup.PlacementTarget; set => popup.PlacementTarget = value; } public double HorizontalOffset { get => popup.HorizontalOffset; set => popup.HorizontalOffset = value; } public double VerticalOffset { get => popup.VerticalOffset; set => popup.VerticalOffset = value; } public event EventHandler Opened; public event EventHandler Closed; public void AddChild(Control child) => panel.Children.Add(child.control); public void RemoveChild(Control child) => panel.Children.Remove(child.control); public void SetChildIndex(Control child, int index) => panel.Children.Insert(index, child.control); public void ApplyStyle(ToolStripProfessionalRenderer r) { } public void FixupDpi() { if (popup.PlacementTarget != null) { double sc1 = Helper.GetDpiScale(panel); double sc2 = Helper.GetDpiScale(popup.PlacementTarget); if (Math.Abs(sc1 - sc2) > 0.01) { // dpi is different, refresh the control popup.IsOpen = true; popup.IsOpen = false; } } } public PopupWrapper(Control owner) { // it will contain children panel = new(); popup = new(); popup.UseLayoutRounding = true; popup.StaysOpen = false; popup.AllowsTransparency = true; popup.PopupAnimation = PopupAnimation.Slide; popup.Tag = owner; border = new() { Margin = new Thickness(0, 0, 3, 3), BorderBrush = System.Windows.SystemColors.ActiveBorderBrush, BorderThickness = new Thickness(1), Background = System.Windows.SystemColors.WindowBrush }; border.Effect = new DropShadowEffect() { Color = System.Windows.Media.Colors.Gray, Opacity = 0.5, ShadowDepth = 3, Direction = 315 }; popup.Child = border; border.Child = panel; popup.Opened += (s, e) => Opened?.Invoke(this, e); popup.Closed += (s, e) => Closed?.Invoke(this, e); } } } }