Click here to Skip to main content
15,886,026 members
Articles / Mobile Apps / Windows Mobile

Pocket PC Numeric Key Pad

Rate me:
Please Sign up or sign in to vote.
4.40/5 (6 votes)
3 Sep 2005CPOL2 min read 74K   380   35   10
An easier way to accept numeric input on the Pocket PC.

Sample Image - ppc_numpad.jpg

Introduction

Numeric input on the Pocket PC has always been a hassle. The numeric keyboard provided by the SIP is too small and not directly accessible. Plus, it's always nice to be able to perform some calculations before entering data.

With these issues in mind, I developed the numeric key pad dialog to help entering numeric data in Pocket PC applications.

Implementation

The sample application I include in this article implements the numeric key pad using two different approaches: window controls and windowless controls.

The first approach uses regular Windows controls such as buttons and static labels to draw the keypad. Instead of a static dialog resource that can be found in regular applications, all controls are created from a dynamically created dialog resource. The class CDialogTemplate (see sample project) is based on FlipCode's Dialog Template code by Max McGuire.

The second approach (depicted on the picture above) doesn't use window controls, but objects that derive from CSubWnd. These objects live on a CSubWndContainer that is an MFC's CWnd and is responsible for dispatching all incoming (and relevant) Windows messages to the contained CSubWnd objects. Each CSubWnd-derived object is responsible for painting its own rectangle and processing clicks. Here's how CSubWndContainer dispatches the paint requests:

void CSubWndContainer::OnPaint() 
{
    CPaintDC              dc(this);
    CSubWndCont::iterator pos,
                          end = m_cont.end();
    HFONT                 hFontOld;

    hFontOld = (HFONT)::SelectObject(dc, m_hFont);

    //
    // Paint all sub windows
    //
    for(pos = m_cont.begin(); pos != end; ++pos)
        if(dc.RectVisible(&(*pos)->GetRect()))
            (*pos)->Paint(dc);

    ::SelectObject(dc, hFontOld);
}

To make click handling easier (and to reduce the number of virtual functions), all click-related Windows messages are dispatched to a single CSubWnd function:

void CSubWndContainer::OnLButtonDown(UINT nFlags, CPoint point) 
{
    m_pSubWnd = SubWndFromPoint(point);
    if(m_pSubWnd)
        m_pSubWnd->Click(CLICK_DOWN, point);
}

void CSubWndContainer::OnLButtonUp(UINT nFlags, CPoint point) 
{
    if(m_pSubWnd)
        m_pSubWnd->Click(CLICK_UP, point);
}
void CSubWndContainer::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
    m_pSubWnd = SubWndFromPoint(point);
    if(m_pSubWnd)
        m_pSubWnd->Click(CLICK_DOWN | CLICK_DOUBLE, point);
}

The sample code uses two CSubWnd-derived classes: CSubStatic used to implement the topmost static control and CSubButton that implements all the keypad buttons. Note how this class has to work around the Windows built-in double-click processing:

void CSubButton::Click(DWORD dwClick, POINT pt)
{
    bool bOn = (dwClick & CLICK_DOWN);

    ASSERT(m_pWnd);

    //
    // Make a double click work right
    //
    if(dwClick & CLICK_DOUBLE)
        Click(CLICK_UP, pt);

    if(m_rc.PtInRect(pt))
    {
        //
        // Inside this button
        //
        if(IsActive() && !bOn)
        {
            CWnd* pParent = m_pWnd->GetParent();

            if(pParent)
                pParent->SendMessage(WM_COMMAND, m_nID, 
                                     (LPARAM)m_pWnd->GetSafeHwnd());
        }

        Activate(bOn);
    }
    else
    {
        //
        // Outside this button
        //
        if(IsActive())
            Activate(false);
    }
}

The numeric keypad implementation is quite straightforward. The value being edited is always a double that can be converted to an int or a CURRENCY. It is displayed in a format that the client chooses:

enum Format
{
    fmtInteger,   // An integer
    fmtNumber,    // Locale number
    fmtCurrency   // Locale currency
};

To consume this class, create an instance of CNumPadDlg using the appropriate constructor arguments:

CNumPadDlg(enum Format format, BOOL bTemplate, CWnd* pParent = NULL);

Before calling the object's DoModal function, set the keypad value using one of:

void SetIntValue(int nVal);
void SetDblValue(double dblVal);
void SetCurValue(CURRENCY cyVal);

Retrieve the keypad value using the corresponding get function:

int      GetIntValue();
double   GetDblValue();
CURRENCY GetCurValue();

And that's it!

Final Notes

This code uses Giuseppe Govi's STL implementation. It's a bit minimal but sufficient for most Pocket PC applications (and is compatible with eVC3).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Frotcom International
Portugal Portugal
I work on R&D for Frotcom International, a company that develops web-based fleet management solutions.

Comments and Discussions

 
GeneralMoving to VS 2005 Pin
AlexEvans22-Jan-07 16:31
AlexEvans22-Jan-07 16:31 
Generali am trying to debug the source under evc4, but.. Pin
Matii yan13-Dec-05 17:55
Matii yan13-Dec-05 17:55 
hi,
i am trying to debug the source under evc4, but got a lot of errors.
such as "error C2065: 'NUM_TOOL_TIPS' : undeclared identifier"

i don't know how to do.
i am a greenhand with evc, anyone would like give a guide.

Thanks a lot.

Matii Yan
GeneralRe: i am trying to debug the source under evc4, but.. Pin
João Paulo Figueira13-Dec-05 21:41
professionalJoão Paulo Figueira13-Dec-05 21:41 
GeneralRe: i am trying to debug the source under evc4, but.. Pin
Matii yan15-Dec-05 14:35
Matii yan15-Dec-05 14:35 
GeneralRe: i am trying to debug the source under evc4, but.. Pin
d-foss@online.no31-Jan-06 4:44
d-foss@online.no31-Jan-06 4:44 
GeneralRe: i am trying to debug the source under evc4, but.. Pin
João Paulo Figueira31-Jan-06 5:33
professionalJoão Paulo Figueira31-Jan-06 5:33 
GeneralRe: i am trying to debug the source under evc4, but.. Pin
d-foss@online.no31-Jan-06 5:52
d-foss@online.no31-Jan-06 5:52 
GeneralRe: i am trying to debug the source under evc4, but.. Pin
João Paulo Figueira31-Jan-06 5:58
professionalJoão Paulo Figueira31-Jan-06 5:58 
Generalbuild error Pin
zjzes4-Sep-05 2:39
zjzes4-Sep-05 2:39 
GeneralRe: build error Pin
João Paulo Figueira4-Sep-05 6:07
professionalJoão Paulo Figueira4-Sep-05 6:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.