2011-11-19 2 views
7

私の自己によって画面の明るさを欲しいです。 Windowsでは制限された範囲でしか調整できません。 0〜100%の表示を薄暗くし、それをオフ/オンにしたい。ウィンドウが自動的に実行できるかどうかを確認することができます(x分後に表示を消す/ x分後に表示をオフにする)。私はいくつかのソースとクラスを私がgoogleで見つけたものを試しました。しかし、彼らの作品はありません。C#設定画面の明るさWindows 7

これまでにこれを試したことがありますか、または私に働くコードを教えてもらえますか?

ありがとうございました。

答えて

15

あなたはWmiSetBrightnessメソッドを使用できます。詳細については

using System.Management; 
//... 
static void SetBrightness(byte targetBrightness) { 
    ManagementScope scope = new ManagementScope("root\\WMI"); 
    SelectQuery query = new SelectQuery("WmiMonitorBrightnessMethods"); 
    using(ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query)) { 
     using(ManagementObjectCollection objectCollection = searcher.Get()) { 
      foreach(ManagementObject mObj in objectCollection) { 
       mObj.InvokeMethod("WmiSetBrightness", 
        new Object[] { UInt32.MaxValue, targetBrightness }); 
       break; 
      } 
     } 
    } 
} 

を、Brightness Control in WDDMを見て、Monitor Configuration Functionsください

3

このようにそれを試してみてください。

using System; 
using System.ComponentModel; 
using System.Runtime.InteropServices; 

namespace MediaManagerSql.DataAccess.Sql.EntityFramework 
{ 
    public class ScreenBrightness : Component 
    { 
     private int _gammaValue; 
     private RAMP _ramp; 

     public ScreenBrightness() 
     { 
      InitializeComponent(); 
     } 

     public ScreenBrightness(IContainer container) 
     { 
      container.Add(this); 
      InitializeComponent(); 
     } 

     [Description("Brightness Gamma Value")] 
     [Category("Brightness")] 
     public int SetGammaValue 
     { 
      get { return _gammaValue; } 
      set { _gammaValue = value; } 
     } 

     [DllImport("gdi32.dll")] 
     public static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp); 
     [DllImport("user32.dll")] 
     private static extern IntPtr GetDC(IntPtr hWnd); 

     /// <summary> 
     /// Apply the selected gamma value to screen 
     /// </summary> 
     public void ApplyGamma() 
     { 
      // since gamma value is max 44 ,, we need to take the percentage from this because 
      // it needed from 0 - 100% 
      double gValue = _gammaValue; 
      gValue = Math.Floor(Convert.ToDouble((gValue/2.27))); 

      _gammaValue = Convert.ToInt16(gValue); 

      if (_gammaValue != 0) 
      { 
       _ramp.Red = new ushort[256]; 
       _ramp.Green = new ushort[256]; 
       _ramp.Blue = new ushort[256]; 

       for (int i = 1; i < 256; i++) 
       { 
        // gamma is a value between 3 and 44 
        _ramp.Red[i] = 
         _ramp.Green[i] = 
         _ramp.Blue[i] = 
         (ushort) 
         (Math.Min(65535, Math.Max(0, Math.Pow((i + 1)/256.0, (_gammaValue + 5)*0.1)*65535 + 0.5))); 
       } 

       SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref _ramp); 
      } 
     } 

     #region Nested type: RAMP 

     [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; 
     } 
     #endregion 
    } 
} 
+0

現在地チュートリアルを見ることができます。そしてそれはガンマ値を変えていますが、私は明るさの値を変えたいと思います。 Windowsのように - >画面の明るさを調整します(モニターのバックライト)。 – sczdavos

+0

ウィンサービスアプリでこのメソッドを使用する方法は? – Butsaty

0

にマイクロソフトのAPIを使用します画面の明るさを調整します。 SetDeviceGammaRamp関数を呼び出します。

このコードを試してください。

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); 
     } 
    } 
} 

ウィンドウアプリケーションを作成し、ボタンとトラックバーをドラッグします。それが唯一のいくつかのPCに取り組んでいますhttp://www.lattepanda.com/topic-f11t3020.html

関連する問題