LoginSignup
0

More than 1 year has passed since last update.

[C++/Windows] 省電力設定のスライダーの現在値を取得

Last updated at Posted at 2021-02-26

もくじ
https://tera1707.com/entry/2022/02/06/144447

やりたいこと

ノートPCなど、バッテリーを積んでるWindowsPCで画面の右下に出てる電池マークを押すと出てくるスライダーが今どこにあるのか、現在値を知りたい。
image.png

やりかた

Win32APIのPowerRegisterForEffectivePowerModeNotifications()を使って、値の変化時に指定/登録した関数を呼んでもらうことで実現する。
※プログラム終了時にはPowerUnregisterFromEffectivePowerModeNotifications()を使って登録解除する必要あり。

サンプル

// PowerRegisterForEffectivePowerModeNotificationsに必要
#pragma comment(lib, "Powrprof.lib")
#include <powersetting.h>

// 要点以外のコードは割愛

void OnEffectivePowerModeChanged(EFFECTIVE_POWER_MODE mode, void* context)
{
    std::wstring buf;
    buf = L"現在のEFFECTIVE_POWER_MODE:" + std::to_wstring(mode) + L"\r\n";
    OutputDebugString(buf.c_str());
}

// EFFECTIVE_POWER_MODE変化のイベント登録ハンドル
void* RegistrationHandle = NULL;

// ダイアログプロシージャ
BOOL CALLBACK MyDlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
{
    std::wstring buf;
    switch (msg) {
        case WM_INITDIALOG:
        {
            HANDLE* Context = NULL;
            // 起動時、スライダの値変化時のハンドラを登録する
            PowerRegisterForEffectivePowerModeNotifications(EFFECTIVE_POWER_MODE_V1, (EFFECTIVE_POWER_MODE_CALLBACK*)OnEffectivePowerModeChanged, Context, &RegistrationHandle);
            break;
        }
        case WM_DESTROY:
        {
            // 終了時、登録したハンドラを破棄する
            if (RegistrationHandle != NULL)
                PowerUnregisterFromEffectivePowerModeNotifications(RegistrationHandle);
            break;
        }
        return FALSE;
    }
}

上記のコードを実行している状態でスライダーの値をぐりぐり変えてやると、変えたときの値がデバッグの出力欄に出てくる。
image.png

参考

スライダーの仕様
https://docs.microsoft.com/ja-jp/windows-hardware/customize/desktop/customize-power-slider#set-default-power-slider-mode

スライダの変化をとるイベント
https://docs.microsoft.com/ja-jp/windows/win32/api/powersetting/nf-powersetting-powerregisterforeffectivepowermodenotifications

オーバーレイ(電力スライダ)の設定についての詳しい説明
https://sites.google.com/site/tweakradje/windows/windows-tweaking
>Windows 10 adds a new scheme, Ultimate Performance:

PowerRegisterForEffectivePowerModeNotifications
https://docs.microsoft.com/ja-jp/windows/win32/api/powersetting/nf-powersetting-powerregisterforeffectivepowermodenotifications

PowerUnregisterFromEffectivePowerModeNotifications
https://docs.microsoft.com/en-us/windows/win32/api/powersetting/nf-powersetting-powerunregisterfromeffectivepowermodenotifications

EFFECTIVE_POWER_MODE_CALLBACK
https://docs.microsoft.com/ja-jp/windows/win32/api/powersetting/nf-powersetting-effective_power_mode_callback

EFFECTIVE_POWER_MODE
https://docs.microsoft.com/ja-jp/windows/win32/api/powersetting/ne-powersetting-effective_power_mode

スライダーの値をpowercfgで変更する
https://www.tenforums.com/tutorials/43655-create-custom-power-plan-windows-10-a-2.html

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0