Exemplo n.º 1
0
        public static Rect CheckScreenRect(IntPtr windowHandle)
        {
            Rect rect = new Rect();

            if (windowHandle == IntPtr.Zero)
            {
                return(rect);
            }

            IntPtr        hMonitor    = DpiUtil.MonitorFromWindow(windowHandle, DpiUtil.MONITOR_DEFAULTTONEAREST);
            MONITORINFOEX monitorInfo = DisplayUtil.CreateMonitorInfo();
            int           result      = DisplayUtil.GetMonitorInfo(hMonitor, ref monitorInfo);

            if (result == 0)
            {
                log.Error("failed to get monitor info");
                return(rect);
            }

            DisplayUtil.DEVMODE dm = DisplayUtil.CreateDevmode();
            if (0 != DisplayUtil.EnumDisplaySettings(
                    monitorInfo.szDevice,
                    DisplayUtil.ENUM_CURRENT_SETTINGS,
                    ref dm))
            {
                rect = new Rect(dm.dmPositionX, dm.dmPositionY, dm.dmPelsWidth, dm.dmPelsHeight);
                log.WarnFormat("CheckScreenRect: {0}, {1}, {2}, {3}, {4}", monitorInfo.szDevice, dm.dmPositionX, dm.dmPositionY, dm.dmPelsWidth, dm.dmPelsHeight);
            }
            return(rect);
        }
Exemplo n.º 2
0
        public static void CheckScreenOrientation(IntPtr windowHandle)
        {
            if (windowHandle == IntPtr.Zero)
            {
                return;
            }

            IntPtr        hMonitor    = DpiUtil.MonitorFromWindow(windowHandle, DpiUtil.MONITOR_DEFAULTTONEAREST);
            MONITORINFOEX monitorInfo = DisplayUtil.CreateMonitorInfo();
            int           result      = DisplayUtil.GetMonitorInfo(hMonitor, ref monitorInfo);

            if (result == 0)
            {
                log.Error("failed to get monitor info");
                return;
            }

            DisplayUtil.DEVMODE dm = DisplayUtil.CreateDevmode();
            if (0 != DisplayUtil.EnumDisplaySettings(
                    monitorInfo.szDevice,
                    DisplayUtil.ENUM_CURRENT_SETTINGS,
                    ref dm))
            {
                // swap width and height
                int temp = dm.dmPelsHeight;
                dm.dmPelsHeight = dm.dmPelsWidth;
                dm.dmPelsWidth  = temp;

                int iRet = 0;
                // determine new orientation
                switch (dm.dmDisplayOrientation)
                {
                case DisplayUtil.DMDO_DEFAULT:
                    break;

                case DisplayUtil.DMDO_270:
                    dm.dmDisplayOrientation = DisplayUtil.DMDO_180;
                    iRet = DisplayUtil.ChangeDisplaySettingsEx(monitorInfo.szDevice, ref dm, IntPtr.Zero, 0, IntPtr.Zero);
                    if (iRet != 0)
                    {
                        log.Error("ChangeDisplaySettings() failed, return value=" + iRet);
                    }
                    break;

                case DisplayUtil.DMDO_180:
                    break;

                case DisplayUtil.DMDO_90:
                    dm.dmDisplayOrientation = DisplayUtil.DMDO_DEFAULT;
                    iRet = DisplayUtil.ChangeDisplaySettingsEx(monitorInfo.szDevice, ref dm, IntPtr.Zero, 0, IntPtr.Zero);
                    if (iRet != 0)
                    {
                        log.Error("ChangeDisplaySettings() failed, return value=" + iRet);
                    }
                    break;

                default:
                    // unknown orientation value
                    // add exception handling here
                    break;
                }
            }
        }