using System; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; namespace MuchInfo.Chart.Infrastructure.Helpers { public class PopupManager { #region Fields //public static Panel PopupRoot { get; set; } #endregion Fields #region Enumerations public enum RelativePositionHoriz { Left, FlushRights, FlushLefts, Right } public enum RelativePositionVert { Above, FlushBottoms, FlushTops, Below } #endregion Enumerations #region Methods private static Popup CreatePopup() { return new Popup { AllowsTransparency = true, Placement = PlacementMode.Relative, }; } #region Public Static Methods public static Popup CreateAppOverlay(Panel popupRoot) { var popup = CreatePopup(); var popupManager = new PopupManager.AppOverlayPopup(); popupManager.BuildPopup(popup, popupRoot); return popup; } public static Popup CreateAppOverlay(MouseEventArgs position, Panel popupRoot) { var popup = CreatePopup(); var popupManager = new PopupManager.AppOverlayPopup(); popupManager.BuildPopup(popup, popupRoot); bool flag = position != null; if (flag) { var p1 = position.GetPosition(popupRoot); popup.HorizontalOffset = (p1.X); popup.VerticalOffset = (p1.Y); } return popup; } public static Popup CreateOverlay(Panel popupRoot) { var popup = CreatePopup(); var popupManager = new PopupManager.OverlayPopup(); popupManager.BuildPopup(popup, popupRoot); return popup; } public static Popup CreateOverlay(MouseEventArgs position, Panel popupRoot) { var popup = CreatePopup(); var popupManager = new PopupManager.OverlayPopup(); popupManager.BuildPopup(popup, popupRoot); bool flag = position != null; if (flag) { var p1 = position.GetPosition(popupRoot); popup.HorizontalOffset = (p1.X); popup.VerticalOffset = (p1.Y); } return popup; } //public static Popup CreatePopUp(MouseEventArgs position, Panel popupRoot) //{ // var popup = new Popup(); // bool flag = position != null; // if (flag) // { // var currentPosition = position.GetPosition(popupRoot); // popup.HorizontalOffset = (currentPosition.X); // popup.VerticalOffset = (currentPosition.Y); // } // popup.Closed += (new EventHandler(PopupManager.PopUpManager_2090)); // popupRoot.Children.Add(popup); // return popup; //} //public static Popup CreatePopUp(Panel popupRoot) //{ // var popup = new Popup(); // popup.Closed += (new EventHandler(PopupManager.PopUpManager_2090)); // popupRoot.Children.Add(popup); // return popup; //} public static void MovePopup(Popup aPop, FrameworkElement relativeTo, PopupManager.RelativePositionHoriz positionLeft, PopupManager.RelativePositionVert positionTop, Panel popupRoot) { aPop.UpdateLayout(); bool flag = aPop.Child != null; if (flag) { aPop.Child.UpdateLayout(); } Point point = GeometryHelper.PositionOfControlRelative(relativeTo, popupRoot); switch (positionLeft) { case PopupManager.RelativePositionHoriz.Left: point.X = (point.X - aPop.ActualWidth); break; case PopupManager.RelativePositionHoriz.FlushRights: point.X = (point.X - (aPop.ActualWidth - relativeTo.ActualWidth)); break; case PopupManager.RelativePositionHoriz.Right: point.X = (point.X + relativeTo.ActualWidth); break; } switch (positionTop) { case PopupManager.RelativePositionVert.Above: point.Y = (point.Y - aPop.ActualHeight); break; case PopupManager.RelativePositionVert.FlushBottoms: point.Y = (point.Y - (aPop.ActualHeight - relativeTo.ActualHeight)); break; case PopupManager.RelativePositionVert.Below: point.Y = (point.Y + relativeTo.ActualHeight); break; } aPop.HorizontalOffset = (point.X); aPop.VerticalOffset = (point.Y); } //public static void SetPopUpRoot(UIElement root) //{ // bool flag = root is Panel; // if (flag) // { // PopupManager.PopupRoot = (Panel)root; // return; // } // throw new Exception("Root is not a panel"); //} #endregion Public Static Methods #region Private Static Methods //private static void PopUpManager_2090(object sender, System.EventArgs e) //{ // bool flag = sender is Popup; // if (flag) // { // var popup = (Popup)sender; // try // { // popup.Child = (null); // popup.Closed -= (new EventHandler(PopupManager.PopUpManager_2090)); // PopupManager.PopupRoot.Children.Remove(popup); // } // catch (Exception expr_59) // { // ProjectData.SetProjectError(expr_59); // ProjectData.ClearProjectError(); // } // } //} #endregion Private Static Methods #endregion Methods #region Nested Types private sealed class AppOverlayPopup { #region Fields private Popup _popup; private Panel _panel; #endregion Fields #region Properties #region Public Properties private Popup Popup { get { return this._popup; } set { var sizeChangedEventHandler = new SizeChangedEventHandler(this.Popup_SizeChanged); var eventHandler = new EventHandler(this.Popup_Closed); bool flag = this._popup != null; if (flag) { this._popup.SizeChanged -= (sizeChangedEventHandler); this._popup.Closed -= (eventHandler); } this._popup = value; flag = (this._popup != null); if (flag) { this._popup.SizeChanged += (sizeChangedEventHandler); this._popup.Closed += (eventHandler); } } } private Panel Root { get { return this._panel; } set { var mouseButtonEventHandler = new MouseButtonEventHandler(this.Panel_MouseLeftButtonDown); var routedEventHandler = new RoutedEventHandler(this.Panel_GotFocus); bool flag = this._panel != null; if (flag) { this._panel.MouseLeftButtonDown -= (mouseButtonEventHandler); this._panel.GotFocus -= (routedEventHandler); } this._panel = value; flag = (this._panel != null); if (flag) { this._panel.MouseLeftButtonDown += (mouseButtonEventHandler); this._panel.GotFocus += (routedEventHandler); } } } #endregion Public Properties #endregion Properties #region Methods #region Internal Methods internal void BuildPopup(Popup aPop, Panel root) { this.Root = root; this.Popup = aPop; root.Children.Add(this.Popup); } #endregion Internal Methods #region Private Methods private void Popup_Closed(object sender, System.EventArgs e) { this.Root.Children.Remove(this.Popup); this.Popup.Child = (null); this.Root = null; this.Popup = null; } private void Panel_GotFocus(object sender, RoutedEventArgs e) { this.Popup.IsOpen = false; } private void Panel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.Popup.IsOpen = false; } private void Popup_SizeChanged(object sender, SizeChangedEventArgs e) { GeometryHelper.EnsureControlInApplication(this.Popup, this.Root); } #endregion Private Methods #endregion Methods } private sealed class OverlayPopup { #region Fields private Popup _popup; private Panel _panel; #endregion Fields #region Constructors public OverlayPopup() { //this.MainGrid = new Grid(); } #endregion Constructors #region Properties #region Public Properties //private Grid MainGrid //{ // get // { // return this._mainGrid; // } // set // { // var mouseButtonEventHandler = new MouseButtonEventHandler(MainGrid_MouseLeftButtonDown); // bool flag = this._mainGrid != null; // if (flag) // { // this._mainGrid.MouseLeftButtonDown -= (mouseButtonEventHandler); // } // this._mainGrid = value; // flag = (this._mainGrid != null); // if (flag) // { // this._mainGrid.MouseLeftButtonDown += (mouseButtonEventHandler); // } // } //} //private Popup BasePopup { get; set; } private Popup Popup { get { return this._popup; } set { var eventHandler = new EventHandler(this.Popup_Closed); bool flag = this._popup != null; if (flag) { this._popup.Closed -= (eventHandler); } this._popup = value; flag = (this._popup != null); if (flag) { this._popup.Closed += (eventHandler); } } } private Panel Root { get { return this._panel; } set { //var sizeChangedEventHandler = new SizeChangedEventHandler(this.Panel_SizeChanged); //bool flag = this._panel != null; //if (flag) //{ // this._panel.SizeChanged -= (sizeChangedEventHandler); //} this._panel = value; //flag = (this._panel != null); //if (flag) //{ // this._panel.SizeChanged += (sizeChangedEventHandler); //} } } #endregion Public Properties #endregion Properties #region Methods #region Internal Methods internal void BuildPopup(Popup popup, Panel root) { this.Root = root; this.Popup = popup; root.Children.Add(this.Popup); //this.BasePopup = new Popup() //{ // AllowsTransparency = true, // Placement = PlacementMode.Relative //}; //root.Children.Add(this.BasePopup); //this.BasePopup.IsOpen = true; //this.MainGrid.Background = new SolidColorBrush(Colors.Transparent); //this.MainGrid.Width = root.ActualWidth; //this.MainGrid.Height = root.ActualHeight; //this.BasePopup.Child = this.MainGrid; //父容器左击其它区域退出菜单 this.Root.MouseLeftButtonDown -= Root_MouseLeftButtonDown; this.Root.MouseLeftButtonDown += Root_MouseLeftButtonDown; //父容器右击其它区域退出菜单(后面添加的) this.Root.MouseRightButtonDown -= Root_MouseLeftButtonDown; this.Root.MouseRightButtonDown += Root_MouseLeftButtonDown; } void Root_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //popup不能弹出看这里:在popup父容器MouseLeftButtonDown set handled = true if (Popup != null) Popup.IsOpen = false; } #endregion Internal Methods #region Private Static Methods #endregion Private Static Methods #region Private Methods //private void MainGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) //{ // this.Popup.IsOpen = false; //} private void Popup_Closed(object sender, System.EventArgs e) { //this.BasePopup.IsOpen = false; this.Root.Children.Remove(this.Popup); //this.Root.Children.Remove(this.BasePopup); this.Root.MouseLeftButtonDown -= Root_MouseLeftButtonDown; this.Root.MouseRightButtonDown -= Root_MouseLeftButtonDown; this.Popup.Child = null; //this.BasePopup = null; this.Root = null; //this.MainGrid = null; this.Popup = null; } //private void Panel_SizeChanged(object sender, SizeChangedEventArgs e) //{ // this.MainGrid.Width = this.Root.ActualWidth; // this.MainGrid.Height = this.Root.ActualHeight; //} #endregion Private Methods #endregion Methods } #endregion Nested Types } }