質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Win32 API

Win32 APIはMicrosoft Windowsの32bitプロセッサのOSで動作するAPIです。

Q&A

解決済

1回答

562閲覧

C#で画面の輝度を変更するプログラムの作成時の質問です

yakoumori

総合スコア12

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Win32 API

Win32 APIはMicrosoft Windowsの32bitプロセッサのOSで動作するAPIです。

0グッド

0クリップ

投稿2018/12/10 17:23

編集2018/12/11 15:08

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型の値を入れて実行しましたが、エラーメッセージが出てしまった。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2018/12/10 21:56

エラーの内容は単純な文法ミスです。直したものがあるならそちらを聞いた方がいいと思います。
YAmaGNZ

2018/12/11 05:21

SetACBrightnessを呼んでませんが、どうやって値を変えるおつもりでしょうか?
yakoumori

2018/12/11 08:29

呼び出す場合どのように書けば呼び出せませすか?
Zuishin

2018/12/11 08:31

エラーの内容は単純な文法ミスです。直したものがあるならそちらを聞いた方がいいと思います。
Zuishin

2018/12/11 08:31

エラーの内容は単純な文法ミスです。直したものがあるならそちらを聞いた方がいいと思います。
Zuishin

2018/12/11 21:43 編集

文法ミスを修正する回答がついています。今更質問を直しても遅いので質問を元に戻し、元々の質問を直した回答をベストアンサーにして新しく質問してください。
Zuishin

2018/12/11 21:43 編集

文法ミスを修正する回答がついています。今更質問を直しても遅いので質問を元に戻し、元々の質問を直した回答をベストアンサーにして新しく質問してください。
Zuishin

2018/12/11 21:43 編集

文法ミスを修正する回答がついています。今更質問を直しても遅いので質問を元に戻し、元々の質問を直した回答をベストアンサーにして新しく質問してください。
guest

回答1

0

ベストアンサー

以下の宣言が重複しています。

Guid GUID_VIDEO_SUBGROUP = new Guid("7516b95f-f776-4464-8c53-06167f40cc99");
Guid DisplayBrightnessGuid = new Guid("aded5e82-b909-4619-9949-f5d71dac0bcb");

bool SetACBrightness(int bright_ac)

↑関数の開始と終了を示す'{' と '}'が抜けています。

投稿2018/12/11 00:14

takabosoft

総合スコア8356

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yakoumori

2018/12/11 04:09

ありがとうございます。{}が足りなかったみたいで追加したらエラーが少なくなり、宣言の重複部分を消したらエラーがなくなりました。 エラーがなくなった状態で実行してみたのですが、Guid MY_POWER_MANAGEMENT_POLICY = new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); の値を変更しているのにも関わらず画面が変化しているようには見えない気がするのですがどのようにしたら画面の輝度が変化したように感じるのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問