📄 lmidialog.cpp
字号:
#include <windows.h>
#include <aygshell.h>
#include "LmiWindows.h"
#include "LmiGlobals.h"
#include "LmiDialog.h"
#include "LmiDoodler.h"
#include "resource.h"
//
// Various internal definitions to manage the dialog state
//
enum LMISTATE
{
lmistate_get_access = 0,
lmistate_get_new,
lmistate_repeat_new
};
typedef struct
{
BOOL fPowerOn;
LMISTATE state;
void* pNewDoodle;
void* pRepeatDoodle;
HWND hwndDoodler;
HFONT hfontCaption;
} LMIINFO;
LPTSTR l_pszObjectClassName = TEXT("CPL_LetMeIn");
//
// This function ensures that we have control of the desktop password,
// i.e. the password hasn't been set already by another password
// mechanism.
//
BOOL LmiDialog_HaveControl(
HWND hwnd )
{
if ( CheckPassword(0) )
return TRUE;
if ( CheckPassword( g_pszDesktopPassword ) )
return TRUE;
MessageBox( hwnd,
TEXT( "Another security module has control " )
TEXT( "of the password. The password must be " )
TEXT( "removed before installing the 'Let Me In' " )
TEXT( "security module." ),
g_pszAppletCaption,
MB_OK|MB_ICONEXCLAMATION|MB_SETFOREGROUND );
return FALSE;
}
//
// Save our security information, activate the desktop password,
// and turn on power-on security.
//
BOOL LmiDialog_EnableSecurity(
HWND hwnd,
BYTE* pData,
int nSize )
{
HKEY hkey;
if ( !LmiDialog_HaveControl( hwnd ) )
return FALSE;
//
// Save doodle info
//
RegCreateKeyEx( HKEY_CURRENT_USER, g_pszLMIRegKey, 0, 0, 0, 0, 0, &hkey, 0 );
RegSetValueEx( hkey, g_pszLMIRegDoodle, 0, REG_BINARY, pData, nSize );
RegCloseKey( hkey );
//
// Enable password protection.
//
if ( CheckPassword(0) )
SetPassword( 0, g_pszDesktopPassword );
SetPasswordActive( TRUE, g_pszDesktopPassword );
//
// Enable power-on dialog.
//
RegCreateKeyEx( HKEY_CURRENT_USER, TEXT("ControlPanel\\Owner"), 0, 0, 0, 0, 0, &hkey, 0 );
RegSetValueEx( hkey, TEXT("PowrPass"), 0, REG_BINARY, (CONST BYTE*)"\x01", 1 );
RegCloseKey( hkey );
return TRUE;
}
//
// Disable desktop password and power-on security.
//
BOOL LmiDialog_ClearSecurity()
{
HKEY hkey;
//
// Disable password protection.
//
SetPasswordActive( FALSE, g_pszDesktopPassword );
SetPassword( g_pszDesktopPassword, 0 );
//
// Disable power-on dialog.
//
RegCreateKeyEx( HKEY_CURRENT_USER, TEXT("ControlPanel\\Owner"), 0, 0, 0, 0, 0, &hkey, 0 );
RegSetValueEx( hkey, TEXT("PowrPass"), 0, REG_BINARY, (CONST BYTE*)"\x00", 1 );
RegCloseKey( hkey );
return TRUE;
}
//
// Get current security doodle from the registry.
//
void* LmiDialog_GetSecurity()
{
HKEY hkey;
BYTE* pData;
DWORD dwData = 0;
RegCreateKeyEx( HKEY_CURRENT_USER, g_pszLMIRegKey, 0, 0, 0, 0, 0, &hkey, 0 );
RegQueryValueEx( hkey, g_pszLMIRegDoodle, 0, 0, 0, &dwData );
if ( dwData )
{
pData = (BYTE*)LocalAlloc( 0, dwData );
RegQueryValueEx( hkey, g_pszLMIRegDoodle, 0, 0, pData, &dwData );
}
RegCloseKey( hkey );
return dwData ? pData : 0;
}
//
// Our sample dialog handles two different "modes". Update mode is used when
// the user is changing the security settings. Power-on mode is used to
// secure the the device at power-on.
//
BOOL CALLBACK LmiDialog_DlgProc(
HWND hDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam )
{
LMIINFO* pli = (LMIINFO*)GetWindowLong( hDlg, DWL_USER );
switch( uMsg )
{
case WM_INITDIALOG:
//
// Initialize our dialog instance data
//
pli = (LMIINFO*)LocalAlloc( 0, sizeof(LMIINFO) );
SetWindowLong( hDlg, DWL_USER, (LONG)pli );
pli->fPowerOn = (BOOL)lParam;
pli->state = lmistate_get_access;
pli->hwndDoodler = GetDlgItem( hDlg, CONTROL_DOODLER );
//
// Center the doodle control relative to the screen
//
{
RECT rcControl;
int x;
GetWindowRect( pli->hwndDoodler, &rcControl );
ScreenToClient( hDlg, (POINT*)&rcControl.left );
ScreenToClient( hDlg, (POINT*)&rcControl.right );
x = ( GetSystemMetrics(SM_CXSCREEN) - ( rcControl.right - rcControl.left ) ) / 2;
SetWindowPos( pli->hwndDoodler, (HWND)0, x, rcControl.top, 0, 0,
SWP_NOSIZE|SWP_NOZORDER );
}
//
// Configure the dialog depending on whether we're in Update or Power-on mode.
//
if ( pli->fPowerOn )
{
//
// Set the caption bar text
//
SHSetNavBarText( hDlg, TEXT("Security Is Enabled") );
//
// When powering up, the only UI component is the input control
// (and the panic button for debug builds)
//
#if ( WIN32_PLATFORM_PSPC >= 310 )
{
//
// Don't show "X" button on Pocket PC 2002 and later
//
DWORD dwStyle = GetWindowLong( hDlg, GWL_STYLE );
dwStyle |= WS_NONAVDONEBUTTON;
SetWindowLong( hDlg, GWL_STYLE, dwStyle );
}
#endif
SetDlgItemText( hDlg, LABEL_PROMPT, TEXT("Enter security doodle:") );
SetForegroundWindow( hDlg );
SHFullScreen( hDlg, SHFS_HIDESTARTICON );
#ifdef DEBUG
//
// Panic button while debugging power-on.
//
SetDlgItemText( hDlg, IDCANCEL, TEXT("Panic") );
ShowWindow( GetDlgItem( hDlg, IDCANCEL ), SW_SHOW );
#endif
}
else
{
SHMENUBARINFO smbi;
//
// Create an empty menubar to make the dialog look like other
// control panel apps.
//
smbi.cbSize = sizeof(SHMENUBARINFO);
smbi.hwndParent = hDlg;
smbi.dwFlags = SHCMBF_EMPTYBAR;
SHCreateMenuBar(&smbi);
//
// Set the caption bar text
//
SHSetNavBarText( hDlg, TEXT("Settings") );
//
// A little extra to make a sub-caption like stock settings
// pages.
//
{
int h = GetSystemMetrics(SM_CYCAPTION);
HDC hdc;
HWND hSubCaption;
TCHAR face[64];
LOGFONT lf;
TEXTMETRIC tm;
CreateWindowEx( 0, TEXT("STATIC"), TEXT(""), WS_VISIBLE|WS_BORDER,
-10, -10, 300, h+10, hDlg, (HMENU)-1, g_hInstance, 0 );
hSubCaption = CreateWindowEx( 0, TEXT("STATIC"), g_pszAppletCaption, WS_VISIBLE|SS_LEFTNOWORDWRAP , 4, 4, 300, h-4-1,
hDlg, (HMENU)LABEL_CAPTION, g_hInstance, 0 );
SetWindowPos( hSubCaption, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
hdc = GetDC( GetDlgItem( hDlg, LABEL_CAPTION ) );
GetTextMetrics( hdc, &tm );
GetTextFace( hdc, 64, face );
ReleaseDC( hDlg, hdc );
memset( &lf, 0, sizeof(lf) );
lf.lfHeight = tm.tmHeight-1;
lf.lfWeight = FW_BOLD;
lstrcpy( lf.lfFaceName, face );
pli->hfontCaption = ::CreateFontIndirect( &lf );
SendDlgItemMessage( hDlg, LABEL_CAPTION, WM_SETFONT, (WPARAM)pli->hfontCaption, TRUE );
}
//
// Enable the additional elements for updating the password.
//
if ( !GetPasswordActive() )
{
//
// If no password is active, don't ask for one
//
SendMessage( pli->hwndDoodler, DIM_SECURE, 0, 0 ); // Enable doodle feedback.
pli->state = lmistate_get_new;
SetDlgItemText( hDlg, LABEL_PROMPT, TEXT("Enter new doodle:") );
}
else
{
//
// Password is active, the user needs to know the security doodle
// before continuing.
//
SetDlgItemText( hDlg, LABEL_PROMPT, TEXT("Enter security doodle:") );
//
// The user checks this box if they just want to delete the current
// security code and not enter a new one.
//
ShowWindow( GetDlgItem( hDlg, CHECKBOX_DISABLE ), SW_SHOW );
}
ShowWindow( GetDlgItem( hDlg, IDCANCEL ), SW_SHOW );
}
return FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -