Valid friendly monitor names with C# - c#

My main development machine is a laptop with 2 screens: an internal screen and an external Samsung monitor.
Generic PnP Monitor= 1366x768, Top: 0, Left: 1920 -> secondary display
SF350_S24F350FH / S24F352FH / S24F354FH (HDMI)= 1920x1080, Top: 0, Left: 0 -> main display
And my codes are:
Dispay.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
internal class Display
{
private Rectangle _bounds;
private DisplayOrientation _orientation;
private Rectangle _workingArea;
private string _name,_deviceId;
private static Display[] _displays;
public Rectangle Bounds
{
get
{
return _bounds;
}
}
public DisplayOrientation Orientation
{
get
{
return _orientation;
}
}
public Rectangle WorkingArea
{
get
{
return _workingArea;
}
}
public string DeviceId
{
get
{
return _deviceId;
}
}
public string Name
{
get
{
return _name;
}
}
public static DisplayImpl[] Displays
{
get
{
if (_displays == null) QueryDisplayDevices();
return _displays;
}
private static void QueryDisplayDevices()
{
List<Display> list = new List<Display>();
WinApi.MonitorEnumDelegate MonitorEnumProc = new WinApi.MonitorEnumDelegate((IntPtr hMonitor, IntPtr hdcMonitor, ref WinApi.RECT lprcMonitor, IntPtr dwData) => {
WinApi.MONITORINFOEX mi = new WinApi.MONITORINFOEX() { Size = Marshal.SizeOf(typeof(WinApi.MONITORINFOEX)) };
if (WinApi.GetMonitorInfo(hMonitor, ref mi))
{
WinApi.DISPLAY_DEVICE device = new WinApi.DISPLAY_DEVICE();
device.Initialize();
if (WinApi.EnumDisplayDevices(mi.DeviceName.ToLPTStr(), 0, ref device, 0))
{
Display display = new Display()
{
_name = device.DeviceString,
_deviceId = mi.DeviceName,
_bounds=new Rectangle(mi.Monitor.Left,mi.Monitor.Top,mi.Monitor.Right-mi.Monitor.Left,mi.Monitor.Bottom-mi.Monitor.Top),
_workingArea = new Rectangle(mi.WorkArea.Left, mi.WorkArea.Top, mi.WorkArea.Right - mi.WorkArea.Left, mi.WorkArea.Bottom - mi.WorkArea.Top),
};
list.Add(display);
}
}
return true;
});
WinApi.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, MonitorEnumProc, IntPtr.Zero);
_displays=list.ToArray();
}
}
}
WinApi.cs:
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
internal class WinApi
{
#region DISPLAY_DEVICE struct
[StructLayout(LayoutKind.Sequential)]
internal struct DISPLAY_DEVICE
{
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
public DisplayDeviceStateFlags StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
public void Initialize()
{
cb = 0;
DeviceName = new string((char)32, 32);
DeviceString = new string((char)32, 128);
DeviceID = new string((char)32, 128);
DeviceKey = new string((char)32, 128);
cb = Marshal.SizeOf(this);
}
}
#endregion
#region RECT struct
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
#endregion
#region MONITORINFOEX struct
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MONITORINFOEX
{
public int Size;
public RECT Monitor;
public RECT WorkArea;
public uint Flags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
}
#endregion
#region DisplayDeviceStateFlags enum
[Flags()]
public enum DisplayDeviceStateFlags : int
{
/// <summary>The device is part of the desktop.</summary>
AttachedToDesktop = 0x1,
MultiDriver = 0x2,
/// <summary>The device is part of the desktop.</summary>
PrimaryDevice = 0x4,
/// <summary>Represents a pseudo device used to mirror application drawing for remoting or other purposes.</summary>
MirroringDriver = 0x8,
/// <summary>The device is VGA compatible.</summary>
VGACompatible = 0x10,
/// <summary>The device is removable; it cannot be the primary display.</summary>
Removable = 0x20,
/// <summary>The device has more display modes than its output devices support.</summary>
ModesPruned = 0x8000000,
Remote = 0x4000000,
Disconnect = 0x2000000,
}
#endregion
public delegate bool MonitorEnumDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData);
[DllImport("user32.dll")]
public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX lpmi);
[DllImport("User32.dll")]
internal static extern bool EnumDisplayDevices(byte[] lpDevice, uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);
public static byte[] ToLPTStr(this string str)
{
var lptArray = new byte[str.Length + 1];
var index = 0;
foreach (char c in str.ToCharArray())
lptArray[index++] = Convert.ToByte(c);
lptArray[index] = Convert.ToByte('\0');
return lptArray;
}
}
Then I try to debug
if(Display.Displays !=null) { }
And I got these results:
Display 0:
Name: Generic PnP Monitor
DeviceId: \\\\.\\DISPLAY1
Bounds:
Top: 0
Left: 0
Width: 1920
Height: 1080
Display 1:
Name: SF350_S24F350FH / S24F352FH / S24F354FH (HDMI)
DeviceId: \\\\.\\DISPLAY2
Bounds:
Top: 0
Left: 1920
Width: 1366
Height: 768
Based on screen resolution and top-left values, Display 0 should be "SF350_S24F350FH / S24F352FH / S24F354FH (HDMI)", and why it got swapped with Display 1?

