AncaA's tech journal

17 Jan, 2008

Sample Win32 C++ Windows Mobile 5.0 application

Posted by: Anca Alimanescu In: Mobile Dev

ou will be presented a basic Windows Mobile application written in native code.

The IDE used to develop it has been VSTO 2005 Proffesional Edition and we have also used the Windows Mobile 5.0 Pocket PC SDK. In order to see how to setup a project and create a “Hello World” application you can check Microsoft’s tutorial.

In order to be able to add a custom menu to the application, open the resource panel and from the Menu folder start customizing the .dlg file. In my application I added a File→Exit menu and an Options→Option1,Options→Option2 menus. They will be defined in the resourceppc.h header file.

#define ID_FILE_EXIT 32771
#define ID_OPTIONS_OPTION1 32772
#define ID_OPTIONS_OPTION2 32773

In the WndProc produre, we will handle the messages in the main window and for the WM_Create message we will build the menu bar.

case WM_CREATE:

SHMENUBARINFO mbi;
memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;
mbi.dwFlags = SHCMBF_HMENU;

if (SHCreateMenuBar(&mbi))
{
HMENU hMenu = (HMENU)::SendMessage(mbi.hwndMB, SHCMBM_GETMENU, 0, 0);
MENUITEMINFO mii;
memset((char *)&mii, 0, sizeof(mii));
mii.cbSize = sizeof(mii);
LRESULT lResult = 0;

UINT i = 0;

while(true)
{
if(0 == ::GetMenuItemInfo(hMenu, i, TRUE, &mii))
{
break;
}
mii.fType |= MFT_OWNERDRAW;
lResult = ::SetMenuItemInfo(hMenu, i, TRUE, &mii);
++i;
}
g_hWndMenuBar = mbi.hwndMB;

}
else
{

g_hWndMenuBar = NULL;

}
// Initialise la structure d'informations sur l'activation du shell
memset(&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);

break;

Each of the submenus will be handeled when the WM_COMMAND message is received:

case WM_COMMAND:

wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Analyse les sélections de menu :
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
case ID_FILE_EXIT:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
case ID_OPTIONS_OPTION1:
DialogBox (g_hInst, // Handle to the application instance.
(LPCTSTR)IDD_DIALOG1, // Identifies the dialog box template.
hWnd, // Handle to the owner window.
ReplaceDialogProc); // Pointer to the dialog box procedure.
break;
case ID_OPTIONS_OPTION2:
DialogBox (g_hInst, // Handle to the application instance.
(LPCTSTR)IDD_DIALOG2, // Identifies the dialog box template.
hWnd, // Handle to the owner window.
ReplaceDialogProc); // Pointer to the dialog box procedure.
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;

For each of the options chosen a custom dialog box will be displayed:

DialogBox (g_hInst, // Handle to the application instance.
(LPCTSTR)IDD_DIALOG2, // Identifies the dialog box template.
hWnd, // Handle to the owner window.
ReplaceDialogProc); // Pointer to the dialog box procedure.

It is important to know that for desingning dialog boxes you have to make sure to design one for the wide screen and one for the tight screen and to make sure to choose the right one according to the screen resolution. The example below shows how to choose the right About dialog box according to the phone’s settings.

#ifdef _DEVICE_RESOLUTION_AWARE

case WM_SIZE:
{
DRA::RelayoutDialog(
g_hInst,
hDlg,
DRA::GetDisplayMode() != DRA::Portrait ? MAKEINTRESOURCE(IDD_ABOUTBOX_WIDE) : MAKEINTRESOURCE(IDD_ABOUTBOX));
}
break;

#endif

In order to handle the mouse events and the key events, the application must handle the mouse button and the key pressed messages:

case WM_LBUTTONDOWN:

{

// Retrieve the handle to the display device context.
hdc = GetDC (hWnd);

// start drawing the text that will be displayed
int bReturn;
TCHAR szHelloStr[50];

StringCchCopy(szHelloStr, 50, L"Mouse clicked!");

// Set text color.
SetTextColor (hdc, RGB(0,0,0));

bReturn = ExtTextOut (hdc, 10 , 140, 0, NULL,
szHelloStr, lstrlen(szHelloStr), NULL);

// Release the device context.

ReleaseDC(hWnd, hdc);

}
break;
case WM_CHAR:
{

//if the space key was pressed
if (wParam == VK_SPACE)
{
MessageBox(hWnd,L"You pressed Space.", L"Space key", MB_OK | MB_ICONINFORMATION);
}
else
{
MessageBox(hWnd,L"You have not pressed Space key.", L"Space key", MB_OK | MB_ICONINFORMATION);
}
}
break;

In our sample application when the left mouse button is clicked, a message will be displayed on the screen. If the Space key is pressed, a message box will appear confirming the user that he has pressed the Space key, or if not, other key.

  • Share/Bookmark

3 Responses to "Sample Win32 C++ Windows Mobile 5.0 application"

1 | Sherif Tawfik

February 6th, 2009 at %I:%M %p

Avatar

Hello Anca,

Thanks for the nice piece of code. However, I have tried it inside my MFC WM6 application, and I am getting this error:

Data Abort: Thread=97c2571c Proc=80096c70 ‘SMSTimer.exe’
AKY=00000801 PC=0002b9e0(SMSTimer.exe+0×0001b9e0) RA=0002b9d4(SMSTimer.exe+0×0001b9d4) BVA=18152dac FSR=0000040f

I look forward to your kind response..

Sherif

2 | Sherif Tawfik

February 6th, 2009 at %I:%M %p

Avatar

Sorry, the code I used is the following:

//////////////////////////////////
// Retrieve the handle to the display device context.
hdc = GetDC (hWnd);

// start drawing the text that will be displayed
int bReturn;
TCHAR szHelloStr[50];

StringCchCopy(szHelloStr, 50, L”Mouse clicked!”);

// Set text color.
SetTextColor (hdc, RGB(0,0,0));

bReturn = ExtTextOut (hdc, 10 , 140, 0, NULL,
szHelloStr, lstrlen(szHelloStr), NULL);

// Release the device context.

ReleaseDC(hWnd, hdc);
////////////////////////////////

By the way, I used :: before each function, to use the CWnd functions.

3 | Sherif Tawfik

February 6th, 2009 at %I:%M %p

Avatar

Hello Anca..

Sorry for bothering you, just solved the problem.

Bye.

Comment Form

Arhive

Mobile Barcode

qrcode

This is a 2D-barcode containing the address of my mobile site.If your mobile has a barcode reader, simply snap this bar code with the camera and launch the site.

About me

Client-focused software engineer with high intellectual mobility and experience in international teams.

Some of my interests are open innovation, design patterns, networking, personal branding, blogging and study of foreign languages.

Enjoy your visit and don’t hesitate to leave me a feedback!


Software entropy

1. A computer program that is used will be modified.

2. When a program is modified, its complexity will increase, provided that one does not actively work against this.

Switch to our mobile site