Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9743

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

$
0
0

動機

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

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

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

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

イメージ画像

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

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

メイン画面

バッテリーメイン.PNG

バッテリーの AC 接続状況

バッテリー接続状態.PNG

バッテリーの充電状況

バッテリー充電状態.PNG

バッテリー現在容量

バッテリー容量.PNG

ソース

MainWindow.xaml

MainWindow.xaml
<Windowx: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><Buttonx: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
usingSystem;usingSystem.Windows;usingSystem.Windows.Forms;namespaceWpfApp1{/// <summary>/// MainWindow.xaml の相互作用ロジック/// </summary>publicpartialclassMainWindow:Window{publicMainWindow(){InitializeComponent();}privatevoidbutton_Click(objectsender,RoutedEventArgse){//AC電源の状態System.Windows.Forms.PowerLineStatuspls=SystemInformation.PowerStatus.PowerLineStatus;switch(pls){caseSystem.Windows.Forms.PowerLineStatus.Offline:System.Windows.MessageBox.Show("AC電源がオフラインです");break;caseSystem.Windows.Forms.PowerLineStatus.Online:System.Windows.MessageBox.Show("AC電源がオンラインです");break;caseSystem.Windows.Forms.PowerLineStatus.Unknown:System.Windows.MessageBox.Show("AC電源の状態は不明です");break;}//バッテリーの充電状態を取得するBatteryChargeStatusbcs=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("バッテリーが存在しません");}}//バッテリー残量(割合)floatblp=SystemInformation.PowerStatus.BatteryLifePercent;System.Windows.MessageBox.Show("バッテリー残量は、"+blp*100+"%です。");//バッテリー残量(時間)intblr=SystemInformation.PowerStatus.BatteryLifeRemaining;if(-1<blr){System.Windows.MessageBox.Show("バッテリー残り時間は、"+blr+"秒です");}else{//AC電源がオンラインの時などSystem.Windows.MessageBox.Show("バッテリー残り時間は、不明です");}//バッテリーがフル充電された時の持ち時間(バッテリー駆動時間)intbfl=SystemInformation.PowerStatus.BatteryFullLifetime;if(-1<bfl){System.Windows.MessageBox.Show("バッテリー駆動時間は、"+bfl+"秒です");}else{System.Windows.MessageBox.Show("バッテリー駆動時間は、不明です");}}}}

終わりに

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

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

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


Viewing all articles
Browse latest Browse all 9743

Trending Articles