Exemple #1
0
        public Monitor(IntPtr hMonitor, IntPtr hdcMonitor)
        {
            _hMonitor   = hMonitor;
            _hdcMonitor = hdcMonitor;

            Log.Info($"Create monitor hMonitor:{_hMonitor} hdcMonitor:{_hdcMonitor}");

            // Get the current brightness
            try
            {
                SupportBrightness = true;

                if (!MonitorMethods.GetNumberOfPhysicalMonitorsFromHMONITOR(_hMonitor, ref _physicalMonitorsCount))
                {
                    throw new ApplicationException($"Cannot GetNumberOfPhysicalMonitorsFromHMONITOR for hmonitor {_hMonitor}");
                }

                _physicalMonitors = new MonitorStructures.PhysicalMonitor[_physicalMonitorsCount];

                if (!MonitorMethods.GetPhysicalMonitorsFromHMONITOR(_hMonitor, _physicalMonitorsCount, _physicalMonitors))
                {
                    throw new ApplicationException($"Cannot GetPhysicalMonitorsFromHMONITOR for hmonitor {_hMonitor}");
                }

                InitialBrightness = GetBrightness();

                foreach (var monitor in _physicalMonitors)
                {
                    Log.Info($"Monitor physicalmonitor handle:{monitor.hPhysicalMonitor} description:{monitor.szPhysicalMonitorDescription} with brightness {InitialBrightness}");
                }
            }
            catch (Exception err)
            {
                Log.Log(LogLevel.Error, err, "Unable to get brightness. Brightness will not be supported.");
                SupportBrightness = false;
            }

            // Get the current gamma Ramp
            try
            {
                _linearGammaRamp = GetDefaultGammaRamp();
                SetDeviceGammaRamp(_hdcMonitor, ref _linearGammaRamp);
            }
            catch (Exception err)
            {
                Log.Log(LogLevel.Error, err, "Unable to get base gamma ramp");
            }
        }
Exemple #2
0
        public static List <Monitor> Create()
        {
            var result = new List <Monitor>();

            var zeroDc = MonitorMethods.GetDC(IntPtr.Zero);

            MonitorMethods.EnumDisplayMonitors(zeroDc, IntPtr.Zero, (IntPtr hMonitor, IntPtr hdcMonitor, ref Rectangle lprcMonitor, IntPtr dwData) =>
            {
                hdcMonitor = zeroDc; // Multi monitor bug? Can't get/set device gamma ramp with given hdcMonitor

                result.Add(new Monitor(hMonitor, hdcMonitor));
                return(true);
            }, IntPtr.Zero);

            return(result);
        }
Exemple #3
0
        public void SetBrightness(int brightness)
        {
            if (!SupportBrightness)
            {
                return;
            }

            uint ubrightness = (uint)brightness;

            ubrightness = Math.Min(ubrightness, Math.Max(0, ubrightness));
            ubrightness = (MaxBrightness - MinBrightness) * ubrightness / 100u + MinBrightness;
            foreach (var physicalMonitor in _physicalMonitors)
            {
                Log.Info($"Set brightness {physicalMonitor.hPhysicalMonitor} to {brightness}");

                MonitorMethods.SetMonitorBrightness(physicalMonitor.hPhysicalMonitor, ubrightness);
            }
        }
Exemple #4
0
        public int GetBrightness()
        {
            if (!SupportBrightness)
            {
                return(100);
            }

            uint brightness    = 0;
            uint minBrightness = 0;
            uint maxBrightness = 0;

            if (!MonitorMethods.GetMonitorBrightness(_physicalMonitors[0].hPhysicalMonitor, ref minBrightness, ref brightness, ref maxBrightness))
            {
                throw new ApplicationException("Unable to retrieve brightness through GetMonitorBrightness call.");
            }

            Log.Info($"Brightness {brightness} Max {maxBrightness} min {minBrightness}");

            MinBrightness = minBrightness;
            MaxBrightness = maxBrightness;
            return((int)brightness);
        }