📄 battstat.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
#include "stdafx.h"
#include "BattStat.h"
#include <windows.h>
#include <commctrl.h>
#include <regext.h>
#include <snapi.h>
#define MAX_LOADSTRING 100
#define STATUS_STRING_LEN 500
#define WM_OFFSET 10000
#define WM_CHANGE_BATTSTRENGTH (WM_OFFSET + 1)
#define WM_CHANGE_BATTSTATE (WM_OFFSET + 2)
#define WM_CHANGE_BACKUPSTRENGTH (WM_OFFSET + 3)
#define WM_CHANGE_BACKUPSTATE (WM_OFFSET + 4)
#define IDC_BATTSTRENGTH (WM_OFFSET + 200)
#define IDC_BATTSTATE (WM_OFFSET + 201)
#define IDC_BACKUPSTRENGTH (WM_OFFSET + 202)
#define IDC_BACKUPSTATE (WM_OFFSET + 203)
#define IDC_CMDLINE (WM_OFFSET + 204)
// Global Variables:
HINSTANCE g_hInst; // Current instance
HWND g_hWndMenuBar; // Menu bar handle
HWND g_hCurWndDialog; // Primary Window Dialog
HWND g_hBattStrength; // Status text windows
HWND g_hBattState;
HWND g_hBackupStrength;
HWND g_hBackupState;
HWND g_hCmdLine;
// RegistryNotifyApp names
const WCHAR c_wszAppBattStrengthName[] = L"BattStat.BattStrength";
const WCHAR c_wszAppBattStateName[] = L"BattStat.BattState";
const WCHAR c_wszAppBackupStrengthName[] = L"BattStat.BackupStrength";
const WCHAR c_wszAppBackupStateName[] = L"BattStat.BackupState";
#define NOTIFY_CNT 4
HREGNOTIFY g_hNotify[NOTIFY_CNT]; // Handles to notifications
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
HRESULT RegisterWindows();
HRESULT RegisterApps();
HRESULT UnregisterWindows();
HRESULT UnregisterApps();
void BattStateCallback(HREGNOTIFY hNotify, DWORD dwUserData, const PBYTE pData, const UINT cbData);
//
// FUNCTION: int WINAPI WinMain(HINSTANCE hInstance,
// HINSTANCE hPrevInstance,
// LPTSTR lpCmdLine,
// int nCmdShow)
//
// PURPOSE: Main loop.
//
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
// Perform application initialization.
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_BATTSTAT));
// Main message loop.
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BATTSTAT));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FUNCTION: void GetBattStateStr(DWORD dwBattState, LPTSTR pszBattStateStr)
//
// PURPOSE: Form the battery state string from the given battery state flag.
//
void GetBattStateStr(DWORD dwBattState, LPTSTR pszBattStateStr)
{
if (dwBattState == 0)
{
StringCchCat(pszBattStateStr, STATUS_STRING_LEN, L"Normal");
}
else
{
// Check each bit; specific battery drivers may set different combinations of bits.
if (dwBattState & 1)
{
StringCchCat(pszBattStateStr, STATUS_STRING_LEN, L"Not Present ");
}
if (dwBattState & 2)
{
StringCchCat(pszBattStateStr, STATUS_STRING_LEN, L"Charging ");
}
if (dwBattState & 4)
{
StringCchCat(pszBattStateStr, STATUS_STRING_LEN, L"Low ");
}
if (dwBattState & 8)
{
StringCchCat(pszBattStateStr, STATUS_STRING_LEN, L"Critical");
}
}
}
//
// FUNCTION: void UpdateBattState(HKEY hKey, LPCWSTR wszSubKey, LPCWSTR wszName,
// DWORD dwBitMask, int nControlID)
//
// PURPOSE: Update the given battery state.
//
void UpdateBattState(HKEY hKey, LPCWSTR wszSubKey, LPCWSTR wszName, DWORD dwBitMask, int nControlID)
{
HRESULT hr;
DWORD dwRegValue;
DWORD dwBattState;
TCHAR szBattStateStr[STATUS_STRING_LEN];
StringCchCopy(szBattStateStr, STATUS_STRING_LEN, L"");
// Read the new value.
hr = RegistryGetDWORD(hKey, wszSubKey, wszName, &dwRegValue);
if (hr == S_OK)
{
// Only look at specified bits.
dwBattState = dwRegValue & dwBitMask;
GetBattStateStr(dwBattState, szBattStateStr);
}
// Update the status text.
SetDlgItemText(g_hCurWndDialog, nControlID, szBattStateStr);
}
//
// FUNCTION: void UpdateBattStrength(HKEY hKey, LPCWSTR wszSubKey, LPCWSTR wszName,
// DWORD dwBitMask, int nControlID)
//
// PURPOSE: Update the given battery strength level.
//
void UpdateBattStrength(HKEY hKey, LPCWSTR wszSubKey, LPCWSTR wszName, DWORD dwBitMask, int nControlID)
{
HRESULT hr;
DWORD dwRegValue;
ULONG dwBattStrength;
TCHAR szBattStrengthStr[STATUS_STRING_LEN];
StringCchCopy(szBattStrengthStr, STATUS_STRING_LEN, L"");
// Read the new value from the registry.
hr = RegistryGetDWORD(hKey, wszSubKey, wszName, &dwRegValue);
if (hr == S_OK)
{
// Get the value of the highword and shift the bits over to read the actual value.
dwBattStrength = (dwRegValue & dwBitMask) >> 16;
// Check the value to get the appropriate string.
if (dwBattStrength == 0)
{
StringCchCopy(szBattStrengthStr, STATUS_STRING_LEN, L"Very Low");
}
else if (dwBattStrength == 21)
{
StringCchCopy(szBattStrengthStr, STATUS_STRING_LEN, L"Low");
}
else if (dwBattStrength == 41)
{
StringCchCopy(szBattStrengthStr, STATUS_STRING_LEN, L"Medium");
}
else if (dwBattStrength == 61)
{
StringCchCopy(szBattStrengthStr, STATUS_STRING_LEN, L"High");
}
else if (dwBattStrength == 81)
{
StringCchCopy(szBattStrengthStr, STATUS_STRING_LEN, L"Very High");
}
}
// Update the status text.
SetDlgItemText(g_hCurWndDialog, nControlID, szBattStrengthStr);
}
//
// FUNCTION: void UpdateAll()
//
// PURPOSE: Update all the battery values.
//
void UpdateAll()
{
UpdateBattStrength(SN_POWERBATTERYSTRENGTH_ROOT, SN_POWERBATTERYSTRENGTH_PATH, SN_POWERBATTERYSTRENGTH_VALUE, SN_POWERBATTERYSTRENGTH_BITMASK, IDC_BATTSTRENGTH);
UpdateBattState(SN_POWERBATTERYSTATE_ROOT, SN_POWERBATTERYSTATE_PATH, SN_POWERBATTERYSTATE_VALUE, SN_POWERBATTERYSTATE_BITMASK, IDC_BATTSTATE);
UpdateBattStrength(SN_POWERBATTERYBACKUPSTRENGTH_ROOT, SN_POWERBATTERYBACKUPSTRENGTH_PATH, SN_POWERBATTERYBACKUPSTRENGTH_VALUE, SN_POWERBATTERYBACKUPSTRENGTH_BITMASK, IDC_BACKUPSTRENGTH);
UpdateBattState(SN_POWERBATTERYBACKUPSTATE_ROOT, SN_POWERBATTERYBACKUPSTATE_PATH, SN_POWERBATTERYBACKUPSTATE_VALUE, SN_POWERBATTERYBACKUPSTATE_BITMASK, IDC_BACKUPSTATE);
SetDlgItemText(g_hCurWndDialog, IDC_CMDLINE, GetCommandLine());
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window.
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // Title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // Main window class name
// Store instance handle in our global variable.
g_hInst = hInstance;
// SHInitExtraControls should be called once during your application's initialization to initialize any
// of the Pocket PC special controls such as CAPEDIT and SIPPREF.
SHInitExtraControls();
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_BATTSTAT, szWindowClass, MAX_LOADSTRING);
//If it is already running, then focus on the window, and exit.
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// Set focus to foremost child window.
// The "| 0x00000001" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}
if (!MyRegisterClass(hInstance, szWindowClass))
{
return FALSE;
}
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
g_hCurWndDialog = hWnd;
// When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
if (g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;
GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);
MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// Create the status text windows - the labels and the current value.
CreateWindow(TEXT("STATIC"),
TEXT("Battery Strength : "),
WS_VISIBLE,
10, 20, 110, 14,
hWnd, NULL, hInstance, NULL);
g_hBattStrength = CreateWindow(TEXT("STATIC"),
TEXT(""),
WS_VISIBLE,
10, 35, 200, 14,
hWnd, (HMENU)IDC_BATTSTRENGTH, hInstance, NULL);
CreateWindow(TEXT("STATIC"),
TEXT("Battery State : "),
WS_VISIBLE,
10, 60, 110, 14,
hWnd, NULL, hInstance, NULL);
g_hBattState = CreateWindow(TEXT("STATIC"),
TEXT(""),
WS_VISIBLE,
10, 75, 200, 14,
hWnd, (HMENU)IDC_BATTSTATE, hInstance, NULL);
CreateWindow(TEXT("STATIC"),
TEXT("Backup Strength : "),
WS_VISIBLE,
10, 100, 110, 14,
hWnd, NULL, hInstance, NULL);
g_hBackupStrength = CreateWindow(TEXT("STATIC"),
TEXT(""),
WS_VISIBLE,
10, 115, 200, 14,
hWnd, (HMENU)IDC_BACKUPSTRENGTH, hInstance, NULL);
CreateWindow(TEXT("STATIC"),
TEXT("Backup State : "),
WS_VISIBLE,
10, 140, 110, 14,
hWnd, NULL, hInstance, NULL);
g_hBackupState = CreateWindow(TEXT("STATIC"),
TEXT(""),
WS_VISIBLE,
10, 155, 200, 14,
hWnd, (HMENU)IDC_BACKUPSTATE, hInstance, NULL);
CreateWindow(TEXT("STATIC"),
TEXT("Command Line : "),
WS_VISIBLE,
10, 200, 110, 14,
hWnd, NULL, hInstance, NULL);
g_hCmdLine = CreateWindow(TEXT("STATIC"),
TEXT(""),
WS_VISIBLE,
10, 215, 200, 14,
hWnd, (HMENU)IDC_CMDLINE, hInstance, NULL);
// Register our window notifications.
RegisterWindows();
// Initialize the display of the battery values.
UpdateAll();
return TRUE;
}
//
// FUNCTION: LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static SHACTIVATEINFO s_sai;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections.
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
// Unregister our window notifications and exit.
UnregisterWindows();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -