using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
public static class DisplayFusionFunction
{
public static void Run(IntPtr windowHandle)
{
Application.EnableVisualStyles();
Application.Run(new MainForm(windowHandle));
}
}
public class MainForm : Form
{
private readonly BrightnessControl monitorControl;
public MainForm(IntPtr hWnd)
{
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(315, 140);
StartPosition = FormStartPosition.CenterScreen;
FormBorderStyle = FormBorderStyle.Fixed3D;
MaximizeBox = false;
MinimizeBox = false;
Text = "DDC/CI Monitor Control";
var butt = new Button
{
Location = new Point(230, 110),
Size = new Size(75, 23),
Text = "OK"
};
butt.Click += (s, e) => Dispose();
Controls.Add(butt);
monitorControl = new BrightnessControl(hWnd);
var monitorCount = monitorControl.GetMonitors();
for (var i = 0; i < monitorCount; ++i)
{
TrackBarBright(i);
//Comment next line if you don't want to use Contrast
TrackBarContrast(i);
}
}
// Brightness
private void TrackBarBright(int monitorNumber)
{
var mInfo = monitorControl.GetBrightnessCapabilities(monitorNumber);
// Check monitor support DDC/CI
if (mInfo.Current == -1)
{
BFS.Dialog.ShowMessageInfo("Error. One of your monitors does not support DDC/CI");
return;
}
var trackBarB = new TrackBar
{
Name = "M" + monitorNumber,
Location = new Point(5, 20),
Size = new Size(300, 40),
Minimum = mInfo.Minimum,
Maximum = mInfo.Maximum,
Value = mInfo.Current
};
var labelB = new Label
{
Location = new Point(10, 5),
Size = new Size(60, 15),
Text = "Brightness: "
};
var labelBValue = new Label
{
Location = new Point(70, 5),
Size = new Size(25, 15),
Text = trackBarB.Value.ToString()
};
trackBarB.ValueChanged += (s, e) =>
{
monitorNumber = int.Parse(trackBarB.Name.Substring(1));
if (monitorControl == null) return;
monitorControl.SetBrightness((short)trackBarB.Value, monitorNumber);
labelBValue.Text = trackBarB.Value.ToString();
};
Controls.Add(labelB);
Controls.Add(labelBValue);
Controls.Add(trackBarB);
}
// Contrast
private void TrackBarContrast(int monitorNumber)
{
var mInfo = monitorControl.GetContrastCapabilities(monitorNumber);
var trackBarC = new TrackBar
{
Name = "M" + monitorNumber,
Location = new Point(5, 80),
Size = new Size(300, 40),
Minimum = mInfo.Minimum,
Maximum = mInfo.Maximum,
Value = mInfo.Current
};
var labelC = new Label
{
Location = new Point(10, 65),
Size = new Size(60, 15),
Text = "Contrast: "
};
var labelCValue = new Label
{
Location = new Point(70, 65),
Size = new Size(25, 15),
Text = trackBarC.Value.ToString()
};
trackBarC.ValueChanged += (s, e) =>
{
monitorNumber = int.Parse(trackBarC.Name.Substring(1));
if (monitorControl == null) return;
monitorControl.SetContrast((short)trackBarC.Value, monitorNumber);
labelCValue.Text = trackBarC.Value.ToString();
};
Controls.Add(labelC);
Controls.Add(labelCValue);
Controls.Add(trackBarC);
}
}
public class BrightnessInfo
{
public int Minimum { get; set; }
public int Maximum { get; set; }
public int Current { get; set; }
}
public static class NativeConstants
{
public const int MonitorDefaultToNull = 0;
public const int MonitorDefaultToPrimary = 1;
public const int MonitorDefaultToNearest = 2;
}
public static class NativeStructures
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct PhysicalMonitor
{
public IntPtr hPhysicalMonitor;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szPhysicalMonitorDescription;
}
public enum McDisplayTechnologyType
{
McShadowMaskCathodeRayTube,
McApertureGrillCathodeRayTube,
McThinFilmTransistor,
McLiquidCrystalOnSilicon,
McPlasma,
McOrganicLightEmittingDiode,
McElectroluminescent,
McMicroelectromechanical,
McFieldEmissionDevice,
}
}
public static class NativeCalls
{
[DllImport("user32.dll")]
public static extern IntPtr MonitorFromWindow([In] IntPtr hwnd, uint dwFlags);
[DllImport("dxva2.dll")]
public static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);
[DllImport("dxva2.dll")]
public static extern bool GetPhysicalMonitorsFromHMONITOR(IntPtr hMonitor,
uint dwPhysicalMonitorArraySize, [Out] NativeStructures.PhysicalMonitor[] pPhysicalMonitorArray);
[DllImport("dxva2.dll")]
public static extern bool DestroyPhysicalMonitors(uint dwPhysicalMonitorArraySize,
[Out] NativeStructures.PhysicalMonitor[] pPhysicalMonitorArray);
[DllImport("dxva2.dll")]
public static extern bool GetMonitorTechnologyType(IntPtr hMonitor,
ref NativeStructures.McDisplayTechnologyType pdtyDisplayTechnologyType);
[DllImport("dxva2.dll")]
public static extern bool GetMonitorCapabilities(IntPtr hMonitor, ref uint pdwMonitorCapabilities,
ref uint pdwSupportedColorTemperatures);
[DllImport("dxva2.dll")]
public static extern bool SetMonitorBrightness(IntPtr hMonitor, short brightness);
[DllImport("dxva2.dll")]
public static extern bool SetMonitorContrast(IntPtr hMonitor, short contrast);
[DllImport("dxva2.dll")]
public static extern bool GetMonitorBrightness(IntPtr hMonitor, ref short pdwMinimumBrightness,
ref short pdwCurrentBrightness, ref short pdwMaximumBrightness);
[DllImport("dxva2.dll")]
public static extern bool GetMonitorContrast(IntPtr hMonitor, ref short pwdMinimumContrast,
ref short pwdCurrentContrast, ref short pwdMaximumContrast);
}
public class BrightnessControl
{
private readonly IntPtr hWnd;
private NativeStructures.PhysicalMonitor[] pPhysicalMonitorArray;
private uint pdwNumberOfPhysicalMonitors;
public BrightnessControl(IntPtr hWnd)
{
hWnd = hWnd;
SetupMonitors();
}
private void SetupMonitors()
{
var hMonitor = NativeCalls.MonitorFromWindow(hWnd, NativeConstants.MonitorDefaultToPrimary);
var numberOfPhysicalMonitorsFromHmonitor = NativeCalls.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, ref pdwNumberOfPhysicalMonitors);
pPhysicalMonitorArray = new NativeStructures.PhysicalMonitor[pdwNumberOfPhysicalMonitors];
var physicalMonitorsFromHmonitor = NativeCalls.GetPhysicalMonitorsFromHMONITOR(hMonitor, pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray);
}
private void GetMonitorCapabilities(int monitorNumber)
{
var pdwMonitorCapabilities = 0u;
var pdwSupportedColorTemperatures = 0u;
var monitorCapabilities = NativeCalls.GetMonitorCapabilities(pPhysicalMonitorArray[monitorNumber].hPhysicalMonitor,
ref pdwMonitorCapabilities, ref pdwSupportedColorTemperatures);
var type = NativeStructures.McDisplayTechnologyType.McShadowMaskCathodeRayTube;
var monitorTechnologyType = NativeCalls.GetMonitorTechnologyType(pPhysicalMonitorArray[monitorNumber].hPhysicalMonitor, ref type);
}
public bool SetBrightness(short brightness, int monitorNumber)
{
var brightnessWasSet = NativeCalls.SetMonitorBrightness(pPhysicalMonitorArray[monitorNumber].hPhysicalMonitor, brightness);
return brightnessWasSet;
}
public BrightnessInfo GetBrightnessCapabilities(int monitorNumber)
{
short current = -1, minimum = -1, maximum = -1;
var getBrightness = NativeCalls.GetMonitorBrightness(pPhysicalMonitorArray[monitorNumber].hPhysicalMonitor, ref minimum, ref current, ref maximum);
return new BrightnessInfo { Minimum = minimum, Maximum = maximum, Current = current };
}
public bool SetContrast(short contrast, int monitorNumber)
{
var contrastWasSet = NativeCalls.SetMonitorContrast(pPhysicalMonitorArray[monitorNumber].hPhysicalMonitor, contrast);
return contrastWasSet;
}
public BrightnessInfo GetContrastCapabilities(int monitorNumber)
{
short current = -1, minimum = -1, maximum = -1;
var getContrast = NativeCalls.GetMonitorContrast(pPhysicalMonitorArray[monitorNumber].hPhysicalMonitor, ref minimum, ref current, ref maximum);
return new BrightnessInfo { Minimum = minimum, Maximum = maximum, Current = current };
}
public uint GetMonitors() { return pdwNumberOfPhysicalMonitors; }
}