c# - 如何更改单个显示器 (NVidia Config) 的 Gamma 斜坡?

标签 c# screen nvidia

我尝试只更改一个屏幕而不是所有屏幕的 Gamma 值。

我使用 this code帮助我

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

[EDIT2] 我看到一件奇怪的事情:SetDeviceGammaRamp 与 Nvidia Panel Controller 的 Gamma 不同(我试图改变我的 SetDeviceGammaRamp 值,就像我改变了 Nvidia 面板中亮度和对比度的值一样)。所以我想我必须使用 NVidia API :/

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

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

最佳答案

您可以使用EnumDisplayMonitors 获取另一台显示器的DC或 GetMonitorInfo功能。

请参阅 HMONITOR and the Device Context 上的完整说明.

编辑

EnumDisplayMonitors 中所述,

  • IntPtr.Zero 传递给 hdc 参数(值包含所有显示器)
  • 然后在MONITORENUMPROC , hdcMonitor 应包含当前正在评估的监视器的正确 DC
  • 然后将您的 di.DC = GetDC(IntPtr.Zero); 更改为 di.DC = GetDC(hdcMonitor);

(将 Zero 传递给 GetDC 显然会指定所有监视器,而不是您想要的)

编辑 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 的东西可能会以某种方式干扰。

关于c# - 如何更改单个显示器 (NVidia Config) 的 Gamma 斜坡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34486842/

相关文章:

c# - 更新行时如何解决DbUpdateConcurrencyException?

c# - 使用 CreateInstance 对表格单元格内容进行 Specflow 步骤参数转换

Android:创建持续闹钟(例如:早上闹钟、来电)

java - 把触摸屏分成4个象限?

c++ - 我在 SFML sf::RenderWindow 中只有黑屏

r - 无法初始化 native 深度学习后端: null

c# - 如何在c#中捕获准确的执行时间

c# - 使用存储库模式 - 出于 EF 性能原因选择实体的特定字段

c++ - 在抛出 'cl::sycl::detail::exception_implementation<(cl::sycl::detail::exception_types)9>' 实例后调用终止

opengl - 简单的 opengl 代码(渲染到纹理)适用于 Mac,但不适用于 ubuntu