Пример #1
0
        // Check if current location of this window is good for delayed adjustment.
        private bool IsLocationGood()
        {
            if (_oldDpiValue == 0)
            {
                return(false);                // Abort.
            }
            float factor = _newDpiValue / _oldDpiValue;

            // Prepare new rectangle shrinked or expanded sticking Left-Top corner.
            int widthDiff  = (int)(ClientSize.Width * factor) - ClientSize.Width;
            int heightDiff = (int)(ClientSize.Height * factor) - ClientSize.Height;

            Win32Api.RECT rect = new Win32Api.RECT()
            {
                left   = Bounds.Left,
                top    = Bounds.Top,
                right  = Bounds.Right + widthDiff,
                bottom = Bounds.Bottom + heightDiff
            };

            // Get handle to monitor that has the largest intersection with the rectangle.
            IntPtr handleMonitor = Win32Api.MonitorFromRect(ref rect, Win32Api.MONITOR_DEFAULTTONULL);

            if (handleMonitor != IntPtr.Zero)
            {
                // Check if DPI of the monitor matches.
                if (GetDpiSpecifiedMonitor(handleMonitor) == _newDpiValue)
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #2
0
        // Get new location of this window after DPI change.
        private void MoveWindow()
        {
            if (_oldDpiValue == 0)
            {
                return;                 // Abort.
            }
            float factor = _newDpiValue / _oldDpiValue;

            // Prepare new rectangles shrinked or expanded sticking four corners.
            int widthDiff  = (int)(ClientSize.Width * factor) - ClientSize.Width;
            int heightDiff = (int)(ClientSize.Height * factor) - ClientSize.Height;

            List <Win32Api.RECT> rectList = new List <Win32Api.RECT>();

            // Left-Top corner
            rectList.Add(new Win32Api.RECT
            {
                left   = Bounds.Left,
                top    = Bounds.Top,
                right  = Bounds.Right + widthDiff,
                bottom = Bounds.Bottom + heightDiff
            });

            // Right-Top corner
            rectList.Add(new Win32Api.RECT
            {
                left   = Bounds.Left - widthDiff,
                top    = Bounds.Top,
                right  = Bounds.Right,
                bottom = Bounds.Bottom + heightDiff
            });

            // Left-Bottom corner
            rectList.Add(new Win32Api.RECT
            {
                left   = Bounds.Left,
                top    = Bounds.Top - heightDiff,
                right  = Bounds.Right + widthDiff,
                bottom = Bounds.Bottom
            });

            // Right-Bottom corner
            rectList.Add(new Win32Api.RECT
            {
                left   = Bounds.Left - widthDiff,
                top    = Bounds.Top - heightDiff,
                right  = Bounds.Right,
                bottom = Bounds.Bottom
            });

            // Get handle to monitor that has the largest intersection with each rectangle.
            for (int i = 0; i <= rectList.Count - 1; i++)
            {
                Win32Api.RECT rectBuf = rectList[i];

                IntPtr handleMonitor = Win32Api.MonitorFromRect(ref rectBuf, Win32Api.MONITOR_DEFAULTTONULL);

                if (handleMonitor != IntPtr.Zero)
                {
                    // Check if at least Left-Top corner or Right-Top corner is inside monitors.
                    IntPtr handleLeftTop  = Win32Api.MonitorFromPoint(new Win32Api.POINT(rectBuf.left, rectBuf.top), Win32Api.MONITOR_DEFAULTTONULL);
                    IntPtr handleRightTop = Win32Api.MonitorFromPoint(new Win32Api.POINT(rectBuf.right, rectBuf.top), Win32Api.MONITOR_DEFAULTTONULL);

                    if ((handleLeftTop != IntPtr.Zero) || (handleRightTop != IntPtr.Zero))
                    {
                        // Check if DPI of the monitor matches.
                        if (GetDpiSpecifiedMonitor(handleMonitor) == _newDpiValue)
                        {
                            // Move this window.
                            Location = new Point(rectBuf.left, rectBuf.top);
                            break;
                        }
                    }
                }
            }
        }