2010-10-25 104 views

回答

10

看看SetDeviceGammaRamp API函数。有一篇CodeProject文章描述了如何在C#中使用它:Setting Screen Brightness in C#

请注意,尽管您的显卡必须支持此功能,但我认为大多数现代设备都支持此功能,但我不知道。

编辑:由于CodeProject文章似乎已关闭,另一个地方找到如何从C#调用它在pInvoke site

+3

这不是真正的亮度。这是伽玛。这只是一个亮度错觉 – cricardol 2014-11-28 14:55:58

+1

那么,CodeProject链接已经死了。使用CodeProject作为CodeProject,这可能不是一件坏事。 – IInspectable 2016-06-07 14:00:41

+0

@IInspectable谢谢,用另一个链接更新了答案。 – 2016-06-07 15:33:27

2

刚刚发现的SetMonitorBrightness功能上MSDN

+1

你是否设法实现这个功能?这里提到的其他方法在任何情况下都不起作用,特别是在Windows 10下连接两个视频卡(使用PCI-E和旧版PCI插槽)的情况下。 – 2016-12-24 09:20:10

0

实际上,您可以使用SetDeviceGammaRamp()来设置C#中的屏幕亮度。

创建一个新的Windows窗体应用程序并复制以下代码。只需将一个轨迹条和一个按钮拖动到窗口中即可。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
namespace brightnesscontrol 
{ 
    public partial class Form1 : Form 
    { 
     [DllImport("gdi32.dll")] 
     private unsafe static extern bool SetDeviceGammaRamp(Int32 hdc, void* ramp); 
     private static bool initialized = false; 
     private static Int32 hdc; 
     private static int a; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private static void InitializeClass() 
     { 
      if (initialized) 
       return; 
      hdc = Graphics.FromHwnd(IntPtr.Zero).GetHdc().ToInt32(); 
      initialized = true; 
     } 
     public static unsafe bool SetBrightness(int brightness) 
     { 
      InitializeClass(); 
      if (brightness > 255) 
       brightness = 255; 
      if (brightness < 0) 
       brightness = 0; 
      short* gArray = stackalloc short[3 * 256]; 
      short* idx = gArray; 
      for (int j = 0; j < 3; j++) 
      { 
       for (int i = 0; i < 256; i++) 
       { 
        int arrayVal = i * (brightness + 128); 
        if (arrayVal > 65535) 
         arrayVal = 65535; 
        *idx = (short)arrayVal; 
        idx++; 
       } 
      } 
      bool retVal = SetDeviceGammaRamp(hdc, gArray); 
      return retVal; 
     } 
     private void trackBar1_Scroll(object sender, EventArgs e) 
     { 
     } 
     private void button1_Click(object sender, EventArgs e) 
     { 
      a = trackBar1.Value; 
      SetBrightness(a); 
     } 
    } 
} 

也许您需要更改滚动条的最大值和最小值。

你可以在这里按照教程。更多图片和详情:http://www.lattepanda.com/topic-f11t3020.html?sid=f9dc5d65cd4f2feb3c91ca41196c087e