代码之家  ›  专栏  ›  技术社区  ›  Steven

使用C调整屏幕亮度#

c#
  •  10
  • Steven  · 技术社区  · 15 年前

    如何在C中调整屏幕亮度?

    谢谢。

    4 回复  |  直到 8 年前
        1
  •  10
  •   Hans Olsson    10 年前

    看看 SetDeviceGammaRamp API函数。这里有一篇代码项目文章描述了如何使用它: Setting Screen Brightness in C#

    不过,请注意,您的图形卡必须支持这一点,我想大多数现代图形卡都支持,但我不知道。

    编辑:由于codeproject文章似乎已关闭,另一个从c找到如何调用它的位置位于 pInvoke site .

        2
  •  2
  •   uTILLIty    10 年前

    刚刚找到了 SetMonitorBrightness 功能打开 MSDN .

        3
  •  1
  •   cricardol    11 年前

    这可以调整屏幕的真实亮度,但并非所有地方都支持:

    http://www.codeproject.com/Articles/236898/Screen-Brightness-Control-for-Laptops-and-Tablets

        4
  •  1
  •   haoming weng    8 年前

    实际上你可以用 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