您的位置:首页 > 编程语言 > C#

C# 改变屏幕亮度 张宇轩

2010-08-29 03:12 344 查看
using System;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;

namespace wgscd
{
public static class Win32
{
[DllImport("gdi32.dll")]
private unsafe static extern bool SetDeviceGammaRamp(Int32 hdc, void* ramp);

private static bool initialized = false;
private static Int32 hdc;

private static void InitializeClass()
{
if (initialized)
return;

//Get the hardware device context of the screen, we can do
//this by getting the graphics object of null (IntPtr.Zero)
//then getting the HDC and converting that to an Int32.
hdc = Graphics.FromHwnd(IntPtr.Zero).GetHdc().ToInt32();

initialized = true;
}

public static unsafe bool SetBrightness(short 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++;
}
}

//For some reason, this always returns false?
bool retVal = SetDeviceGammaRamp(hdc, gArray);

//Memory allocated through stackalloc is automatically free'd
//by the CLR.

return retVal;

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: