protected virtual NativeMethods.MonitorInfo GetMonitorInfo()
        {
            var monitorHandle = NativeMethods.GetCurrentMonitor();
            var monitorInfo   = new NativeMethods.MonitorInfo();

            NativeMethods.GetMonitorInfo(monitorHandle, monitorInfo);

            return(monitorInfo);
        }
Example #2
0
        /// <summary>
        /// Implements custom placement for ribbon popup
        /// </summary>
        /// <param name="popupsize"></param>
        /// <param name="targetsize"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        private CustomPopupPlacement[] CustomPopupPlacementMethod(Size popupsize, Size targetsize, Point offset)
        {
            if (this.DropDownPopup != null &&
                this.SelectedTabItem != null)
            {
                // Get current workarea
                var tabItemPos  = this.SelectedTabItem.PointToScreen(new Point(0, 0));
                var tabItemRect = new NativeMethods.Rect
                {
                    Left   = (int)tabItemPos.X,
                    Top    = (int)tabItemPos.Y,
                    Right  = (int)tabItemPos.X + (int)this.SelectedTabItem.ActualWidth,
                    Bottom = (int)tabItemPos.Y + (int)this.SelectedTabItem.ActualHeight
                };

                const uint MONITOR_DEFAULTTONEAREST = 0x00000002;

                var monitor = NativeMethods.MonitorFromRect(ref tabItemRect, MONITOR_DEFAULTTONEAREST);
                if (monitor != IntPtr.Zero)
                {
                    var monitorInfo = new NativeMethods.MonitorInfo();
                    monitorInfo.Size = Marshal.SizeOf(monitorInfo);
                    NativeMethods.GetMonitorInfo(monitor, monitorInfo);

                    var startPoint = this.PointToScreen(new Point(0, 0));
                    if (this.FlowDirection == FlowDirection.RightToLeft)
                    {
                        startPoint.X -= this.ActualWidth;
                    }

                    var inWindowRibbonWidth = monitorInfo.Work.Right - Math.Max(monitorInfo.Work.Left, startPoint.X);

                    var actualWidth = this.ActualWidth;
                    if (startPoint.X < monitorInfo.Work.Left)
                    {
                        actualWidth -= monitorInfo.Work.Left - startPoint.X;
                        startPoint.X = monitorInfo.Work.Left;
                    }

                    // Set width and prevent negative values
                    this.DropDownPopup.Width = Math.Max(0, Math.Min(actualWidth, inWindowRibbonWidth));
                    return(new[]
                    {
                        new CustomPopupPlacement(new Point(startPoint.X - tabItemPos.X, this.SelectedTabItem.ActualHeight - ((FrameworkElement)this.DropDownPopup.Child).Margin.Top), PopupPrimaryAxis.None),
                        new CustomPopupPlacement(new Point(startPoint.X - tabItemPos.X, -((ScrollViewer)this.SelectedContent).ActualHeight - ((FrameworkElement)this.DropDownPopup.Child).Margin.Bottom), PopupPrimaryAxis.None)
                    });
                }
            }

            return(null);
        }
Example #3
0
        /// <summary>
        /// Implements custom placement for ribbon popup
        /// </summary>
        /// <param name="popupsize"></param>
        /// <param name="targetsize"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        CustomPopupPlacement[] CustomPopupPlacementMethod(Size popupsize, Size targetsize, Point offset)
        {
            if ((popup != null) && (SelectedTabItem != null))
            {
                // Get current workarea
                Point tabItemPos = SelectedTabItem.PointToScreen(new Point(0, 0));
                NativeMethods.Rect tabItemRect = new NativeMethods.Rect();
                tabItemRect.Left   = (int)tabItemPos.X;
                tabItemRect.Top    = (int)tabItemPos.Y;
                tabItemRect.Right  = (int)tabItemPos.X + (int)SelectedTabItem.ActualWidth;
                tabItemRect.Bottom = (int)tabItemPos.Y + (int)SelectedTabItem.ActualHeight;

                uint          MONITOR_DEFAULTTONEAREST = 0x00000002;
                System.IntPtr monitor = NativeMethods.MonitorFromRect(ref tabItemRect, MONITOR_DEFAULTTONEAREST);
                if (monitor != System.IntPtr.Zero)
                {
                    NativeMethods.MonitorInfo monitorInfo = new NativeMethods.MonitorInfo();
                    monitorInfo.Size = Marshal.SizeOf(monitorInfo);
                    NativeMethods.GetMonitorInfo(monitor, monitorInfo);

                    Point startPoint = PointToScreen(new Point(0, 0));
                    if (FlowDirection == FlowDirection.RightToLeft)
                    {
                        startPoint.X -= ActualWidth;
                    }
                    double inWindowRibbonWidth = monitorInfo.Work.Right - Math.Max(monitorInfo.Work.Left, startPoint.X);

                    double actualWidth = ActualWidth;
                    if (startPoint.X < monitorInfo.Work.Left)
                    {
                        actualWidth -= monitorInfo.Work.Left - startPoint.X;
                        startPoint.X = monitorInfo.Work.Left;
                    }
                    // Set width
                    popup.Width = Math.Min(actualWidth, inWindowRibbonWidth);
                    return(new CustomPopupPlacement[]
                    {
                        new CustomPopupPlacement(new Point(startPoint.X - tabItemPos.X, SelectedTabItem.ActualHeight - (popup.Child as FrameworkElement).Margin.Top), PopupPrimaryAxis.None),
                        new CustomPopupPlacement(new Point(startPoint.X - tabItemPos.X, -(SelectedContent as RibbonGroupsContainer).ActualHeight - (popup.Child as FrameworkElement).Margin.Bottom), PopupPrimaryAxis.None),
                    });
                }
            }
            return(null);
        }
Example #4
0
        /// <summary>
        /// Returns monitor in witch control is placed
        /// </summary>
        /// <param name="control">Control</param>
        /// <returns>Workarea in witch control is placed</returns>
        public static Rect GetControlMonitor(FrameworkElement control)
        {
            Point tabItemPos = control.PointToScreen(new Point(0, 0));

            NativeMethods.Rect tabItemRect = new NativeMethods.Rect();
            tabItemRect.Left   = (int)tabItemPos.X;
            tabItemRect.Top    = (int)tabItemPos.Y;
            tabItemRect.Right  = (int)tabItemPos.X + (int)control.ActualWidth;
            tabItemRect.Bottom = (int)tabItemPos.Y + (int)control.ActualHeight;
            uint MONITOR_DEFAULTTONEAREST = 0x00000002;

            System.IntPtr monitor = NativeMethods.MonitorFromRect(ref tabItemRect, MONITOR_DEFAULTTONEAREST);
            if (monitor != System.IntPtr.Zero)
            {
                NativeMethods.MonitorInfo monitorInfo = new NativeMethods.MonitorInfo();
                monitorInfo.Size = Marshal.SizeOf(monitorInfo);
                NativeMethods.GetMonitorInfo(monitor, monitorInfo);
                return(new Rect(monitorInfo.Monitor.Left, monitorInfo.Monitor.Top, monitorInfo.Monitor.Right - monitorInfo.Monitor.Left, monitorInfo.Monitor.Bottom - monitorInfo.Monitor.Top));
            }
            return(new Rect());
        }