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

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

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

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

Win32 API

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

Visual Studio 2013

Microsoft Visual Studio 2013は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2012の次のバージョンです

Q&A

解決済

2回答

5755閲覧

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

yakoumori

総合スコア12

C#

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

Win32 API

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

Visual Studio 2013

Microsoft Visual Studio 2013は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2012の次のバージョンです

0グッド

0クリップ

投稿2018/12/12 02:58

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にて定義されていると言われました。そのため実行ができませんでした。

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

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

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

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

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

guest

回答2

0

自己解決

buttonをformにて作成し、
buttonをクリックしたら画面の明るさを変更するようにしたらうまく実行できました。

また画面の明るさを徐々に変化させるプログラムを追記します。

C#

1private void button1_Click_1(object sender, EventArgs e) 2 { 3 //徐々にスクリーンの明るさの変更を行うソースコード 4 int c_ac = 1; 5 for (int i = 100; i >1; i--) 6 { 7 c_ac = i; 8 9 //boolの実行 10 if (SetACBrightness(c_ac) == true) 11 { 12 } 13 } 14 for (int i = 0; i <= 100; i++) 15 { 16 c_ac = i; 17 //boolの実行 18 if (SetACBrightness(c_ac) == true) 19 { 20 } 21 } 22}

投稿2018/12/18 15:04

yakoumori

総合スコア12

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

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

0

SetACBrightnessの呼び出し方と言われても、まず「いつ呼び出すか」、「引数のbright_acの値をどうやって決定するか」を決めてください。
これはプログラムを作成する貴方が決定することであり、これが決まらないと呼び出し方も決定しません。

投稿2018/12/12 09:47

YAmaGNZ

総合スコア10242

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問