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

📄 editctrl.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 "Edit.hpp"
#include <winuserm.h>
#include "Input.hpp"
#include "Common.hpp"
#include "Debug.hpp"


/*------------------------------------------------------------------------------
    EditImpl_t::EditImpl_t

    Constructor
------------------------------------------------------------------------------*/
EditImpl_t::EditImpl_t(
    )
{
    TRACE(ZONE_COMMON_CTOR);
}

/*------------------------------------------------------------------------------
    EditImpl_t::~EditImpl_t

    Destructor
------------------------------------------------------------------------------*/
EditImpl_t::~EditImpl_t(
    )
{
    TRACE(ZONE_COMMON_CTOR);
}

/*------------------------------------------------------------------------------
    EditImpl_t::ControlWindowProc

    Handle messages
------------------------------------------------------------------------------*/
LRESULT
EditImpl_t::ControlWindowProc(
    UINT Message,
    WPARAM wParam,
    LPARAM lParam,
    bool& Handled
    )
{
    LRESULT Result;

    //by default assume we handled the message
    Handled = true;

    switch (Message)
    {
    case EM_SETINPUTMODE:
        return OnSetInputMode(wParam, lParam);

    case WM_CHAR:
        return OnChar(wParam, lParam);

    case WM_CREATE:
        return OnCreate(reinterpret_cast<CREATESTRUCT*>(lParam));

    case WM_KEYDOWN:
        return OnKeydown(wParam, lParam);

    case WM_KILLFOCUS:
        return OnKillFocus(wParam, lParam);

    case WM_SETFOCUS:
        return OnSetFocus(wParam, lParam);

    case WM_CLOSE:
        Handled = ForwardMessageToParent(Message, wParam, lParam, &Result);
        return Result;

    case WM_COMMON_GETCALLBACK_PTR:
        return OnGetCallback();

    case WM_COMMON_SETCALLBACK_PTR:
        return OnSetCallback(lParam);

    default:
        Handled = false;
        return 0;
    }
}

bool
EditImpl_t::IsDialPad(
    )
{
    return (GetWindowLong(m_hwnd, GWL_STYLE) & VES_DIALPAD) ? true : false;
}

/*------------------------------------------------------------------------------
    EditImpl_t::OnSetCallback

    Handles VMSG_PTR_SETCALLBACK

    Sets a parent defined identifier with which we pass back to the parent
    on notification events
------------------------------------------------------------------------------*/
LRESULT
EditImpl_t::OnSetCallback(
    LPARAM lParam
    )
{
    m_pCallback = (INT_PTR)lParam;

    return S_OK;
}

/*------------------------------------------------------------------------------
    EditImpl_t::OnGetCallback

    Handles VMSG_PTR_GETCALLBACK

    Gets a parent defined identifier with which we pass back to the parent
    on notification events
------------------------------------------------------------------------------*/
LRESULT
EditImpl_t::OnGetCallback(
    )
{
    return (LRESULT)m_pCallback;
}

/*------------------------------------------------------------------------------
    EditImpl_t::OnCreate

    Handles WM_CREATE
------------------------------------------------------------------------------*/
LRESULT
EditImpl_t::OnCreate(
    CREATESTRUCT* pCreateStruct
    )
{
    m_InputMode  = (pCreateStruct) ?
        reinterpret_cast<UINT>(pCreateStruct->lpCreateParams) :
        EIM_NUMBERS;

    m_IsFirstTime = true;

    return 0;
}


/*------------------------------------------------------------------------------
    EditImpl_t::OnKillFocus

    Handles WM_KILLFOCUS

    Force redraw, and arbitrarily hide the Sip panel
------------------------------------------------------------------------------*/
LRESULT
EditImpl_t::OnKillFocus(
    WPARAM wParam,
    LPARAM lParam
    )
{
    HWND NewFocus = reinterpret_cast<HWND>(wParam);

    //force redraw
    InvalidateRect(m_hwnd, NULL, TRUE);
    UpdateWindow(m_hwnd);

    if (NewFocus != NULL)
    {
        // Get the type of the control which will get the focus
        CommonUtilities_t::ControlType_e Type =
            CommonUtilities_t::GetControlType(NewFocus);

        switch (Type)
        {
        case CommonUtilities_t::ControlTypeMenuButton:
            goto exit;

        case CommonUtilities_t::ControlTypeEdit:
            if (!(GetWindowLong(NewFocus, GWL_STYLE) & (ES_NUMBER | ES_READONLY)))
            {
                goto exit;
            }
            break;
        }
    }

    Input_HandleEvent(
        Input_t::ieEditControlLostFocus,
        reinterpret_cast<Input_t::IPARAM>(m_hwnd)
        );

exit:
    return DefWindowProc(WM_KILLFOCUS, wParam, lParam);
}


/*------------------------------------------------------------------------------
    EditImpl_t::OnSetFocus

    Handles WM_SETFOCUS
------------------------------------------------------------------------------*/
LRESULT
EditImpl_t::OnSetFocus(
    WPARAM wParam,
    LPARAM lParam
    )
{
    LRESULT Result = 0;

    if (IsDialPad())
    {
        SetNumbersOnly(true);
    }

    if (!(GetWindowLong(m_hwnd, GWL_STYLE) & (ES_NUMBER | ES_READONLY)))
    {
        SendMessage(
            m_hwnd,
            EM_SETINPUTMODE,
            0,
            (LPARAM)m_InputMode
            );
    }

    //force redraw
    InvalidateRect(m_hwnd, NULL, TRUE);
    UpdateWindow(m_hwnd);

    Result = DefWindowProc(WM_SETFOCUS, wParam, lParam);

    // If it is the first time got focus, always put caret to the END of the edit box
    // otherwise, stay wherever it used to be
    if (m_IsFirstTime)
    {
        DefWindowProc(WM_KEYDOWN, (WPARAM)VK_END, 0);
        m_IsFirstTime = false;
    }

    Input_HandleEvent(
        Input_t::ieEditControlGainFocus,
        reinterpret_cast<Input_t::IPARAM>(m_hwnd)
        );

    NotifyParent(VNM_SETFOCUS);

    return Result;
}

LRESULT
EditImpl_t::OnSetInputMode(
    WPARAM wParam,
    LPARAM lParam
    )
{
    m_InputMode = static_cast<UINT>(lParam);
    return DefWindowProc(EM_SETINPUTMODE, wParam, lParam);
}

/*------------------------------------------------------------------------------
    EditImpl_t::OnKeyDown

    Handles WM_KEYDOWN

    Notifies the parent window that KEYDOWN was processed
------------------------------------------------------------------------------*/
LRESULT
EditImpl_t::OnKeydown(
    WPARAM wParam,
    LPARAM lParam
    )
{
    int keycode = static_cast<int>(wParam);
    LRESULT Result = 0;

//    if (!Input_ShouldDispatchKey(keycode))
//    {
//        return Result;
//    }

    switch (keycode)
    {
    case VK_UP:
    case VK_DOWN:
        ForwardMessageToParent(WM_KEYDOWN, wParam, lParam);
        break;

    default:
        return DefWindowProc(WM_KEYDOWN, wParam, lParam);
    }

    return Result;
}

/*------------------------------------------------------------------------------
    EditImpl_t::OnChar

    Handles WM_CHAR

    Notifies our parent when we handle a char. Instead of relying on EN_UPDATE
    we specifically send a ctrl notify message to our parent, since we assume all of our
    parent don't handle CHAR's other than as reporting mechanisms
------------------------------------------------------------------------------*/
LRESULT
EditImpl_t::OnChar(
    WPARAM wParam,
    LPARAM lParam
    )
{
    if (IsDialPad())
    {
        if ((wParam < L'0' || wParam > L'9') &&
            (wParam != L'*') &&
            (wParam != L'#') &&
            (wParam != L'\b'))
        {
            return 0;
        }
    }

    LRESULT Result = DefWindowProc(WM_CHAR, wParam, lParam);

    // Notify parent of an update to the control
    NotifyParent(EN_UPDATE);

    return Result;
}

bool
EditImpl_t::SetNumbersOnly(
    bool Enable
    )
{
    DWORD Style = GetWindowLong(m_hwnd, GWL_STYLE);

    if (Enable)
    {
        Style |= ES_NUMBER;
    }
    else
    {
        Style &= ~ES_NUMBER;
    }

    return (SetWindowLong(m_hwnd, GWL_STYLE, Style) != 0);
}

⌨️ 快捷键说明

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