如何更改单个显示器的伽玛斜坡(NVidia Config)?

2024-03-05

我尝试仅更改一个屏幕而不是所有屏幕的伽玛值。

I use 这段代码 http://devadd.com/2010/10/too-bright-too-early/帮我

但是这个SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref s_ramp);适用于所有设备。

[EDIT2]我看到一件奇怪的事情:SetDeviceGammaRamp 与Nvidia 面板控制器 http://international.download.nvidia.com/geforce-com/international/images/geforce-garage/how-to-calibrate-your-monitor/nvidia-control-panel-color-adjustment.png(我尝试更改 SetDeviceGammaRamp 的值,就像更改 Nvidia 面板中的亮度和对比度值一样)。所以我想我必须使用 NVidia API :/

那么,我如何更改此代码以将伽玛放在我的第一个屏幕或第二个屏幕上,但不能同时显示在两个屏幕上

[EDIT1]这就是我做的:

 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()
    {

    }
}

对于 SetDeviceGammaRamp,我做了这个:

    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 类:

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);
    }

您可以使用以下方法获取另一台显示器的 DC枚举显示监视器 https://msdn.microsoft.com/en-us/library/windows/desktop/dd162610(v=vs.85).aspx or 获取监控信息 https://msdn.microsoft.com/en-us/library/windows/desktop/dd144901(v=vs.85).aspx功能。

完整解释请参见HMONITOR 和设备上下文 https://msdn.microsoft.com/en-us/library/windows/desktop/dd144968(v=vs.85).aspx.

EDIT

正如中所解释的枚举显示监视器 https://msdn.microsoft.com/en-us/library/windows/desktop/dd162610(v=vs.85).aspx,

  • pass IntPtr.Zero to hdc参数(值涵盖所有显示)
  • then in 监控数字过程 https://msdn.microsoft.com/en-us/library/windows/desktop/dd145061(v=vs.85).aspx, hdcMonitor应包含当前正在评估的监视器的正确 DC
  • 然后改变你的di.DC = GetDC(IntPtr.Zero); to di.DC = GetDC(hdcMonitor);

(通过Zero to GetDC显然会指定所有监视器,而不是您想要的)

EDIT 2

与文档很少混淆,实际上应该执行 EnumDisplayMonitors 注释中的第三种类型的调用:

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;
    }
}

您应该能够获得每个显示器的 DC(无法 100% 确认,因为我只有一个屏幕)。

如果其他一切都失败了,那么也许 NVidia 的东西会在幕后以某种方式干扰。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何更改单个显示器的伽玛斜坡(NVidia Config)? 的相关文章