📄 mybacklight.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//
// mybacklight.cpp
//
#include "myBackLight.h"
#define NUM_APPLETS 1
#define ID_BATT 0
#define ID_AC 1
// The registry values pertaining to the backlight settings:
//
// [HKEY_CURRENT_USER\ControlPanel\BackLight]
// "BatteryTimeoutUnchecked" ; On Battery Power timeout, saved here when checkbox is unchecked, 0 otherwise (seconds)
// "BatteryTimeout" ; On Battery Power timeout, saved here when checkbox is checked, 0 otherwise (seconds)
// "ACTimeoutUnchecked" ; On AC Power timeout, saved here when checkbox is unchecked, 0 otherwise (seconds)
// "ACTimeout" ; On AC Power timeout, saved here when checkbox is checked, 0 otherwise (seconds)
// "BacklightOnTap" ; Whether to turn on backlight when user taps screen, when on battery power
// "ACBacklightOnTap" ; As above, but when on AC Power
//
// BacklightOnTap and ACBacklightOnTap are not demonstrated in this sample
//
#define REGKEY TEXT("ControlPanel\\Backlight")
#define MYREGKEY TEXT("ControlPanel\\MyBacklight")
#define BRIGHTNESS TEXT("Brightness")
#define ACTIMEOUT TEXT("ACTimeout")
#define ACTIMEOUTUNCHECKED TEXT("ACTimeoutUnchecked")
#define BATTERYTIMEOUT TEXT("BatteryTimeout")
#define BATTERYTIMEOUTUNCHECKED TEXT("BatteryTimeoutUnchecked")
typedef enum tagRegistryOp
{
eBrightness,
eACPower,
eBatt
} REGISTRYOP;
typedef struct tagApplets
{
int icon; // icon resource identifier
int namestring; // name-string resource identifier
int descstring; // description-string resource identifier
} APPLETS;
const APPLETS SystemApplets[] =
{
{APPLET_ICON, APPLET_NAME, APPLET_DESC}
// add more struct members here if supporting more than on applet
};
HINSTANCE g_hInstance = NULL;
BOOL CreatePropertySheet(HWND hWnd, int iApplet);
////////////////////////////////////////////////////////
// DllMain
//
////////////////////////////////////////////////////////
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
if (DLL_PROCESS_ATTACH == ul_reason_for_call)
g_hInstance = (HINSTANCE)hModule;
return TRUE;
}
////////////////////////////////////////////////////////
// Do initialization, eg memory allocations, etc.
// return TRUE if all OK and FALSE if applet shouldn't
// be loaded
//
////////////////////////////////////////////////////////
BOOL InitApplet(HWND hwndParent)
{
return TRUE;
}
////////////////////////////////////////////////////////
// Do cleanup here
//
////////////////////////////////////////////////////////
void TermApplet()
{
return;
}
////////////////////////////////////////////////////////
// This is the entry point called by ctlpnl.exe
//
////////////////////////////////////////////////////////
extern "C"
__declspec(dllexport)
LONG WINAPI CPlApplet(HWND hwndCPL, UINT uMsg, LONG lParam1, LONG lParam2)
{
static int iInitCount = 0;
int iApplet;
switch (uMsg)
{
// First message sent. It is sent only once to
// allow the dll to initialize it's applet(s)
case CPL_INIT:
if (!iInitCount)
{
if (!InitApplet(hwndCPL))
return FALSE;
}
iInitCount++;
return TRUE;
// Second message sent. Return the count of applets supported
// by this dll
case CPL_GETCOUNT:
// Return the number of applets we support
return (LONG)((sizeof(SystemApplets))/(sizeof(SystemApplets[0])));
// Third message sent. Sent once for each applet supported by this dll.
// The lParam1 contains the number that indicates which applet this is
// for, from 0 to 1 less than the count of applets.
// lParam2 is a NEWCPLINFO that should be filled with information about
// this applet before returning
case CPL_NEWINQUIRE:
{
LPNEWCPLINFO lpNewCPlInfo;
lpNewCPlInfo = (LPNEWCPLINFO)lParam2;
iApplet = (int)lParam1;
lpNewCPlInfo->dwSize = (DWORD)sizeof(NEWCPLINFO);
lpNewCPlInfo->dwFlags = 0;
lpNewCPlInfo->dwHelpContext = 0;
lpNewCPlInfo->lData = SystemApplets[iApplet].icon;
lpNewCPlInfo->hIcon = LoadIcon(g_hInstance,(LPCTSTR)MAKEINTRESOURCE(SystemApplets[iApplet].icon));
lpNewCPlInfo->szHelpFile[0] = '\0';
LoadString(g_hInstance,SystemApplets[iApplet].namestring,lpNewCPlInfo->szName,32);
LoadString(g_hInstance,SystemApplets[iApplet].descstring,lpNewCPlInfo->szInfo,64);
}
break;
// This is sent whenever the user clicks an icon in Settings for one of
// the applets supported by this dll. lParam1 contains the number indicating
// which applet. Return 0 if applet successfully launched, non-zero otherwise
case CPL_DBLCLK:
iApplet = (UINT)lParam1;
if (!CreatePropertySheet(hwndCPL,iApplet))
return 1;
break;
// Sent once per applet, before CPL_EXIT
case CPL_STOP:
break;
// Sent once before the dll is unloaded
case CPL_EXIT:
iInitCount--;
if (!iInitCount)
TermApplet();
break;
default:
break;
}
return 0;
}
////////////////////////////////////////////////////////
// Called to retrieve the values corresponding to the
// backlight stored in the registry. The necessary
// registry values are described above
//
////////////////////////////////////////////////////////
HRESULT GetFromRegistry(REGISTRYOP eVal, DWORD *pdwValueChecked, DWORD *pdwValueUnchecked)
{
HKEY hKey;
DWORD dwSize;
DWORD dwType;
HRESULT hRes;
TCHAR *szRegistryKey;
TCHAR *szRegValue1;
TCHAR *szRegValue2;
if (pdwValueChecked)
*pdwValueChecked = 0;
if (pdwValueUnchecked)
*pdwValueUnchecked = 0;
switch (eVal)
{
case eBrightness:
szRegistryKey = MYREGKEY;
szRegValue1 = BRIGHTNESS;
break;
case eACPower:
szRegistryKey = REGKEY;
szRegValue1 = ACTIMEOUT;
szRegValue2 = ACTIMEOUTUNCHECKED;
break;
case eBatt:
szRegistryKey = REGKEY;
szRegValue1 = BATTERYTIMEOUT;
szRegValue2 = BATTERYTIMEOUTUNCHECKED;
break;
default:
return E_FAIL;
}
hRes = RegOpenKeyExW(HKEY_CURRENT_USER,szRegistryKey,0,KEY_ALL_ACCESS,&hKey);
if (hRes != ERROR_SUCCESS)
goto Done;
if (pdwValueChecked)
{
dwSize = sizeof(DWORD);
hRes = RegQueryValueExW(hKey,szRegValue1,0,&dwType,(LPBYTE)pdwValueChecked,&dwSize);
if (hRes != ERROR_SUCCESS)
goto Done;
}
if (pdwValueUnchecked)
{
dwSize = sizeof(DWORD);
hRes = RegQueryValueExW(hKey,szRegValue2,0,&dwType,(LPBYTE)pdwValueUnchecked,&dwSize);
}
Done:
RegCloseKey(hKey);
return hRes;
}
////////////////////////////////////////////////////////
// Called to save backlight related values to the
// registry
//
////////////////////////////////////////////////////////
HRESULT SetToRegistry(REGISTRYOP eVal,DWORD dwValueChecked,DWORD dwValueUnchecked)
{
HKEY hKey;
DWORD dwDisp;
HRESULT hRes;
TCHAR *szRegistryKey;
TCHAR *szRegValue1;
TCHAR *szRegValue2;
switch (eVal)
{
case eBrightness:
szRegistryKey = MYREGKEY;
szRegValue1 = BRIGHTNESS;
break;
case eACPower:
szRegistryKey = REGKEY;
szRegValue1 = ACTIMEOUT;
szRegValue2 = ACTIMEOUTUNCHECKED;
break;
case eBatt:
szRegistryKey = REGKEY;
szRegValue1 = BATTERYTIMEOUT;
szRegValue2 = BATTERYTIMEOUTUNCHECKED;
break;
default:
return E_FAIL;
}
hRes = RegCreateKeyExW(HKEY_CURRENT_USER, szRegistryKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisp);
if (hRes != ERROR_SUCCESS)
{
TCHAR szDebug[80];
wsprintf(szDebug,L"RegCreateKeyExW failed with %d\n",hRes);
OutputDebugString(szDebug);
return hRes;
}
else
OutputDebugString(L"RegCreateKeyExW succeeded\n");
hRes = RegSetValueExW(hKey,szRegValue1,0,REG_DWORD,(LPBYTE)&dwValueChecked,sizeof(DWORD));
if (eVal != eBrightness)
hRes = RegSetValueExW(hKey,szRegValue2,0,REG_DWORD,(LPBYTE)&dwValueUnchecked,sizeof(DWORD));
RegCloseKey(hKey);
return hRes;
}
////////////////////////////////////////////////////////
// The DialogProc for the On Battery Power property page
//
////////////////////////////////////////////////////////
int CALLBACK BattPageProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
HWND hwndSlider;
HWND hwndCheck;
DWORD dwValueChecked;
DWORD dwValueUnchecked;
DWORD dwValue;
LPPROPSHEETPAGE ppsp;
TCHAR szStatic[16];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -