private static bool EnumMonitorsProc(IntPtr hMonitor, IntPtr hdcMonitor, ref RectStruct lprcMonitor, IntPtr dwData) { MonitorInfoEx mi = new MonitorInfoEx(); mi.Size = (uint)Marshal.SizeOf(mi); bool success = GetMonitorInfo(hMonitor, ref mi); if (success) { ScreenInfo si = new ScreenInfo(); si.MonitorArea = Oblong.FromRectStruct(mi.Monitor); si.WorkArea = Oblong.FromRectStruct(mi.WorkArea); si.DeviceName = mi.DeviceName; si.IsPrimaryScreen = ((mi.Flags & MONITORINFOF_PRIMARY) == 1);; DEVMODE DeviceMode = new DEVMODE(); DeviceMode.Initialize(); if (EnumDisplaySettingsEx(ToLPTStr(mi.DeviceName), -1, ref DeviceMode)) { si.Scaling = Math.Round(((double)DeviceMode.dmPelsHeight / (mi.Monitor.bottom - mi.Monitor.top)) * 100); } si.NativeWorkArea = new Oblong((int)(mi.WorkArea.left * si.Scaling) / 100, (int)(mi.WorkArea.top * si.Scaling) / 100, (int)(mi.WorkArea.right * si.Scaling) / 100, (int)(mi.WorkArea.bottom * si.Scaling) / 100); si.NativeArea = new Oblong((int)(mi.Monitor.left * si.Scaling) / 100, (int)(mi.Monitor.top * si.Scaling) / 100, (int)(mi.Monitor.right * si.Scaling) / 100, (int)(mi.Monitor.bottom * si.Scaling) / 100); Display.Screens.Add(si); } return(true); }
private static DEVMODE GetDeviceMode() { var mode = new DEVMODE(); mode.Initialize(); if (Win32Api.EnumDisplaySettings(null, Win32Api.ENUM_CURRENT_SETTINGS, ref mode)) { return(mode); } else { throw new InvalidOperationException(GetLastError()); } }
public static double getMonitorScaling(MonitorInfoEx monitor) { DEVMODE DeviceMode = new DEVMODE(); DeviceMode.Initialize(); if (EnumDisplaySettingsEx(ToLPTStr(monitor.DeviceName), -1, ref DeviceMode)) { //si.NativeHeight = DeviceMode.dmPelsHeight; //si.NativeWidth = DeviceMode.dmPelsWidth; return(Math.Round((double)DeviceMode.dmPelsHeight / (monitor.Monitor.bottom - monitor.Monitor.top), 2)); } return(1); }
public CResolution(uint w,uint h,Screen s) { Screen screen = s; uint iWidth = w; uint iHeight = h; DEVMODE dm = new DEVMODE(); dm.Initialize(); //dm.dmDeviceName = new String (new char[32]); //dm.dmFormName = new String (new char[32]); //dm.dmSize = (ushort)Marshal.SizeOf (dm); // make sure we can read current monitor settings if (User_32.EnumDisplaySettings (StringExtensions.ToLPTStr(screen.DeviceName), User_32.ENUM_CURRENT_SETTINGS, ref dm)) { dm.dmPelsWidth = iWidth; dm.dmPelsHeight = iHeight; // check to see if requested graphics mode can be set int iRet = User_32.ChangeDisplaySettingsEx (screen.DeviceName, ref dm, IntPtr.Zero, ChangeDisplaySettingsFlags.CDS_TEST, IntPtr.Zero); if (iRet == User_32.DISP_CHANGE_FAILED) { MessageBox.Show("DISP_CHANGE_FAILED", "FAILURE",MessageBoxButtons.OK,MessageBoxIcon.Information); } else { //have to use ChangeDisplaySettingsEx() instead of ChangeDisplaySettings() so we can specify device name (for multi-monitor support) iRet = User_32.ChangeDisplaySettingsEx(screen.DeviceName, ref dm, IntPtr.Zero, ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero); switch (iRet) { case User_32.DISP_CHANGE_SUCCESSFUL: { break; //successfull change } case User_32.DISP_CHANGE_RESTART: { MessageBox.Show("Resolution Change Requires Reboot.","Information",MessageBoxButtons.OK,MessageBoxIcon.Information); break; //windows 9x series you have to restart } default: { MessageBox.Show("Something has gone horribly wrong.","Unknown Failure",MessageBoxButtons.OK,MessageBoxIcon.Information); break; //failed to change } } } } }