📄 battstat.cpp
字号:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
case ID_MENU_UPDATE:
// Manually update all battery status values.
UpdateAll();
break;
case ID_MENU_PERSIST:
// Register the app notifications.
RegisterApps();
break;
case ID_MENU_REMOVEPERSIST:
// Unregister the app notifications.
UnregisterApps();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
SHMENUBARINFO mbi;
memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;
if (!SHCreateMenuBar(&mbi))
{
g_hWndMenuBar = NULL;
}
else
{
g_hWndMenuBar = mbi.hwndMB;
}
// Initialize the shell activate info structure.
memset(&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_ACTIVATE:
// Notify shell of our activate message.
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;
case WM_CHANGE_BATTSTRENGTH:
// Main battery strength value changed.
UpdateBattStrength(SN_POWERBATTERYSTRENGTH_ROOT, SN_POWERBATTERYSTRENGTH_PATH, SN_POWERBATTERYSTRENGTH_VALUE, SN_POWERBATTERYSTRENGTH_BITMASK, IDC_BATTSTRENGTH);
break;
case WM_CHANGE_BATTSTATE:
// Main battery state value changed.
UpdateBattState(SN_POWERBATTERYSTATE_ROOT, SN_POWERBATTERYSTATE_PATH, SN_POWERBATTERYSTATE_VALUE, SN_POWERBATTERYSTATE_BITMASK, IDC_BATTSTATE);
break;
case WM_CHANGE_BACKUPSTRENGTH:
// Backup battery strength value changed.
UpdateBattStrength(SN_POWERBATTERYBACKUPSTRENGTH_ROOT, SN_POWERBATTERYBACKUPSTRENGTH_PATH, SN_POWERBATTERYBACKUPSTRENGTH_VALUE, SN_POWERBATTERYBACKUPSTRENGTH_BITMASK, IDC_BACKUPSTRENGTH);
break;
case WM_CHANGE_BACKUPSTATE:
// Backup battery state value changed.
UpdateBattState(SN_POWERBATTERYBACKUPSTATE_ROOT, SN_POWERBATTERYBACKUPSTATE_PATH, SN_POWERBATTERYBACKUPSTATE_VALUE, SN_POWERBATTERYBACKUPSTATE_BITMASK, IDC_BACKUPSTATE);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//
// FUNCTION: INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
//
// PURPOSE: Message handler for about box.
//
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Create a Done button and size it.
SHINITDLGINFO shidi;
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
}
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
#ifdef _DEVICE_RESOLUTION_AWARE
case WM_SIZE:
{
DRA::RelayoutDialog(
g_hInst,
hDlg,
DRA::GetDisplayMode() != DRA::Portrait ? MAKEINTRESOURCE(IDD_ABOUTBOX_WIDE) : MAKEINTRESOURCE(IDD_ABOUTBOX));
}
break;
#endif
}
return (INT_PTR)FALSE;
}
//
// FUNCTION: HRESULT RegisterWindows()
//
// PURPOSE: Register the window notifications.
//
HRESULT RegisterWindows()
{
HRESULT hr;
NOTIFICATIONCONDITION nc;
// Make sure we aren't already registered.
UnregisterWindows();
nc.ctComparisonType = REG_CT_ANYCHANGE;
nc.dwMask = SN_POWERBATTERYSTRENGTH_BITMASK;
nc.TargetValue.dw = 0;
// Register battery strength notification.
hr = RegistryNotifyWindow(
SN_POWERBATTERYSTRENGTH_ROOT,
SN_POWERBATTERYSTRENGTH_PATH,
SN_POWERBATTERYSTRENGTH_VALUE,
g_hCurWndDialog,
WM_CHANGE_BATTSTRENGTH, // This notification uses a custom window message.
0,
&nc,
&g_hNotify[0]
);
if (FAILED(hr))
{
goto Error;
}
// Notify us of any change in the value and set the bitmask of the value to check.
nc.ctComparisonType = REG_CT_ANYCHANGE;
nc.dwMask = SN_POWERBATTERYSTATE_BITMASK;
nc.TargetValue.dw = 0;
// Register battery state notification.
hr = RegistryNotifyCallback(
SN_POWERBATTERYSTATE_ROOT,
SN_POWERBATTERYSTATE_PATH,
SN_POWERBATTERYSTATE_VALUE,
BattStateCallback, // This notification uses a callback.
0,
&nc,
&g_hNotify[1]
);
if (FAILED(hr))
{
goto Error;
}
nc.ctComparisonType = REG_CT_ANYCHANGE;
nc.dwMask = SN_POWERBATTERYBACKUPSTRENGTH_BITMASK;
nc.TargetValue.dw = 0;
// Register backup battery strength notification.
hr = RegistryNotifyWindow(
SN_POWERBATTERYBACKUPSTRENGTH_ROOT,
SN_POWERBATTERYBACKUPSTRENGTH_PATH,
SN_POWERBATTERYBACKUPSTRENGTH_VALUE,
g_hCurWndDialog,
WM_CHANGE_BACKUPSTRENGTH,
0,
&nc,
&g_hNotify[2]
);
if (FAILED(hr))
{
goto Error;
}
nc.ctComparisonType = REG_CT_ANYCHANGE;
nc.dwMask = SN_POWERBATTERYBACKUPSTATE_BITMASK;
nc.TargetValue.dw = 0;
// Register backup battery state notification.
hr = RegistryNotifyWindow(
SN_POWERBATTERYBACKUPSTATE_ROOT,
SN_POWERBATTERYBACKUPSTATE_PATH,
SN_POWERBATTERYBACKUPSTATE_VALUE,
g_hCurWndDialog,
WM_CHANGE_BACKUPSTATE,
0,
&nc,
&g_hNotify[3]
);
if (FAILED(hr))
{
goto Error;
}
Error:
return hr;
}
//
// FUNCTION: HRESULT RegisterApps()
//
// PURPOSE: Register the application notifications.
//
HRESULT RegisterApps()
{
HRESULT hr;
NOTIFICATIONCONDITION nc;
TCHAR szExePath[MAX_PATH];
TCHAR szThisPath[MAX_PATH];
// Make sure we aren't already registered.
UnregisterApps();
// Get the path to this application.
GetModuleFileName(NULL, szThisPath, MAX_PATH);
// Need to put a quote before and after the path, in case the path contains spaces.
StringCchCopy(szExePath, MAX_PATH, L"\"");
StringCchCat(szExePath, MAX_PATH, szThisPath);
StringCchCat(szExePath, MAX_PATH, L"\"");
// Notify us of any change in the value and set the bitmask of the value to check.
nc.ctComparisonType = REG_CT_ANYCHANGE;
nc.dwMask = SN_POWERBATTERYSTRENGTH_BITMASK;
nc.TargetValue.dw = 0;
// Register battery strength notification.
hr = RegistryNotifyApp(
SN_POWERBATTERYSTRENGTH_ROOT,
SN_POWERBATTERYSTRENGTH_PATH,
SN_POWERBATTERYSTRENGTH_VALUE,
c_wszAppBattStrengthName,
szExePath, // Path to our app so that BattStat will be launched if it is not already running.
NULL,
NULL,
WM_CHANGE_BATTSTRENGTH,
RNAF_NONAMEONCMDLINE, // Don't pass along anything on the command line.
&nc
);
if (FAILED(hr))
{
goto Error;
}
nc.ctComparisonType = REG_CT_ANYCHANGE;
nc.dwMask = SN_POWERBATTERYSTATE_BITMASK;
nc.TargetValue.dw = 0;
// Register battery state notification.
hr = RegistryNotifyApp(
SN_POWERBATTERYSTATE_ROOT,
SN_POWERBATTERYSTATE_PATH,
SN_POWERBATTERYSTATE_VALUE,
c_wszAppBattStateName,
szExePath,
NULL,
NULL,
WM_CHANGE_BATTSTATE,
0, // Command line will contain "/notify <notification name>".
&nc
);
if (FAILED(hr))
{
goto Error;
}
nc.ctComparisonType = REG_CT_ANYCHANGE;
nc.dwMask = SN_POWERBATTERYBACKUPSTRENGTH_BITMASK;
nc.TargetValue.dw = 0;
// Register backup battery strength notification.
hr = RegistryNotifyApp(
SN_POWERBATTERYBACKUPSTRENGTH_ROOT,
SN_POWERBATTERYBACKUPSTRENGTH_PATH,
SN_POWERBATTERYBACKUPSTRENGTH_VALUE,
c_wszAppBackupStrengthName,
szExePath,
NULL,
NULL,
WM_CHANGE_BACKUPSTRENGTH,
RNAF_NONAMEONCMDLINE, // Don't pass along anything on the command line.
&nc
);
if (FAILED(hr))
{
goto Error;
}
nc.ctComparisonType = REG_CT_ANYCHANGE;
nc.dwMask = SN_POWERBATTERYBACKUPSTATE_BITMASK;
nc.TargetValue.dw = 0;
// Register backup battery state notification.
hr = RegistryNotifyApp(
SN_POWERBATTERYBACKUPSTATE_ROOT,
SN_POWERBATTERYBACKUPSTATE_PATH,
SN_POWERBATTERYBACKUPSTATE_VALUE,
c_wszAppBackupStateName,
szExePath,
NULL,
NULL,
WM_CHANGE_BACKUPSTATE,
0, // Command line will contain "/notify <notification name>".
&nc
);
if (FAILED(hr))
{
goto Error;
}
Error:
return hr;
}
//
// FUNCTION: HRESULT UnregisterWindows()
//
// PURPOSE: Unregister all window notifications.
//
HRESULT UnregisterWindows()
{
long lNotifyIndx;
// For each notification handle:
for (lNotifyIndx=0; lNotifyIndx < NOTIFY_CNT; lNotifyIndx++)
{
if (g_hNotify[lNotifyIndx] != NULL)
{
// Close off the notification.
RegistryCloseNotification(g_hNotify[lNotifyIndx]);
}
g_hNotify[lNotifyIndx] = 0;
}
return S_OK;
}
//
// FUNCTION: HRESULT UnregisterApps()
//
// PURPOSE: Unregister all application notifications.
//
HRESULT UnregisterApps()
{
RegistryStopNotification(c_wszAppBattStrengthName);
RegistryStopNotification(c_wszAppBattStateName);
RegistryStopNotification(c_wszAppBackupStrengthName);
RegistryStopNotification(c_wszAppBackupStateName);
return S_OK;
}
//
// FUNCTION: void BattStateCallback(HREGNOTIFY hNotify, DWORD dwUserData,
// const PBYTE pData, const UINT cbData)
//
// PURPOSE: Callback function that is called when battery state changes.
//
void BattStateCallback(HREGNOTIFY hNotify, DWORD dwUserData, const PBYTE pData, const UINT cbData)
{
DWORD dwBattState = 0;
PDWORD pdwData;
TCHAR szBattStateStr[STATUS_STRING_LEN];
StringCchCopy(szBattStateStr, STATUS_STRING_LEN, L"");
// Get the new value.
pdwData = (DWORD *)pData;
dwBattState = *pdwData;
// Apply our bitmap mask and form the battery state as a string.
dwBattState = dwBattState & SN_POWERBATTERYSTATE_BITMASK;
GetBattStateStr(dwBattState, szBattStateStr);
// Update the status text.
SetDlgItemText(g_hCurWndDialog, IDC_BATTSTATE, szBattStateStr);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -