⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setpin.cpp

📁 一个WinCE6。0下的IP phone的源代码
💻 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.
//
#include "SetPIN.hpp"
#include "settingsApp.hpp"
#include "resource.h"
#include "ControlDefinitions.h"
#include <Controls.hpp>
#include <PaintHelper.hpp>
#include <windowsx.h>
#include "Layout.hpp"
#include "VoIPNotify.hpp"
#include "SettingsApi.hpp"

const UINT   SetPINDialog_t::sc_idxOldPIN   = 0;
const UINT   SetPINDialog_t::sc_idxNewPIN   = 1;
const UINT   SetPINDialog_t::sc_idxConfirm  = 2;

const UINT c_rgSetPIN[] =
{
    IDC_PIN_OLD    ,
    IDC_PIN_NEW    ,
    IDC_PIN_CONFIRM,
};

SetPINDialog_t::SetPINDialog_t()
{
    m_ErrorDialog = NULL;
    m_HelpDialog = NULL;
    m_Control = NULL;

    ZeroMemory(&m_OldPIN, sizeof(m_OldPIN));

    m_ToLockPhone = PHGetSetting(phsLockPhoneAfterUpdatingPIN) ? true : false;
}

SetPINDialog_t::~SetPINDialog_t()
{
    if (m_ErrorDialog)
    {
        DestroyWindow(m_ErrorDialog);
    }

    if (m_HelpDialog)
    {
        DestroyWindow(m_HelpDialog);
    }
}

const WCHAR*
SetPINDialog_t::GetWindowClassName(
    void
    )
{
    return WNDCLASS_SETPIN;
}

HRESULT 
SetPINDialog_t::CreateDialogScreen(
    )
{ 
    HWND Dialog;
    HRESULT hr = Create(
        MAKEINTRESOURCE(IDD_SETPINDIALOG),
        NULL,
        dtModelessDialog,
        NULL,
        &Dialog
        );
    if (FAILED(hr))
    {
        return hr;
    }

    ASSERT(m_hwnd == Dialog);
    return S_OK;
}

HWND
SetPINDialog_t::GetDialog()
{
    return m_hwnd;
}

BOOL 
SetPINDialog_t::DialogWindowProc(
    UINT    Message,
    WPARAM  wParam,
    LPARAM  lParam,
    bool&   Handled
    )
{
    Handled = true;
    switch (Message)
    {
    case WM_INITDIALOG:
        if (FAILED(OnInitDialog()))
        {
            DestroyWindow(m_hwnd);
        }
        return FALSE;

    case WM_ERASEBKGND:
        return LogoScreen_t::OnEraseBackground(
            m_hwnd,
            IDB_LOGO_BIG,
            reinterpret_cast<HDC>(wParam),
            lParam
            );

    case WM_COMMAND:
        return SUCCEEDED(OnCommand(
            LOWORD(wParam),
            HIWORD(wParam),
            reinterpret_cast<HWND>(lParam)
            ));

    case WM_PAINT:
        return LogoScreen_t::OnPaint(
            m_hwnd
            );

    case WM_KEYDOWN:
        OnKeyDown(wParam);
        return FALSE;

    case WM_HELP:
        return SUCCEEDED(OnHelp());

    case WM_NOTIFY:
        switch(wParam)
        {
        case IDC_ERRORMSG_OK:
            DestroyWindow(m_ErrorDialog);
            m_ErrorDialog = NULL;
            SetFocus(m_Control);
            break;

        case IDC_HELPMSG_OK:
            DestroyWindow(m_HelpDialog);
            m_HelpDialog = NULL;
            SetFocus(m_Control);
            break;
        }
        break;

    default:
        return FALSE;
    }

    return FALSE;
}

HRESULT
SetPINDialog_t::OnInitDialog()
{
    for (INT i = 0; i < c_cSetPINEditControls; i++)
    {
        m_rgEdit[i] = GetDlgItem(m_hwnd, c_rgSetPIN[i]);
        SendMessage(m_rgEdit[i], EM_SETLIMITTEXT, c_PINLength, 0);
    }
    
    InitializeEditControls(); 

    m_MenuBar = GetDlgItem(m_hwnd, IDC_MENUBAR);
    m_MenuBar.SetMenu(
        GlobalData_t::s_ModuleInstance, 
        IDMB_SETPIN_BACKSPACE_DONE_BUTTONS
        );
    UpdateMenuBar(); 

    return S_OK;
}

