Maximize MenuMinimize Menu

 

 

TOP

Trajecto's Win32 API Programming Tutorial for Pocket PC

Smartphone Dialog Based Application

This tutorial demonstrates creating a dialog based application using the Smartphone application wizard. The resource editor is used to design a simple percentage change calculator which will have working C code assigned to it.

 

START
Once you've downloaded and installed Pelles C for Windows (version 4.5 beta 2 or greater) start a new project named percentage:

smartphone application wizard

Use Smartphone application wizard

smartphone wizard step 1

Select A dialog based program.

smartphone wizard step 2

Four files are created by the wizard:

main.c The main source code - Smartphone Windows skeleton
main.h Contains definitions of symbolic names for the resource file
main.ico The icon file
main.rc The resource file

source files

The source files panel shows the various files attached to the project. Double-clicking any of the files will open them in their respective editor.

Double-click main.rc

main.rc

Double-clicking main.rc opens it.

Double-click DLG_MAIN

resource editor

Double-clicking DLG_MAIN opens the resource editor. The Form designer, and Controls palette are available by default.

Delete the two radiobuttons, add two pushbuttons, three text labels and three editboxes:

resource editor

The form with default value controls above

 


resource values

Using the Properties panel, edit the Name values of the editboxes and pushbuttons to match the values above. Edit the Text values also.
It is very IMPORTANT the names match for our code sample to work.

 

resource symbols

In the Resource Editor, Select View -> Resource symbols. Note the values of the editboxes and pushbuttons you edited before.

 

main.h

Double-click on main.h to open it in the editor. Add the values as shown above for the editboxes and pushbuttons.

 

     case IDCALC:
         {
         WCHAR wszBuffer[40], *pwszEnd;
         double InitialValue;
         double CurrentValue;
         double Value;
         /*
         * Get the "Initial" value as a string (from the dialog control).
         */
         GetDlgItemText(hwndDlg, IDC_INITVALUE, wszBuffer, NELEMS(wszBuffer));
         /*
         * Convert the "Initial" value to a double-precision floating-point value
         * (pwszEnd will point right after the converted characters).
         */
         InitialValue = wcstod(wszBuffer, &pwszEnd);
         /*
         * Check the remaining part of the string (we keep it simple, and just
         * check for the string terminator).
         */
         if (*pwszEnd != L'\0')
         {
         /* The value seems wrong. Display an error message, set the input focus
         to the problematic field, and leave */
         MessageBox(hwndDlg,
         L"Sorry, can't understand this value. Try again.",
         L"Input error",
         MB_OK|MB_SETFOREGROUND);
         SetDlgItemFocus(hwndDlg, IDC_INITVALUE);
         break;
         }
         /*
         * Get the "Current" value as a string (from the dialog control).
         */
         GetDlgItemText(hwndDlg, IDC_CURRVALUE, wszBuffer, NELEMS(wszBuffer));
         /*
         * Convert the "Current" value to a double-precision floating-point value
         * (pwszEnd will point right after the converted characters).
         */
         CurrentValue = wcstod(wszBuffer, &pwszEnd);
         /*
         * Check the remaining part of the string (we keep it simple, and just
         * check for the string terminator).
         */
         if (*pwszEnd != L'\0')
         {
         /* The value seems wrong. Display an error message, set the input focus
         to the problematic field, and leave */
         MessageBox(hwndDlg,
         L"Sorry, can't understand this value. Try again.",
         L"Input error",
         MB_OK|MB_SETFOREGROUND);
         SetDlgItemFocus(hwndDlg, IDC_CURRVALUE);
         break;
         }
         /*
         * Amazing. We seem to have two correct values. Do the calculation.
         */
         Value = ((CurrentValue - InitialValue) / InitialValue) * 100.0;
         /*
         * Convert the result to a string. We use %G, but other formats are possible.
         * (The format must be accepted by the *Microsoft* function swprintf,
         * since we are using the Microsoft C runtime on Pocket PC.
         */
         swprintf(wszBuffer, L"%G", Value);
         /*
         * Put the string into the "Percentage Change" dialog field.
         */
         SetDlgItemText(hwndDlg, IDC_CHANGE, wszBuffer);
         break;
         }
     case IDCLEAR:
         {
SetDlgItemText(hwndDlg, IDC_INITVALUE, L""); SetDlgItemText(hwndDlg, IDC_CURRVALUE, L""); SetDlgItemText(hwndDlg, IDC_CHANGE, L"");
         break;
         }
 

Now it is time to add our code. Paste the above code right after the 'return TRUE;' line in the 'case IDOK' statement in main.c. Pelle himself has provided comments in the code to help us understand how it works.

/* Useful macros */
       #define NELEMS(a)  (sizeof(a) / sizeof((a)[0]))
       #define SetDlgItemFocus(hwnd,idCtl)  FORWARD_WM_NEXTDLGCTL((hwnd), GetDlgItem((hwnd),(idCtl)), TRUE, PostMessage)

Paste the above code right after the #include statements.

compile smartphone 

Save and compile your work, select File -> Save all. Select Project -> Compile, then Project -> Build. The executable percent.exe will be created.

smartphone emulator

To run your application, you can use the new Standalone Device Emulator from Microsoft.

smartphone emulator

From within the emulator, select File -> Configure. Here you set up a shared directory with your PC. This is where you will place a copy of your executable. Use the smartphone emulator file explorer to explore the storage card. Your executable resides there to be launched.

 

Radiosity Games
See our new game site

 

 

©Copyright TrajectoryLabs.com. All Rights Reserved.