VisualStudio2013にてC#を利用して画面の輝度を変更するプログラムを作成しています。
そのためにサンプルプログラムを見つけ実行したのですがうまく動作しません。
エラーメッセージ なし 問題 画面の輝度が変わるはずなのに画面が変化せずformが起動しただけになっている。 またbool SetACBrightnessを呼び出し値を入れる方法がわからない。
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Windows.Forms; 9using System.Runtime.InteropServices; 10using System.Drawing.Imaging; 11using System.Globalization; 12using Microsoft.Win32; 13 14namespace WindowsFormsApplication3 15{ 16 public partial class Form1 : Form 17 { 18 public Form1() 19 { 20 InitializeComponent(); 21 } 22 23 #region Constants 24 /// <summary> 25 /// Settings in this subgroup control configuration of the video power management features. 26 /// </summary> 27 public Guid GUID_VIDEO_SUBGROUP = new Guid("7516b95f-f776-4464-8c53-06167f40cc99"); 28 29 /// <summary> 30 /// Guid for display brightness setting. 31 /// </summary> 32 public Guid DisplayBrightnessGuid = new Guid("aded5e82-b909-4619-9949-f5d71dac0bcb"); 33 #endregion 34 35 #region Methods 36 /// <summary> 37 /// Retrieves the AC index of the specified power setting. 38 /// </summary> 39 /// <param name="RootPowerKey">This parameter is reserved for future use and must be set to NULL.</param> 40 /// <param name="SchemeGuid">The identifier of the power scheme.</param> 41 /// <param name="SubGroupOfPowerSettingsGuid">The subgroup of power settings. This parameter can be one of the following values defined in WinNT.h. Use NO_SUBGROUP_GUID to refer to the default power scheme.</param> 42 /// <param name="PowerSettingGuid">The identifier of the power setting</param> 43 /// <param name="AcValueIndex">A pointer to a variable that receives the AC value index.</param> 44 /// <returns>Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed.</returns> 45 [DllImport("powrprof.dll", EntryPoint = "PowerReadACValueIndex", CharSet = CharSet.Auto, SetLastError = true)] 46 public static extern uint PowerReadACValueIndex(IntPtr RootPowerKey, ref Guid SchemeGuid, ref Guid SubGroupOfPowerSettingsGuid, ref Guid PowerSettingGuid, out uint AcValueIndex); 47 48 49 /// <summary> 50 /// Sets the AC value index of the specified power setting. 51 /// </summary> 52 /// <param name="RootPowerKey">This parameter is reserved for future use and must be set to NULL.</param> 53 /// <param name="SchemeGuid">The identifier of the power scheme.</param> 54 /// <param name="SubGroupOfPowerSettingsGuid">The subgroup of power settings. This parameter can be one of the following values defined in WinNT.h. Use NO_SUBGROUP_GUID to refer to the default power scheme.</param> 55 /// <param name="PowerSettingGuid">The identifier of the power setting</param> 56 /// <param name="AcValueIndex">The AC value index.</param> 57 /// <returns>Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed.</returns> 58 [DllImport("powrprof.dll", EntryPoint = "PowerWriteACValueIndex", CharSet = CharSet.Auto, SetLastError = true)] 59 public static extern uint PowerWriteACValueIndex(IntPtr RootPowerKey, ref Guid SchemeGuid, ref Guid SubGroupOfPowerSettingsGuid, ref Guid PowerSettingGuid, uint AcValueIndex); 60 61 /// <summary> 62 /// Retrieves the AC index of the specified power setting. 63 /// </summary> 64 /// <param name="RootPowerKey">This parameter is reserved for future use and must be set to NULL.</param> 65 /// <param name="SchemeGuid">The identifier of the power scheme.</param> 66 /// <param name="SubGroupOfPowerSettingsGuid">The subgroup of power settings. This parameter can be one of the following values defined in WinNT.h. Use NO_SUBGROUP_GUID to refer to the default power scheme.</param> 67 /// <param name="PowerSettingGuid">The identifier of the power setting</param> 68 /// <param name="AcValueIndex">A pointer to a variable that receives the AC value index.</param> 69 /// <returns>Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed.</returns> 70 [DllImport("powrprof.dll", EntryPoint = "PowerReadDCValueIndex", CharSet = CharSet.Auto, SetLastError = true)] 71 public static extern uint PowerReadDCValueIndex(IntPtr RootPowerKey, ref Guid SchemeGuid, ref Guid SubGroupOfPowerSettingsGuid, ref Guid PowerSettingGuid, out uint AcValueIndex); 72 73 74 /// <summary> 75 /// Sets the AC value index of the specified power setting. 76 /// </summary> 77 /// <param name="RootPowerKey">This parameter is reserved for future use and must be set to NULL.</param> 78 /// <param name="SchemeGuid">The identifier of the power scheme.</param> 79 /// <param name="SubGroupOfPowerSettingsGuid">The subgroup of power settings. This parameter can be one of the following values defined in WinNT.h. Use NO_SUBGROUP_GUID to refer to the default power scheme.</param> 80 /// <param name="PowerSettingGuid">The identifier of the power setting</param> 81 /// <param name="AcValueIndex">The AC value index.</param> 82 /// <returns>Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed.</returns> 83 [DllImport("powrprof.dll", EntryPoint = "PowerWriteDCValueIndex", CharSet = CharSet.Auto, SetLastError = true)] 84 public static extern uint PowerWriteDCValueIndex(IntPtr RootPowerKey, ref Guid SchemeGuid, ref Guid SubGroupOfPowerSettingsGuid, ref Guid PowerSettingGuid, uint AcValueIndex); 85 86 87 /// <summary> 88 /// Sets the active power scheme for the current user. 89 /// </summary> 90 /// <param name="UserRootPowerKey">This parameter is reserved for future use and must be set to NULL.</param> 91 /// <param name="SchemeGuid">The identifier of the power scheme.</param> 92 /// <returns>Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed.</returns> 93 [DllImport("powrprof.dll", EntryPoint = "PowerSetActiveScheme", CharSet = CharSet.Auto, SetLastError = true)] 94 public static extern uint PowerSetActiveScheme(IntPtr UserRootPowerKey, ref Guid SchemeGuid); 95 96 #endregion 97 98 Guid MY_POWER_MANAGEMENT_POLICY = new Guid("8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"); // 今回は<SCHEME_GUID>をベタ指定 99 //381b4222-f694-41f0-9685-ff5bb260df2e(バランス) 100 //8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c(高パフォーマンス) 101 //a1841308-3541-4fab-bc81-f71556f20b4a(省電力) 102 103 104 105 106 bool SetACBrightness(int bright_ac){ 107 108 if(bright_ac < 0 || bright_ac > 100) 109 return false; 110 111 uint ret = PowerWriteACValueIndex(IntPtr.Zero, ref MY_POWER_MANAGEMENT_POLICY, ref GUID_VIDEO_SUBGROUP, ref DisplayBrightnessGuid, (uint)bright_ac); 112 if(ret != 0) 113 return false; 114 115 ret = PowerSetActiveScheme(IntPtr.Zero, ref MY_POWER_MANAGEMENT_POLICY); 116 if(ret != 0) 117 return false; 118 119 return true; 120 } 121 122 123 } 124 125 126} 127
#試したこと
bool SetACBrightnessの前にint SetACBrightnessを設定し値を代入したのですがbool SetACBrightnessにて定義されていると言われました。そのため実行ができませんでした。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。