It depends on which monitor is defined as the main one. It does not matter the position of each monitor.
This library does the job very well if you want : WindowsDisplayAPI

According to the document of the EnumDisplayDevices
To obtain information on a display monitor, first call
EnumDisplayDevices with lpDevice set to NULL. Then call
EnumDisplayDevices with lpDevice set to
DISPLAY_DEVICE.DeviceName from the first call to
EnumDisplayDevices and with iDevNum set to zero. Then
DISPLAY_DEVICE.DeviceString is the monitor name.
The samples:
private static void QueryDisplayDevices()
{
DISPLAY_DEVICE device = new DISPLAY_DEVICE();
device.Initialize();
uint DispNum = 0;
while (EnumDisplayDevices(null, DispNum, ref device, 0))
{
DISPLAY_DEVICE dev = new DISPLAY_DEVICE();
dev.Initialize();
if (EnumDisplayDevices(device.DeviceName, 0, ref dev, 0))
{
Console.WriteLine("Device Name:" + dev.DeviceName);
Console.WriteLine("Monitor name:" + dev.DeviceString);
}
DispNum++;
device.Initialize();
}
}

With #Dmo 's clues, I use this library
Rectangle rect;
Display display;
foreach (PathInfo pi in PathInfo.GetActivePaths())
{
if (!pi.TargetsInfo[0].DisplayTarget.IsAvailable) continue;
rect=System.Windows.Forms.Screen.GetWorkingArea(new Rectangle(pi.Position, pi.Resolution));
display = new DisplayImpl()
{
_name = string.IsNullOrEmpty(pi.TargetsInfo[0].DisplayTarget.FriendlyName)? "Generic PnP Monitor" : pi.TargetsInfo[0].DisplayTarget.FriendlyName,
_deviceId = pi.DisplaySource.DisplayName,
_devicePath=pi.TargetsInfo[0].DisplayTarget.DevicePath,
_bounds = new Rectangle(pi.Position,pi.Resolution),
_workingArea = rect,
};
list.Add(display);
}
It produces the correct monitor name and settings pair! 👍

Related

Any windows api to rotate screen in c#? [duplicate]

I'm looking for a way to rotate the display of one of my monitors programmatically.
I have two monitors set up on a desk mount, and I often use them in varying orientations when programming or using various other programs, and in order to change the orientation I jump into display settings and rotate it that way (so i can have one or both in portrait).
Call me lazy as i know thats hardly any hassle to do, but it would be nice to just have a quick little executable on my taskbar that i can run to instantly rotate one of the screens back and forth when i need to.
I've looked into it and found various explanations using DEVMODE structs etc, and it all seems like a really long winded process, is there really not an easier way to do this? Not a windows dll that has this functionailty already?
Thanks in advance.
it's not that hard to achieve this using the mentioned DEVMODE struct and native calls.
I've written a little wrapper that does this:
using System;
using System.Runtime.InteropServices;
public class Display
{
public enum Orientations
{
DEGREES_CW_0 = 0,
DEGREES_CW_90 = 3,
DEGREES_CW_180 = 2,
DEGREES_CW_270 = 1
}
public static bool Rotate(uint DisplayNumber, Orientations Orientation)
{
if(DisplayNumber == 0)
throw new ArgumentOutOfRangeException("DisplayNumber", DisplayNumber, "First display is 1.");
bool result = false;
DISPLAY_DEVICE d = new DISPLAY_DEVICE();
d.cb = Marshal.SizeOf(d);
DEVMODE dm = new DEVMODE();
if(!NativeMethods.EnumDisplayDevices(null, DisplayNumber-1, ref d, 0))
throw new ArgumentOutOfRangeException("DisplayNumber", DisplayNumber, "Number is greater than connected displays.");
if (0 != NativeMethods.EnumDisplaySettings(
d.DeviceName, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm))
{
if ((dm.dmDisplayOrientation + (int)Orientation) % 2 == 1) // Need to swap height and width?
{
int temp = dm.dmPelsHeight;
dm.dmPelsHeight = dm.dmPelsWidth;
dm.dmPelsWidth = temp;
}
switch (Orientation)
{
case Orientations.DEGREES_CW_90:
dm.dmDisplayOrientation = NativeMethods.DMDO_270;
break;
case Orientations.DEGREES_CW_180:
dm.dmDisplayOrientation = NativeMethods.DMDO_180;
break;
case Orientations.DEGREES_CW_270:
dm.dmDisplayOrientation = NativeMethods.DMDO_90;
break;
case Orientations.DEGREES_CW_0:
dm.dmDisplayOrientation = NativeMethods.DMDO_DEFAULT;
break;
default:
break;
}
DISP_CHANGE ret = NativeMethods.ChangeDisplaySettingsEx(
d.DeviceName, ref dm, IntPtr.Zero,
DisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero);
result = ret == 0;
}
return result;
}
public static void ResetAllRotations()
{
try
{
uint i = 0;
while (++i <= 64)
{
Rotate(i, Orientations.DEGREES_CW_0);
}
}
catch(ArgumentOutOfRangeException ex)
{
// Everything is fine, just reached the last display
}
}
}
internal class NativeMethods
{
[DllImport("user32.dll")]
internal static extern DISP_CHANGE ChangeDisplaySettingsEx(
string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd,
DisplaySettingsFlags dwflags, IntPtr lParam);
[DllImport("user32.dll")]
internal static extern bool EnumDisplayDevices(
string lpDevice, uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice,
uint dwFlags);
[DllImport("user32.dll", CharSet = CharSet.Ansi)]
internal static extern int EnumDisplaySettings(
string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
public const int DMDO_DEFAULT = 0;
public const int DMDO_90 = 1;
public const int DMDO_180 = 2;
public const int DMDO_270 = 3;
public const int ENUM_CURRENT_SETTINGS = -1;
}
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/dd183565(v=vs.85).aspx
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
internal struct DEVMODE
{
public const int CCHDEVICENAME = 32;
public const int CCHFORMNAME = 32;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
[System.Runtime.InteropServices.FieldOffset(0)]
public string dmDeviceName;
[System.Runtime.InteropServices.FieldOffset(32)]
public Int16 dmSpecVersion;
[System.Runtime.InteropServices.FieldOffset(34)]
public Int16 dmDriverVersion;
[System.Runtime.InteropServices.FieldOffset(36)]
public Int16 dmSize;
[System.Runtime.InteropServices.FieldOffset(38)]
public Int16 dmDriverExtra;
[System.Runtime.InteropServices.FieldOffset(40)]
public DM dmFields;
[System.Runtime.InteropServices.FieldOffset(44)]
Int16 dmOrientation;
[System.Runtime.InteropServices.FieldOffset(46)]
Int16 dmPaperSize;
[System.Runtime.InteropServices.FieldOffset(48)]
Int16 dmPaperLength;
[System.Runtime.InteropServices.FieldOffset(50)]
Int16 dmPaperWidth;
[System.Runtime.InteropServices.FieldOffset(52)]
Int16 dmScale;
[System.Runtime.InteropServices.FieldOffset(54)]
Int16 dmCopies;
[System.Runtime.InteropServices.FieldOffset(56)]
Int16 dmDefaultSource;
[System.Runtime.InteropServices.FieldOffset(58)]
Int16 dmPrintQuality;
[System.Runtime.InteropServices.FieldOffset(44)]
public POINTL dmPosition;
[System.Runtime.InteropServices.FieldOffset(52)]
public Int32 dmDisplayOrientation;
[System.Runtime.InteropServices.FieldOffset(56)]
public Int32 dmDisplayFixedOutput;
[System.Runtime.InteropServices.FieldOffset(60)]
public short dmColor;
[System.Runtime.InteropServices.FieldOffset(62)]
public short dmDuplex;
[System.Runtime.InteropServices.FieldOffset(64)]
public short dmYResolution;
[System.Runtime.InteropServices.FieldOffset(66)]
public short dmTTOption;
[System.Runtime.InteropServices.FieldOffset(68)]
public short dmCollate;
[System.Runtime.InteropServices.FieldOffset(72)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
public string dmFormName;
[System.Runtime.InteropServices.FieldOffset(102)]
public Int16 dmLogPixels;
[System.Runtime.InteropServices.FieldOffset(104)]
public Int32 dmBitsPerPel;
[System.Runtime.InteropServices.FieldOffset(108)]
public Int32 dmPelsWidth;
[System.Runtime.InteropServices.FieldOffset(112)]
public Int32 dmPelsHeight;
[System.Runtime.InteropServices.FieldOffset(116)]
public Int32 dmDisplayFlags;
[System.Runtime.InteropServices.FieldOffset(116)]
public Int32 dmNup;
[System.Runtime.InteropServices.FieldOffset(120)]
public Int32 dmDisplayFrequency;
}
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct DISPLAY_DEVICE
{
[MarshalAs(UnmanagedType.U4)]
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
[MarshalAs(UnmanagedType.U4)]
public DisplayDeviceStateFlags StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
}
// See: https://msdn.microsoft.com/de-de/library/windows/desktop/dd162807(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential)]
internal struct POINTL
{
long x;
long y;
}
internal enum DISP_CHANGE : int
{
Successful = 0,
Restart = 1,
Failed = -1,
BadMode = -2,
NotUpdated = -3,
BadFlags = -4,
BadParam = -5,
BadDualView = -6
}
// http://www.pinvoke.net/default.aspx/Enums/DisplayDeviceStateFlags.html
[Flags()]
internal enum DisplayDeviceStateFlags : int
{
/// <summary>The device is part of the desktop.</summary>
AttachedToDesktop = 0x1,
MultiDriver = 0x2,
/// <summary>The device is part of the desktop.</summary>
PrimaryDevice = 0x4,
/// <summary>Represents a pseudo device used to mirror application drawing for remoting or other purposes.</summary>
MirroringDriver = 0x8,
/// <summary>The device is VGA compatible.</summary>
VGACompatible = 0x10,
/// <summary>The device is removable; it cannot be the primary display.</summary>
Removable = 0x20,
/// <summary>The device has more display modes than its output devices support.</summary>
ModesPruned = 0x8000000,
Remote = 0x4000000,
Disconnect = 0x2000000
}
// http://www.pinvoke.net/default.aspx/user32/ChangeDisplaySettingsFlags.html
[Flags()]
internal enum DisplaySettingsFlags : int
{
CDS_NONE = 0,
CDS_UPDATEREGISTRY = 0x00000001,
CDS_TEST = 0x00000002,
CDS_FULLSCREEN = 0x00000004,
CDS_GLOBAL = 0x00000008,
CDS_SET_PRIMARY = 0x00000010,
CDS_VIDEOPARAMETERS = 0x00000020,
CDS_ENABLE_UNSAFE_MODES = 0x00000100,
CDS_DISABLE_UNSAFE_MODES= 0x00000200,
CDS_RESET = 0x40000000,
CDS_RESET_EX = 0x20000000,
CDS_NORESET = 0x10000000
}
[Flags()]
internal enum DM : int
{
Orientation = 0x00000001,
PaperSize = 0x00000002,
PaperLength = 0x00000004,
PaperWidth = 0x00000008,
Scale = 0x00000010,
Position = 0x00000020,
NUP = 0x00000040,
DisplayOrientation = 0x00000080,
Copies = 0x00000100,
DefaultSource = 0x00000200,
PrintQuality = 0x00000400,
Color = 0x00000800,
Duplex = 0x00001000,
YResolution = 0x00002000,
TTOption = 0x00004000,
Collate = 0x00008000,
FormName = 0x00010000,
LogPixels = 0x00020000,
BitsPerPixel = 0x00040000,
PelsWidth = 0x00080000,
PelsHeight = 0x00100000,
DisplayFlags = 0x00200000,
DisplayFrequency = 0x00400000,
ICMMethod = 0x00800000,
ICMIntent = 0x01000000,
MediaType = 0x02000000,
DitherType = 0x04000000,
PanningWidth = 0x08000000,
PanningHeight = 0x10000000,
DisplayFixedOutput = 0x20000000
}
You can simply invoke the static Rotate-Function with the display number you want to rotate (Monitor 1=1, Monitor 2=2, etc.) and the degree you need.
Like this:
Display.Rotate(1, Display.Orientations.DEGREES_CW_180);
There is also a little short-hand function ResetAllRotations() to reset all displays.
Regards

How to change the gamma ramp of a single display monitor (NVidia Config)?

I try to change my gamma of just one screen and not all my screens.
I use this code to help me
But this SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref s_ramp);
Is for all devices.
[EDIT2] I saw one weird thing : SetDeviceGammaRamp is not the same gamma of the Nvidia Panel Controller (I tried to change my value of SetDeviceGammaRamp, and it's like if i changed the value of brightness and contrast in the Nvidia panel). So i think i must to use NVidia API :/
So, how can i change this code to put my gamma on my first screen, or my second, but not both
[EDIT1] This is what i made :
class Monitor
{
[DllImport("user32.dll")]
static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
public delegate int MonitorEnumProc(IntPtr hMonitor, IntPtr hDCMonitor, ref Rect lprcMonitor, IntPtr dwData);
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool GetMonitorInfo(IntPtr hmon, ref MonitorInfo mi);
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
/// <summary>
/// The struct that contains the display information
/// </summary>
public class DisplayInfo
{
public string Availability { get; set; }
public string ScreenHeight { get; set; }
public string ScreenWidth { get; set; }
public Rect MonitorArea { get; set; }
public Rect WorkArea { get; set; }
public IntPtr DC { get; set; }
}
[StructLayout(LayoutKind.Sequential)]
struct MonitorInfo
{
public uint size;
public Rect monitor;
public Rect work;
public uint flags;
}
/// <summary>
/// Collection of display information
/// </summary>
public class DisplayInfoCollection : List<DisplayInfo>
{
}
/// <summary>
/// Returns the number of Displays using the Win32 functions
/// </summary>
/// <returns>collection of Display Info</returns>
public DisplayInfoCollection GetDisplays()
{
DisplayInfoCollection col = new DisplayInfoCollection();
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
delegate (IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
{
MonitorInfo mi = new MonitorInfo();
mi.size = (uint)Marshal.SizeOf(mi);
bool success = GetMonitorInfo(hMonitor, ref mi);
if (success)
{
DisplayInfo di = new DisplayInfo();
di.ScreenWidth = (mi.monitor.right - mi.monitor.left).ToString();
di.ScreenHeight = (mi.monitor.bottom - mi.monitor.top).ToString();
di.MonitorArea = mi.monitor;
di.WorkArea = mi.work;
di.Availability = mi.flags.ToString();
di.DC = GetDC(hdcMonitor);
col.Add(di);
}
return 1;
}, IntPtr.Zero);
return col;
}
public Monitor()
{
}
}
And for SetDeviceGammaRamp, i made this :
GammaRamp gamma = new GammaRamp();
Monitor.DisplayInfoCollection monitors;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Monitor monitor = new Monitor();
monitors = monitor.GetDisplays();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
int value = trackBar1.Value;
gamma.SetValue(Convert.ToByte(value), monitors[1].DC);
}
GammaRamp class :
public void SetValue(byte value, IntPtr hdc)
{
Ramp gammaArray = new Ramp { Red = new ushort[256], Green = new ushort[256], Blue = new ushort[256] };
for (int i = 0; i < 256; i++)
{
gammaArray.Red[i] = gammaArray.Green[i] = gammaArray.Blue[i] = (ushort)Math.Min(i * (value + 128), ushort.MaxValue);
}
SetDeviceGammaRamp(hdc, ref gammaArray);
}
You can get the DC of another monitor by using EnumDisplayMonitors or GetMonitorInfo functions.
See complete explanations at HMONITOR and the Device Context.
EDIT
As explained in EnumDisplayMonitors,
pass IntPtr.Zero to hdc parameter (values encompasses all displays)
then in MONITORENUMPROC, hdcMonitor should contain the right DC for the current monitor being evaluated
then change your di.DC = GetDC(IntPtr.Zero); to di.DC = GetDC(hdcMonitor);
(passing Zero to GetDC will obviously specify all monitors, not what you want)
EDIT 2
Little confusion with the docs, in fact the 3rd type of call in the remarks of EnumDisplayMonitors should be performed:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow
{
private readonly List<IntPtr> _dcs = new List<IntPtr>();
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var hdc = NativeMethods.GetDC(IntPtr.Zero);
if (hdc == IntPtr.Zero)
throw new InvalidOperationException();
if (!NativeMethods.EnumDisplayMonitors(hdc, IntPtr.Zero, Monitorenumproc, IntPtr.Zero))
throw new InvalidOperationException();
if (NativeMethods.ReleaseDC(IntPtr.Zero, hdc) == 0)
throw new InvalidOperationException();
foreach (var monitorDc in _dcs)
{
// do something cool !
}
}
private int Monitorenumproc(IntPtr param0, IntPtr param1, ref tagRECT param2, IntPtr param3)
{
// optional actually ...
var info = new MonitorInfo {cbSize = (uint) Marshal.SizeOf<MonitorInfo>()};
if (!NativeMethods.GetMonitorInfoW(param0, ref info))
throw new InvalidOperationException();
_dcs.Add(param1); // grab DC for current monitor !
return 1;
}
}
public class NativeMethods
{
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
public static extern int ReleaseDC([In] IntPtr hWnd, [In] IntPtr hDC);
[DllImport("user32.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC([In] IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetMonitorInfoW")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetMonitorInfoW([In] IntPtr hMonitor, ref MonitorInfo lpmi);
[DllImport("user32.dll", EntryPoint = "EnumDisplayMonitors")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumDisplayMonitors([In] IntPtr hdc, [In] IntPtr lprcClip, MONITORENUMPROC lpfnEnum,
IntPtr dwData);
}
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int MONITORENUMPROC(IntPtr param0, IntPtr param1, ref tagRECT param2, IntPtr param3);
[StructLayout(LayoutKind.Sequential)]
public struct MonitorInfo
{
public uint cbSize;
public tagRECT rcMonitor;
public tagRECT rcWork;
public uint dwFlags;
}
[StructLayout(LayoutKind.Sequential)]
public struct tagRECT
{
public int left;
public int top;
public int right;
public int bottom;
}
}
You should be able to get per-monitor DC, (cannot 100% confirm since I only have one screen).
If all else fails then maybe that NVidia thing interferes somehow under the hood.

Available Resolutions For a specific Screen

I might have 2 monitor/screens connected to my machine.
I want to know all Avaliable resolutions for a specific screen (I have an instance of type System.Windows.Forms.Screen).
I've seen the following:
How to list available video modes using C#?
List of valid resolutions for a given Screen?
but they all give results for all monitors and not just a specific one. Any suggestions? Thanks!!!
Edit 1:
This is the info about my 2 screens:
In the first of those links you're told about EnumDisplaySettings. Take a few seconds to lookup that function and the FIRST PARAMETER is the
string that specifies the display device about whose graphics mode the
function will obtain information.
Here's a sample class to fetch information about displays. I've deliberately omitted DEVMODE since you've already got it.
public class NativeMethods
{
[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
[DllImport("user32.dll")]
public static extern bool EnumDisplayDevices(string deviceName, int modeNum, ref DISPLAY_DEVICE displayDevice, int flags);
}
[StructLayout(LayoutKind.Sequential)]
public struct DISPLAY_DEVICE
{
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
public int StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
}
static class Display
{
public static List<DISPLAY_DEVICE> GetGraphicsAdapters()
{
int i = 0;
DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE();
List<DISPLAY_DEVICE> result = new List<DISPLAY_DEVICE>();
displayDevice.cb = Marshal.SizeOf(displayDevice);
while (NativeMethods.EnumDisplayDevices(null, i, ref displayDevice, 1))
{
result.Add(displayDevice);
i++;
}
return result;
}
public static List<DISPLAY_DEVICE> GetMonitors(string graphicsAdapter)
{
DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE();
List<DISPLAY_DEVICE> result = new List<DISPLAY_DEVICE>();
int i = 0;
displayDevice.cb = Marshal.SizeOf(displayDevice);
while (NativeMethods.EnumDisplayDevices(graphicsAdapter, i, ref displayDevice, 0))
{
result.Add(displayDevice);
i++;
}
return result;
}
public static List<DEVMODE> GetDeviceModes(string graphicsAdapter)
{
int i = 0;
DEVMODE devMode = new DEVMODE();
List<DEVMODE> result = new List<DEVMODE>();
while (NativeMethods.EnumDisplaySettings(graphicsAdapter, i, ref devMode))
{
result.Add(devMode);
i++;
}
return result;
}
}

C# setting screen brightness Windows 7

I want ajust screen brightness by my self. Because Windows lets me only adjusting in limited range. I want dim the display from 0 to 100% and turning it off/on. It should be possible if windows can it do automatically (Dim display after: x minutes/Turn off display after: x minutes). I tried some sources and classes what I found by google. But no of them works.
Have you ever tried this or can you recommend me any working code?
Thanks for responds.
You can use the WmiSetBrightness method:
using System.Management;
//...
static void SetBrightness(byte targetBrightness) {
ManagementScope scope = new ManagementScope("root\\WMI");
SelectQuery query = new SelectQuery("WmiMonitorBrightnessMethods");
using(ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query)) {
using(ManagementObjectCollection objectCollection = searcher.Get()) {
foreach(ManagementObject mObj in objectCollection) {
mObj.InvokeMethod("WmiSetBrightness",
new Object[] { UInt32.MaxValue, targetBrightness });
break;
}
}
}
}
For more details, please take a look at Brightness Control in WDDM and Monitor Configuration Functions
try it like this:
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace MediaManagerSql.DataAccess.Sql.EntityFramework
{
public class ScreenBrightness : Component
{
private int _gammaValue;
private RAMP _ramp;
public ScreenBrightness()
{
InitializeComponent();
}
public ScreenBrightness(IContainer container)
{
container.Add(this);
InitializeComponent();
}
[Description("Brightness Gamma Value")]
[Category("Brightness")]
public int SetGammaValue
{
get { return _gammaValue; }
set { _gammaValue = value; }
}
[DllImport("gdi32.dll")]
public static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
/// <summary>
/// Apply the selected gamma value to screen
/// </summary>
public void ApplyGamma()
{
// since gamma value is max 44 ,, we need to take the percentage from this because
// it needed from 0 - 100%
double gValue = _gammaValue;
gValue = Math.Floor(Convert.ToDouble((gValue/2.27)));
_gammaValue = Convert.ToInt16(gValue);
if (_gammaValue != 0)
{
_ramp.Red = new ushort[256];
_ramp.Green = new ushort[256];
_ramp.Blue = new ushort[256];
for (int i = 1; i < 256; i++)
{
// gamma is a value between 3 and 44
_ramp.Red[i] =
_ramp.Green[i] =
_ramp.Blue[i] =
(ushort)
(Math.Min(65535, Math.Max(0, Math.Pow((i + 1)/256.0, (_gammaValue + 5)*0.1)*65535 + 0.5)));
}
SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref _ramp);
}
}
#region Nested type: RAMP
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct RAMP
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public UInt16[] Red;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public UInt16[] Green;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public UInt16[] Blue;
}
#endregion
}
}

