c# DXVA2下使用GetVCPfeature / SetVCPfeature

DXVA2提供API GetVCPFeatureAndVCPFeatureReply SetVCPfeature 2個函式可用來進行DDC/CI溝通

配合上一章 C# 使用 DXVA2 取得/設定 外接螢幕亮度 的程式再加入下列內容


EXE執行檔下載

DXVA2

// ====== DVXA2 ======
[DllImport("dxva2.dll", SetLastError = true)]
public static extern bool SetVCPFeature(
    IntPtr hPhysicalMonitor,
    byte bVCPCode,
    uint dwNewValue);

[DllImport("dxva2.dll", SetLastError = true)]
static extern bool GetVCPFeatureAndVCPFeatureReply(
    IntPtr hMonitor, byte bVCPCode,
    out uint pvct, out uint pdwCurrentValue, out uint pdwMaximumValue);

API

// ====== API ======
private void SetVCPfeature(IntPtr hMonitor, byte vcp, uint value)
{
    if (!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, out uint count) || count == 0)
        return;

    PHYSICAL_MONITOR[] pms = new PHYSICAL_MONITOR[count];

    if (GetPhysicalMonitorsFromHMONITOR(hMonitor, count, pms))
    {
        for (int i = 0; i < count; i++)
        {
            if(SetVCPFeature(
                pms[i].hPhysicalMonitor,
                vcp,
                value))
            {
                Console.WriteLine($"Set VCP:0x{vcp:X} success!");
            }
            else
            {
                Console.WriteLine($"Not Support");
            }
        }

        DestroyPhysicalMonitors(count, pms);
    }
}

public static bool GetVCPFeature(
    IntPtr hMonitor,
    byte vcpCode,
    out uint currentValue,
    out uint maximumValue)
{
    bool result = true;
    currentValue = 0;
    maximumValue = 0;

    GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, out uint count);
    PHYSICAL_MONITOR[] physicalMonitors = new PHYSICAL_MONITOR[count];
    GetPhysicalMonitorsFromHMONITOR(hMonitor, count, physicalMonitors);

    foreach (var pm in physicalMonitors)
    {
        if (GetVCPFeatureAndVCPFeatureReply(pm.hPhysicalMonitor, vcpCode,
            out uint vct, out currentValue, out maximumValue))
        {
            Console.WriteLine($"Brightness: {currentValue} / {maximumValue}");
            result = true;
        }
        else
        {
            result = false;
        }
    }
    return result;
}

執行

private void button5_Click(object sender, EventArgs e)
{
    byte vcp = Convert.ToByte(textBox3.Text, 16);
    uint value = Convert.ToUInt32(textBox4.Text);

    int index = comboBox1.SelectedIndex;
    IntPtr hMonitor = monitors[index].hMonitor;
    SetVCPfeature(hMonitor, vcp, value);
}

private void button6_Click(object sender, EventArgs e)
{
    byte vcp = Convert.ToByte(textBox3.Text, 16);
    uint value = 0;
    uint maxvalue = 0;

    int index = comboBox1.SelectedIndex;
    IntPtr hMonitor = monitors[index].hMonitor;

    if (GetVCPFeature(hMonitor, vcp, out value, out maxvalue))
    {
        textBox5.Text = value.ToString();
    }
    else
    {
        textBox5.Text = "Fail";
    }
}

1. 點擊 btn1_Search,選擇 DISPLAY2 (通常 DISPLAY1 為筆電內建螢幕),程式內會選擇對應的 hMonitor

2. btn5_SetVCPfeature 對 VCP 0x10 (亮度)做調整

3. btn6_GetVCPfeature 讀取 VCP 0x10 (亮度)數值(

留言