14 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
15 LPSTR lpCmdLine,
int nCmdShow) {
20 red = CreateSolidBrush(RGB(0xFF, 0, 0));
21 green = CreateSolidBrush(RGB(0, 0xFF, 0));
22 yellow = CreateSolidBrush(RGB(0xFF, 0xFF, 0));
23 blue = CreateSolidBrush(RGB(0, 0, 0xDF));
55 while (GetMessage(&Msg, NULL, 0, 0) > 0) {
56 TranslateMessage(&Msg);
57 DispatchMessage(&Msg);
68 GetWindowRect(GetDesktopWindow(), &desktop);
78 hwnd = CreateWindowEx(
79 WS_EX_TOOLWINDOW | WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TRANSPARENT,
83 desktop.right - 90, 20, 70, 30,
86 SetLayeredWindowAttributes(hwnd, 0, 120, LWA_ALPHA);
87 ShowWindow(hwnd, nCmdShow);
95 nfi.cbSize =
sizeof (nfi);
98 Shell_NotifyIcon(NIM_DELETE, &nfi);
107 wc.cbSize =
sizeof (WNDCLASSEX);
113 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
114 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
115 wc.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));
116 wc.lpszMenuName = NULL;
118 wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
120 return RegisterClassEx(&wc);
124 char* buf =
new char[255];
125 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0, buf, 255, NULL);
127 MessageBox(NULL, buf,
"Error",
128 MB_ICONEXCLAMATION | MB_OK);
133 LRESULT CALLBACK
wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
143 SYSTEM_POWER_STATUS pwr;
144 GetSystemPowerStatus(&pwr);
145 if (pwr.BatteryFlag == 128 || pwr.BatteryFlag == 255) {
147 }
else if (pwr.ACLineStatus) {
148 battery = -(double) pwr.BatteryLifePercent / 100.0;
150 AbortSystemShutdown(NULL);
155 battery = (double) pwr.BatteryLifePercent / 100.0;
160 InvalidateRect(hwnd, NULL,
true);
165 HDC hdc = BeginPaint(hwnd, &p);
188 FillRect(hdc, &r2,
red);
191 FillRect(hdc, &r1, load);
192 SetBkMode(hdc, TRANSPARENT);
193 SetTextColor(hdc, 0);
203 DrawText(hdc, time, 5, &rtext, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
205 DrawText(hdc,
"N/A", 3, &rtext, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
212 if (wParam != 0)
break;
214 case WM_RBUTTONDBLCLK:
215 PostMessage(hwnd, WM_CLOSE, 0, 0);
229 return DefWindowProc(hwnd, msg, wParam, lParam);
238 nfi.cbSize =
sizeof (nfi);
242 strcpy(nfi.szTip,
"BatteryMonitor");
243 nfi.uFlags = NIF_ICON | NIF_TIP | 0x80 | NIF_MESSAGE;
244 nfi.uCallbackMessage = WM_USER + 1;
246 Shell_NotifyIcon(NIM_ADD, &nfi);
251 nfi.cbSize =
sizeof (nfi);
254 nfi.uFlags = NIF_INFO;
255 strcpy(nfi.szInfoTitle,
"Battery Info");
256 nfi.uTimeout = 30000;
257 nfi.dwInfoFlags = NIIF_INFO;
259 SYSTEM_POWER_STATUS pwr;
260 GetSystemPowerStatus(&pwr);
261 if (info.DesignedCapacity) {
263 memcpy(chem, info.Chemistry, 4);
265 sprintf(nfi.szInfo,
"Type: %s\nAC: %s\nCharge: %d%%\nWear Level: %lu%%", chem, pwr.ACLineStatus ?
"Online" :
"Offline", pwr.BatteryLifePercent, 100 - info.FullChargedCapacity * 100 / info.DesignedCapacity);
267 strcpy(nfi.szInfo,
"No information available :(");
270 Shell_NotifyIcon(NIM_MODIFY, &nfi);
275 TOKEN_PRIVILEGES tkp;
279 if (!OpenProcessToken(GetCurrentProcess(),
280 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
281 MessageBox(0,
"Low Battery! Shutdown failed.",
"Error", MB_ICONERROR);
285 LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
287 tkp.PrivilegeCount = 1;
288 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
292 AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
294 if (GetLastError() != ERROR_SUCCESS)
297 InitiateSystemShutdown(NULL,
"Low battery", 30, FALSE, FALSE);
303 HDEVINFO devinfo = SetupDiGetClassDevs(&GUID_DEVICE_BATTERY, NULL, NULL, DIGCF_DEVICEINTERFACE);
304 if (INVALID_HANDLE_VALUE != devinfo) {
305 SP_DEVICE_INTERFACE_DATA deviceinfo;
306 deviceinfo.cbSize =
sizeof (deviceinfo);
307 if (SetupDiEnumDeviceInterfaces(devinfo, 0, &GUID_DEVICE_BATTERY, 0, &deviceinfo)) {
308 PSP_DEVICE_INTERFACE_DETAIL_DATA devdetails;
310 SetupDiGetDeviceInterfaceDetail(devinfo, &deviceinfo, 0, 0, &reqSize, 0);
311 if (ERROR_INSUFFICIENT_BUFFER == GetLastError()) {
312 devdetails = (PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalAlloc(LPTR, reqSize);
314 devdetails->cbSize =
sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);
315 if (SetupDiGetDeviceInterfaceDetail(devinfo, &deviceinfo, devdetails, reqSize, &reqSize, NULL)) {
316 devicepath =
new char[strlen(devdetails->DevicePath)];
320 LocalFree(devdetails);
325 if (GetLastError() != ERROR_SUCCESS)
errOut();
329 BATTERY_INFORMATION batteryinformation;
331 HANDLE bat = CreateFile(
devicepath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
332 if (INVALID_HANDLE_VALUE != bat) {
336 if (DeviceIoControl(bat, IOCTL_BATTERY_QUERY_TAG, &timeout,
sizeof (timeout), &tag,
sizeof (tag), &buf, NULL)) {
337 BATTERY_QUERY_INFORMATION infoqueryin;
338 infoqueryin.BatteryTag = tag;
339 infoqueryin.InformationLevel = BatteryInformation;
340 if (DeviceIoControl(bat, IOCTL_BATTERY_QUERY_INFORMATION, &infoqueryin,
sizeof (infoqueryin), &batteryinformation,
sizeof (batteryinformation), &buf, NULL)) {
342 return batteryinformation;
349 batteryinformation.DesignedCapacity = 0;
350 return batteryinformation;
UINT WM_TASKBARCREATED
Window message for taskbarcreated-event.
BATTERY_INFORMATION getBatteryInformation()
retrieves battery informations from the system.
void cleanUp(HWND hwnd)
Memory and handler cleanups, taskbar icon removal.
HBRUSH yellow
Yellow brush.
void initDevice()
Retrieves the path of the battery device.
void addNotifyIcon(HWND hwnd)
Adds an icon to the taskbar.
void errOut()
Error output.
#define UPDATE_BATTERY
Window message for updating battery status.
#define MAIN_ICON
The icon of BatteryMonitor.
public funktions and variables
double battery
Battery charge.
bool shutdown_enabled
Indicates, if shutdown was invoked by BatteryMonitor.
long batterylifetime
Remaining battery lifetime in seconds.
HINSTANCE instance
Application instance.
char * devicepath
Path to battery device.
void showBatteryInfo(HWND hwnd)
Shows a balloon tip with battery information.
long setupWindowClass()
registers the window class stored in classname.
HWND setupWindow(int nCmdShow)
Shows the main window.
LRESULT CALLBACK wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
The window procedure.
char classname[14]
Name of registered window class.
void shutdown()
Tries to shut down the computer.