Пример #1
0
        private static Win32Api.W32Rect GetMonitor(Win32Api.W32Point position)
        {
            IntPtr monitorHandle = Win32Api.MonitorFromPoint(position, Win32Api.MonitorOptions.MONITOR_DEFAULT_NEAREST); //0x00000002: return nearest monitor if pt is not contained in any monitor

            Win32Api.W32MonitorInfo monitorInfo = new Win32Api.W32MonitorInfo
            {
                Size = Marshal.SizeOf(typeof(Win32Api.W32MonitorInfo))
            };

            if (!Win32Api.GetMonitorInfo(monitorHandle, ref monitorInfo))
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            return(monitorInfo.WorkArea);
        }
Пример #2
0
        private static void ComputeTopLeft(ref Window window, Point?caretPosition)  //TODO: don't use ref
        {
            Win32Api.W32Rect monitor;
            double           top;
            double           left;

            //if caret position can be found open the window there, if not open it at the mouse cursor //TODO: Define in settings where to open?
            if (caretPosition != null)
            {
                monitor = GetMonitor(new Win32Api.W32Point()
                {
                    X = (int)caretPosition.Value.X, Y = (int)caretPosition.Value.Y
                });
                top  = caretPosition.Value.Y;
                left = caretPosition.Value.X;
            }
            else
            {
                Win32Api.W32Point cursorPosition = new Win32Api.W32Point();
                if (!Win32Api.GetCursorPos(ref cursorPosition))
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }

                monitor = GetMonitor(cursorPosition);

                double offsetX = Math.Round(window.Width / 2);
                double offsetY = Math.Round(window.Height / 2);

                top  = cursorPosition.Y - offsetY;
                left = cursorPosition.X - offsetX;
            }

            Rect screen = new Rect(new Point(monitor.Left, monitor.Top), new Point(monitor.Right, monitor.Bottom));
            Rect wnd    = new Rect(new Point(left, top), new Point(left + window.Width, top + window.Height));

            window.Top  = wnd.Top;
            window.Left = wnd.Left;

            if (!screen.Contains(wnd))
            {
                if (wnd.Top < screen.Top)
                {
                    double diff = Math.Abs(screen.Top - wnd.Top);
                    window.Top = wnd.Top + diff;
                }

                if (wnd.Bottom > screen.Bottom)
                {
                    double diff = wnd.Bottom - screen.Bottom;
                    window.Top = wnd.Top - diff;
                }

                if (wnd.Left < screen.Left)
                {
                    double diff = Math.Abs(screen.Left - wnd.Left);
                    window.Left = wnd.Left + diff;
                }

                if (wnd.Right > screen.Right)
                {
                    double diff = wnd.Right - screen.Right;
                    window.Left = wnd.Left - diff;
                }
            }
        }