Click here to Skip to main content
13,146,402 members (48,066 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 3 Sep 2005

Pocket PC Numeric Key Pad

, 3 Sep 2005
Rate this:
Please Sign up or sign in to vote.
An easier way to accept numeric input on the Pocket PC.

Sample Image - ppc_numpad.jpg


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.


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)

    ::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);
        m_pSubWnd->Click(CLICK_DOWN, point);

void CSubWndContainer::OnLButtonUp(UINT nFlags, CPoint point) 
        m_pSubWnd->Click(CLICK_UP, point);
void CSubWndContainer::OnLButtonDblClk(UINT nFlags, CPoint point) 
    m_pSubWnd = SubWndFromPoint(point);
        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);


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

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

                pParent->SendMessage(WM_COMMAND, m_nID, 

        // Outside this button

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).


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


About the Author

João Paulo Figueira
Software Developer (Senior) Frotcom International
Portugal Portugal
I work on R&D for Frotcom International, a company that develops web-based fleet management solutions.

You may also be interested in...

Comments and Discussions

GeneralMoving to VS 2005 Pin
AlexEvans22-Jan-07 16:31
memberAlexEvans22-Jan-07 16:31 
Generali am trying to debug the source under evc4, but.. Pin
Matii yan13-Dec-05 17:55
memberMatii yan13-Dec-05 17:55 
GeneralRe: i am trying to debug the source under evc4, but.. Pin
João Paulo Figueira13-Dec-05 21:41
memberJoã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
memberMatii 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
memberd-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
memberJoã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
memberd-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
memberJoão Paulo Figueira31-Jan-06 5:58 
Generalbuild error Pin
zjzes4-Sep-05 2:39
memberzjzes4-Sep-05 2:39 
GeneralRe: build error Pin
João Paulo Figueira4-Sep-05 6:07
memberJoã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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 3 Sep 2005
Article Copyright 2005 by João Paulo Figueira
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid