public static extern bool EnumDisplaySettingsExW(
     [param: MarshalAs(UnmanagedType.LPWStr)]
     [In] string lpszDeviceName,
     [param: MarshalAs(UnmanagedType.U4)]
     [In] int iModeNum,
     [In, Out] ref DEVMODEW lpDevMode,
     [In] int dwFlags);
 public static extern int ChangeDisplaySettingsExW(
     [param: MarshalAs(UnmanagedType.LPWStr)]
     [In] string lpszDeviceName,
     [In, Out] ref DEVMODEW lpDevMode,
     IntPtr hwnd,
     [param: MarshalAs(UnmanagedType.U4)]
     [In] uint dwFlags,
     [In] IntPtr lParam);
            //[MarshalAs(UnmanagedType.ByValArray, SizeConst = privateDriverDataLength)]
            //public Byte[] privateDriverData;

            public static DEVMODEW InitializeNew()
            {
                var result = new DEVMODEW()
                {
                    dmDeviceName = new Char[CCHDEVICENAME],
                    dmFormName   = new Char[CCHFORMNAME],
                    // privateDriverData = new byte[privateDriverDataLength],
                    dmDriverExtra = privateDriverDataLength,
                    dmSize        = (ushort)Marshal.SizeOf(typeof(DEVMODEW)),
                };

                return(result);
            }
Beispiel #4
0
        private string GetTextForTextBlock()
        {
            // 1. Get the window handle ("HWND" in Win32 parlance)
            WindowInteropHelper helper = new WindowInteropHelper(this);
            IntPtr hwnd = helper.Handle;

            // 2. Get a monitor handle ("HMONITOR") for the window.
            //    If the window is straddling more than one monitor, Windows will pick the "best" one.
            IntPtr hmonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);

            if (hmonitor == IntPtr.Zero)
            {
                return("MonitorFromWindow returned NULL ☹");
            }

            // 3. Get more information about the monitor.
            MONITORINFOEXW monitorInfo = new MONITORINFOEXW();

            monitorInfo.cbSize = (uint)Marshal.SizeOf <MONITORINFOEXW>();

            bool bResult = GetMonitorInfoW(hmonitor, ref monitorInfo);

            if (!bResult)
            {
                return("GetMonitorInfoW returned FALSE ☹");
            }

            // 4. Get the current display settings for that monitor, which includes the resolution and refresh rate.
            DEVMODEW devMode = new DEVMODEW();

            devMode.dmSize = (ushort)Marshal.SizeOf <DEVMODEW>();

            bResult = EnumDisplaySettingsW(monitorInfo.szDevice, ENUM_CURRENT_SETTINGS, out devMode);
            if (!bResult)
            {
                return("EnumDisplaySettingsW returned FALSE ☹");
            }

            // Done!
            return(string.Format("{0} x {1} @ {2}hz", devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmDisplayFrequency));
        }
Beispiel #5
0
        private (MONITORINFO mi, string device, int refreshRate) GetMonitorInfoAndSettings(IntPtr hMonitor)
        {
            var(mi, device) = GetMonitorInfoEx(hMonitor);
            DEVMODEW devMode = default;

            if (!EnumDisplaySettings(device, ENUM_DISPLAY_SETTINGS_MODE.ENUM_CURRENT_SETTINGS, ref devMode))
            {
                if (!IsMonitorValid(hMonitor))
                {
                    throw new InvalidDisplayReferenceException(hMonitor);
                }
                if (device == VirtualDeviceName)
                {
                    return(mi, device, GetVirtualMonitorRefreshRate());
                }
                else
                {
                    throw new Win32Exception($"Could not read the settings for monitor \"{device}\".");
                }
            }

            return(mi, device, (int)GetVirtualMonitorRefreshRate());
        }
Beispiel #6
0
 private static extern bool EnumDisplaySettingsW(
     [MarshalAs(UnmanagedType.LPWStr)] string lpszDeviceName,
     uint iModeNum,
     out DEVMODEW lpDevMode);
 public static extern BOOL EnumDisplaySettingsW(string lpszDeviceName, ENUM iModeNum, ref DEVMODEW lpDevMode);