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