Created
November 4, 2016 13:27
-
-
Save voidproc/e871a89fff37c9d3d32e879b7afae4d5 to your computer and use it in GitHub Desktop.
Show system power status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define NOMINMAX | |
#define STRICT | |
#define WIN32_LEAN_AND_MEAN | |
#include <windows.h> | |
#include <Siv3D.hpp> | |
// システムの電源情報をWin32APIにより取得し保持する | |
class PowerStatus | |
{ | |
public: | |
// 電源の情報を更新する | |
bool update() | |
{ | |
BOOL ret = GetSystemPowerStatus(&powerStatus_); | |
return (ret != 0); | |
} | |
// AC電源につながっているかどうか | |
Optional<bool> ac() | |
{ | |
switch (powerStatus_.ACLineStatus) | |
{ | |
case 0: | |
return false; | |
case 1: | |
return true; | |
case 255: | |
return none; | |
} | |
return none; | |
} | |
// バッテリー残量 (%) | |
Optional<int> batteryLife() | |
{ | |
if (powerStatus_.BatteryLifePercent == 255) | |
{ | |
return none; | |
} | |
return powerStatus_.BatteryLifePercent; | |
} | |
private: | |
SYSTEM_POWER_STATUS powerStatus_; | |
}; | |
void Main() | |
{ | |
Font font(10, Typeface::Bold); | |
PowerStatus power; | |
power.update(); | |
while (System::Update()) | |
{ | |
// 1秒間隔で電源の情報を更新 | |
if (System::FrameCount() % 60 == 0) | |
{ | |
power.update(); | |
} | |
// バッテリーアイコンを描く | |
Rect batteryIcon(2, 2, 50, 20); | |
batteryIcon.draw(); | |
Rect(4, 10).movedBy(batteryIcon.x + batteryIcon.w, batteryIcon.center.y - 10/2).draw(); | |
if (power.batteryLife()) | |
{ | |
// バッテリー残量ゲージを描く | |
Rect(batteryIcon.pos, batteryIcon.w * power.batteryLife().value() / 100, batteryIcon.h).draw(Palette::Limegreen); | |
// バッテリー残量をパーセント表示 | |
font(power.batteryLife().value(), L"%").drawCenter(batteryIcon.center, Palette::Black); | |
} | |
// AC電源接続状態を表示 | |
s3d::RoundRect acIcon(64, 2, 40, 20, 4); | |
if (power.ac()) | |
{ | |
if (power.ac().value()) | |
{ | |
acIcon.draw(Palette::Orange); | |
font(L"AC").drawCenter(acIcon.center, Palette::Black); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment