Maximize MenuMinimize Menu

 

 

TOP

Trajecto's Win32 API Programming Tutorial for Pocket PC

Dialog Based Application

In this tutorial we will create a dialog based application using the Pocket PC application wizard. We will then use the resource editor to design a simple percentage change calculator which we will assign working C code to.

Once you've downloaded and installed Pelles C for Windows start a new project named percentChange:

Use Pocket PC application wizard

Select A dialog based program.

dialog_based_step_2

Four files are created by the wizard:

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

source_files_panel

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.c opens it.

Double-click DLG_MAIN

resource_editor

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

Add two pushbuttons, three text labels and three editboxes:

form_editor_start

The form with default value controls

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.

 

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

Make sure you do a File - Save all. In order, Compile, Build, and Execute percentChange.exe. The file will be copied directly to your device and execute.

Use the built-in keyboard and give Percentage Change a try!

To capture an image like this from your device use:
Tools - Capture Screen from Pocket PC

mobile_5

You can also use the new Standalone Device Emulator from Microsoft.
This one is running Mobile 5.

Radiosity Games
See our new game site

 

 

 

©Copyright TrajectoryLabs.com. All Rights Reserved.