.NET: How to place my window near the notification area (systray)?

I'd like to display a little popup window next to the notification area. It's similar to what Outlook/Skype/Live! Messenger/etc does when it displays the notification about a new message. In my case it will have some input controls (textbox, datetimepicker, buttons...) so a simple bubble won't do.
The trick is doing this correctly when the user has multiple monitors and/or the taskbar is not located at the bottom of the screen. I could not find any functions that would let me determine the position and orientation of the taskbar/notification area.
Use WinAPI calls to find the TaskBar position, and position your window according to it
C# Example
class Program
{
static void Main(string[] args)
{
Taskbar taskbar = new Taskbar();
Console.WriteLine("Position: {0}, AlwaysOnTop: {1}; AutoHide: {2}; Bounds: {3}", taskbar.Position, taskbar.AlwaysOnTop, taskbar.AutoHide, taskbar.Bounds);
Console.ReadLine();
}
}
public enum TaskbarPosition
{
Unknown = -1,
Left,
Top,
Right,
Bottom,
}
public sealed class Taskbar
{
private const string ClassName = "Shell_TrayWnd";
public Rectangle Bounds
{
get;
private set;
}
public TaskbarPosition Position
{
get;
private set;
}
public Point Location
{
get
{
return this.Bounds.Location;
}
}
public Size Size
{
get
{
return this.Bounds.Size;
}
}
//Always returns false under Windows 7
public bool AlwaysOnTop
{
get;
private set;
}
public bool AutoHide
{
get;
private set;
}
public Taskbar()
{
IntPtr taskbarHandle = User32.FindWindow(Taskbar.ClassName, null);
APPBARDATA data = new APPBARDATA();
data.cbSize = (uint) Marshal.SizeOf(typeof(APPBARDATA));
data.hWnd = taskbarHandle;
IntPtr result = Shell32.SHAppBarMessage(ABM.GetTaskbarPos, ref data);
if (result == IntPtr.Zero)
throw new InvalidOperationException();
this.Position = (TaskbarPosition) data.uEdge;
this.Bounds = Rectangle.FromLTRB(data.rc.left, data.rc.top, data.rc.right, data.rc.bottom);
data.cbSize = (uint) Marshal.SizeOf(typeof(APPBARDATA));
result = Shell32.SHAppBarMessage(ABM.GetState, ref data);
int state = result.ToInt32();
this.AlwaysOnTop = (state & ABS.AlwaysOnTop) == ABS.AlwaysOnTop;
this.AutoHide = (state & ABS.Autohide) == ABS.Autohide;
}
}
public enum ABM : uint
{
New = 0x00000000,
Remove = 0x00000001,
QueryPos = 0x00000002,
SetPos = 0x00000003,
GetState = 0x00000004,
GetTaskbarPos = 0x00000005,
Activate = 0x00000006,
GetAutoHideBar = 0x00000007,
SetAutoHideBar = 0x00000008,
WindowPosChanged = 0x00000009,
SetState = 0x0000000A,
}
public enum ABE : uint
{
Left = 0,
Top = 1,
Right = 2,
Bottom = 3
}
public static class ABS
{
public const int Autohide = 0x0000001;
public const int AlwaysOnTop = 0x0000002;
}
public static class Shell32
{
[DllImport("shell32.dll", SetLastError = true)]
public static extern IntPtr SHAppBarMessage(ABM dwMessage, [In] ref APPBARDATA pData);
}
public static class User32
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
[StructLayout(LayoutKind.Sequential)]
public struct APPBARDATA
{
public uint cbSize;
public IntPtr hWnd;
public uint uCallbackMessage;
public ABE uEdge;
public RECT rc;
public int lParam;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
You need to get the actual location of your notification icon, and place your pop-up window near that (or wherever you like).
You need to translate your XY locations relative to desktop(s). AFAIK, there are no direct function, even in Win32 API which can directly give you the answer.
These sites will help you-
1. http://forum.codecall.net/managed-c/262-dual-monitors-window-position.html
2. http://msdn.microsoft.com/en-us/magazine/cc188759.aspx

Categories

Resources