LoginSignup
1
1

More than 3 years have passed since last update.

ノートパソコンの画面のバッテリー状況をボタン一つで取得する【C#】

Last updated at Posted at 2021-03-11

動機

ボタンで何かできるかシリーズの第三弾です。

今回はバッテリー情報も C#から取得することができるということだったので、それについてまとめてみました。

参考 1:ノートパソコンの画面の明るさをボタン一つで変更する【C#】
参考 2:ノートパソコンの画面の音量をボタン一つで変更する【C#】

なお、作成に関しては WpfApp で作成しました。

イメージ画像

プロトのため、画面は相変わらずそっけないです。

バッテリーチェックボタンを選択すると、バッテリーの AC 接続状況や充電状況、バッテリー容量、バッテリーの駆動時間などを表示してくれます。

メイン画面

バッテリーメイン.PNG

バッテリーの AC 接続状況

バッテリー接続状態.PNG

バッテリーの充電状況

バッテリー充電状態.PNG

バッテリー現在容量

バッテリー容量.PNG

ソース

MainWindow.xaml

MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button x:Name="button" Content="バッテリーチェック" HorizontalAlignment="Left" Margin="212,92,0,0" VerticalAlignment="Top" Width="317" Click="button_Click" Height="184" FontSize="36"/>

    </Grid>
</Window>

MainWindow.xaml.cs

MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Forms;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            //AC電源の状態
            System.Windows.Forms.PowerLineStatus pls = SystemInformation.PowerStatus.PowerLineStatus;
            switch (pls)
            {
                case System.Windows.Forms.PowerLineStatus.Offline:
                    System.Windows.MessageBox.Show("AC電源がオフラインです");
                    break;
                case System.Windows.Forms.PowerLineStatus.Online:
                    System.Windows.MessageBox.Show("AC電源がオンラインです");
                    break;
                case System.Windows.Forms.PowerLineStatus.Unknown:
                    System.Windows.MessageBox.Show("AC電源の状態は不明です");
                    break;
            }

            //バッテリーの充電状態を取得する
            BatteryChargeStatus bcs =
                SystemInformation.PowerStatus.BatteryChargeStatus;
            if (bcs == BatteryChargeStatus.Unknown)
            {
                System.Windows.MessageBox.Show("不明です");
            }
            else
            {
                if ((bcs & BatteryChargeStatus.High) ==
                    BatteryChargeStatus.High)
                {
                    System.Windows.MessageBox.Show("充電レベルは、高い(66%より上)です");
                }
                if ((bcs & BatteryChargeStatus.Low) ==
                    BatteryChargeStatus.Low)
                {
                    System.Windows.MessageBox.Show("充電レベルは、低い(33%未満)です");
                }
                if ((bcs & BatteryChargeStatus.Critical) ==
                    BatteryChargeStatus.Critical)
                {
                    System.Windows.MessageBox.Show("充電レベルは、最低(5%未満)です");
                }
                if ((bcs & BatteryChargeStatus.Charging) ==
                    BatteryChargeStatus.Charging)
                {
                    System.Windows.MessageBox.Show("充電中です");
                }
                if ((bcs & BatteryChargeStatus.NoSystemBattery) ==
                    BatteryChargeStatus.NoSystemBattery)
                {
                    System.Windows.MessageBox.Show("バッテリーが存在しません");
                }
            }

            //バッテリー残量(割合)
            float blp = SystemInformation.PowerStatus.BatteryLifePercent;
            System.Windows.MessageBox.Show("バッテリー残量は、"+ blp * 100+"%です。");

            //バッテリー残量(時間)
            int blr = SystemInformation.PowerStatus.BatteryLifeRemaining;
            if (-1 < blr)
            {
                System.Windows.MessageBox.Show("バッテリー残り時間は、" + blr+ "秒です");
            }
            else
            {
                //AC電源がオンラインの時など
                System.Windows.MessageBox.Show("バッテリー残り時間は、不明です");
            }

            //バッテリーがフル充電された時の持ち時間(バッテリー駆動時間)
            int bfl = SystemInformation.PowerStatus.BatteryFullLifetime;
            if (-1 < bfl)
            {
                System.Windows.MessageBox.Show("バッテリー駆動時間は、" + bfl+ "秒です");
            }
            else
            {
                System.Windows.MessageBox.Show("バッテリー駆動時間は、不明です");
            }
        }
    }
}

終わりに

今回はボタン一つで Windows10 の設定を変更するといった内容ではないですが、バッテリーの状況を把握したいときも有るでしょう!ということで、興味半分で調べて実装してみました。

何かのお役に立てれば幸いです。

これでまた Windows10 便利化計画に一歩近づきそうです。

1
1
0

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
1
1