using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;//必须引用此项
//警告:实验此代码可能给你的显示器的显示带来问题 ,花了两个小时查API的用法
// 不过我试过是没有事情的(BS下自己:不是用自己的机子)
namespace WindowsApplication1
...{
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
...{
SetGamma(trackBar1.Value);
}
[DllImport("gdi32.dll")]
public static extern int GetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);
RAMP ramp = new RAMP();
[DllImport("gdi32.dll")]
public static extern int SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hWnd);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct RAMP
...{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public UInt16[] Red;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public UInt16[] Green;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public UInt16[] Blue;
}
void SetGamma(int gamma)
...{
ramp.Red = new ushort[256];
ramp.Green = new ushort[256];
ramp.Blue = new ushort[256];
for (int i = 1; i < 256; i++)
...{
// gamma 必须在3和44之间
ramp.Red[i] = ramp.Green[i] = ramp.Blue[i] = (ushort)(Math.Min(65535, Math.Max(0, Math.Pow((i + 1) / 256.0, gamma * 0.1) * 65535 + 0.5)));
}
SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref ramp);
}
<br />using System;<br />using System.Collections.Generic;<br />using System.ComponentModel;<br />using System.Data;<br />using System.Drawing;<br />using System.Text;<br />using System.Windows.Forms;<br />using System.Runtime.InteropServices;//必须引用此项<br /
有2种方法,一是直接调用
API
,二是修改注册表
直接调用
API
,需要设备厂家的手册,或者查看注册表的 HKEY_LOCAL_MACHINE\\Drivers\\BuiltIn\\BackLite 项
修改注册表,注册表的键值也是不同的,根据设备来定
废话不多说,直接上代码
注册表方式
using Microsoft.Win32;
using
Github:https://github.com/CHNMaxGor/AjustScreenBrightness
方法一: 使用网上常说的 Gdi32.dll 下的 SetDeviceGammaRamp (修改系统Gamma)
DllImport("gdi32.dll")]
public static extern bool GetDeviceGammaRamp(In...
函数功能:该函数检索一指定窗口的客户区域或整个
屏幕
的显示设备上下文环境的句柄,以后可以在GDI函数中使用该句柄来在设备上下文环境中绘图。
GetDCEx函数是GetDC的一个扩展,它能使应用程序更多地控制在客户区域内如何或是否发生剪切。
函数原型:HDC GetDC(HWND hWnd);
hWnd:设备上下文环境被检索的窗口的句柄,如果该值为NULL,GetDC则检索整个
屏幕
的设
C#
读取和
调节
屏幕
亮度
,注意是
亮度
,不是对比度!
Object
Query query = new
Object
Query("SELECT * FROM WmiMonitorBrightness");//读取
new
Object
[] { UInt32.MaxValue, targetBrightness });//设置
从.NET平台调用Win32
API
Win32
API
可以直接控制Microsoft Windows的核心,因为
API
(Application Programming Interface)本来就是微软留给我们直接控制Windows的接口。
一. 基础知识
Win32
API
是C语言(注意,不是C++语言,尽管C语言是C++语言的子集)函数集。
C is a high-level programming language that was originally developed at Bell Labs by Dennis Ritchie in the early 1970s. It is a general-purpose language that is widely used in system programming, embedded systems, and the development of various applications.
C is a
struct
ured programming language and supports a wide range of programming con
struct
s such as loops, functions, arrays, pointers, and conditional statements. It is a compiled language, which means that the source code is compiled into machine code before execution.
Some of the features of C include its simplicity, efficiency, and portability. It is often used as the basis for many other programming languages, such as C++, Java, and Python.
C has a large and active community of developers who contribute to its development and support. It has been used to develop many important systems and applications, including operating systems, databases, and scientific applications.