LRESULT
SetPINDialog_t::OnCommand(
    WORD CommandId,
    WORD NotifyCode,
    HWND Control
    )
{
    LRESULT hr;

    switch (NotifyCode)
    {
    case EN_UPDATE:
    case EN_SETFOCUS:
        UpdateMenuBar();
        break;
    }

    switch (CommandId)
        {
        case IDOK:
            hr = OnDoneClick();
            if (FAILED(hr))
            {
                return hr;
            }
            return GetSettingsApp()->GoBackToPreviousScreen();
        case IDCANCEL:
            return GetSettingsApp()->GoBackToPreviousScreen();
        case IDC_BACKSPACE:
            SendMessage(
                GetFocus(), 
                WM_CHAR, 
                L'\b',
                0
                );
            return S_OK;
        default:
            return E_NOTIMPL;
        }
}

VOID
SetPINDialog_t::OnKeyDown(
    int Keycode
    )
{
    if (Keycode != VK_UP && Keycode != VK_DOWN)
    {
        return;
    }

    BOOL IsReadOnly = GetWindowLong(m_rgEdit[sc_idxOldPIN], GWL_STYLE) & ES_READONLY;
    HWND Focus = GetFocus();
    HWND NewFocus = NULL;
    int NewIndex = 0;

    for (int Index = 0; Index < c_cSetPINEditControls; Index++)
    {
        if (Focus == m_rgEdit[Index])
        {
            if (!IsReadOnly)
            {
                NewIndex = (Keycode == VK_UP) ? Index-1 : Index+1;
                if (NewIndex < 0 || NewIndex > c_cSetPINEditControls-1)
                {
                    NewIndex = (NewIndex < 0) ? NewIndex + c_cSetPINEditControls : NewIndex - c_cSetPINEditControls;
                }
                NewFocus = m_rgEdit[NewIndex % c_cSetPINEditControls];
                goto exit;
            }
            else
            {
                ASSERT(!(Focus == m_rgEdit[sc_idxOldPIN]));
                NewFocus = (Index == sc_idxConfirm) ? m_rgEdit[Index-1] : m_rgEdit[Index+1];
                goto exit;
            }
        }       
    }

exit:
    ASSERT(NewFocus);
    SetFocus(NewFocus);
    UpdateMenuBar();
    return;
}

HRESULT 
SetPINDialog_t::InitializeEditControls(
    void
    )
{
    PHReadPIN(
        m_OldPIN, 
        _countof(m_OldPIN)
        ); 

    if (! m_OldPIN[0])
    {
        SendMessage(m_rgEdit[sc_idxOldPIN], EM_SETREADONLY, TRUE, 0);
        SetFocus(m_rgEdit[sc_idxNewPIN]);
    }
    else
    {
        SetFocus(m_rgEdit[sc_idxOldPIN]);
    }

    return S_OK;
}

VOID
SetPINDialog_t::UpdateMenuBar(
    void
    )
{
    // Just need to check first char to decide "Backspace" and "Done" show or hide
    WCHAR       OldPIN[2]       = L"";
    WCHAR       NewPIN[2]       = L"";
    WCHAR       ConfirmPIN[2]   = L"";

    HWND Focus = GetFocus();
    GetWindowText(m_rgEdit[sc_idxOldPIN] , OldPIN    , _countof(OldPIN));
    GetWindowText(m_rgEdit[sc_idxNewPIN] , NewPIN    , _countof(NewPIN));
    GetWindowText(m_rgEdit[sc_idxConfirm], ConfirmPIN, _countof(ConfirmPIN)); 

    bool ShowDoneButton = (NewPIN[0] && ConfirmPIN[0]) && 
                          (OldPIN[0] || ! m_OldPIN[0]);
    
    bool FocusedControlHasText = (Focus == m_rgEdit[sc_idxOldPIN] && OldPIN[0])       ||
                                 (Focus == m_rgEdit[sc_idxNewPIN] && NewPIN[0])       ||
                                 (Focus == m_rgEdit[sc_idxConfirm] && ConfirmPIN[0]);

    m_MenuBar.ShowMenuButton(IDOK, ShowDoneButton);
    m_MenuBar.ShowMenuButton(IDC_BACKSPACE, FocusedControlHasText);
    
    return;
}

HRESULT 
SetPINDialog_t::OnDoneClick(
    void
    )
{
    WCHAR   wszOld[c_PINLength + 1] = L"";
    WCHAR   wszNew[c_PINLength + 1] = L"";
    WCHAR   wszCon[c_PINLength + 1] = L"";

    UINT    ErrorId = 0;
    
    GetWindowText(m_rgEdit[sc_idxOldPIN], wszOld, _countof(wszOld));
    GetWindowText(m_rgEdit[sc_idxNewPIN], wszNew, _countof(wszNew));
    GetWindowText(m_rgEdit[sc_idxConfirm], wszCon, _countof(wszCon));

    if (m_OldPIN[0] && wcsncmp(m_OldPIN, wszOld, c_PINLength) != 0)
    {
        ErrorId = IDS_ERROR_POPUP_WRONGPIN;
        goto error;
    }

    if (wcscmp(wszNew, wszCon) != 0)
    {
        ErrorId = IDS_ERROR_POPUP_PINMISMATCH;
        goto error;
    }

    if (wcslen(wszNew) != c_PINLength)
    {
        ErrorId = IDS_ERROR_POPUP_PINTOOSMALL;
        goto error;
    }

    // setRegistry
    HRESULT hr = PHWritePIN(
        wszNew
        ); 
    ASSERT(SUCCEEDED(hr));

    if (m_ToLockPhone)
    {
        PHSetValue(phsDeviceLocked, 1);
    }

    PHSetValue(phsLockPhoneAfterUpdatingPIN, 0);

    return S_OK;

error:
    PH_MESSAGE_BOX_PARAMETERS MessageBoxParameters = {0}; 
    MessageBoxParameters.StructSize = sizeof(MessageBoxParameters);
    MessageBoxParameters.Flags      = VDF_TYPE_MODELESS;
    MessageBoxParameters.Owner      = m_hwnd; 
    MessageBoxParameters.Instance   = GlobalData_t::s_ModuleInstance;
    MessageBoxParameters.pTitle     = CommonUtilities_t::LoadString(GlobalData_t::s_ModuleInstance, IDS_TITLE_ERROR);
    MessageBoxParameters.pText      = CommonUtilities_t::LoadString(GlobalData_t::s_ModuleInstance, ErrorId);
    MessageBoxParameters.IconId     = IDB_POPUP_ERROR;
    MessageBoxParameters.MenuId     = IDMB_MSGBOX_ERROR; 

    m_Control = GetFocus();

    if (!PHMessageBox(
            &MessageBoxParameters
            ))
    {
        ASSERT(FALSE);
    }

    m_ErrorDialog = MessageBoxParameters.result.Dialog;  
    return E_FAIL;
}

HRESULT
SetPINDialog_t::OnHelp(
    void                                    
    )
{
    if (IsWindow(m_ErrorDialog) || IsWindow(m_HelpDialog))
    {
        return E_FAIL;
    }
    
    m_Control = GetFocus();

    PH_MESSAGE_BOX_PARAMETERS   Params = {0};
    Params.StructSize = sizeof(Params);
    Params.Flags      = VDF_TYPE_MODELESS;
    Params.IconId     = IDB_HELP_ICON;
    Params.Instance   = GlobalData_t::s_ModuleInstance;
    Params.Owner      = m_hwnd;
    Params.MenuId     = IDMB_MSGBOX_HELP;
    Params.pTitle     = CommonUtilities_t::LoadString(GlobalData_t::s_ModuleInstance, IDS_TITLE_HELP);
    Params.pText      = CommonUtilities_t::LoadString(GlobalData_t::s_ModuleInstance, IDS_SCREENHELP_DEFAULT);

    if (! PHMessageBox(&Params))
    {
        ASSERT(FALSE);
        return E_FAIL;
    }

    m_HelpDialog = Params.result.Dialog;
    return S_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -