PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Creating Menus On The Fly?

I've been looking for a tutorial or source code example for creating a menu dynamically for the Pocket PC, but have been coming up dry. I've only been able to find a couple of code snippets but no complete examples.

Something along the lines of the 2nd example for creating menus dynamically for Win32 from this site:

http://www.winprog.org/tutorial/menus.html

I've been going through the tutorials on this site and converting them to code that would work on the Pocket PC, but this one has me stumped.

Is there anything out there? :(

Thanks.
This exact topic was discussed a month or two ago in this forum.

Dan East
I can help you if you want. I was the one that was having problems doing it.

Regards,
Kornalius
Thanks Kornalius,

I looked at the link that Dan East mentioned and I'm still lost. :(

I'm looking for something along the lines of the 2nd example for creating menus dynamically for Win32 from this site, but that can be compiled for the Pocket PC:

http://www.winprog.org/tutorial/menus.html
Sorry, I completely forgot about this thread, :( I have been so busy with my PPL project lately.

Here is how I do it.

1. First I create a menubar resource with one menu item that is not visible.

2. When creating my form, I do this:

Code:
HWND CreateEmptyRpCommandBar(HWND hwnd)
{
SHMENUBARINFO mbi;

memset(&mbi;, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.dwFlags = 0;
mbi.hwndParent = hwnd;
mbi.nToolBarId = IDR_EMPTYMENUBAR1;
mbi.hInstRes = Instance;
mbi.nBmpId = 0;
mbi.cBmpImages = 0;
#ifdef SHCMBF_COLORBK
mbi.clrBk = 0 ; // PocketPC2002 only
#endif

if (!SHCreateMenuBar(&mbi;))
return NULL;

return mbi.hwndMB;
}

...

HWND m = CreateEmptyRpCommandBar(h);
if (m != NULL)
CommandBar_Show(m, true);


3. Then I create my menu:

Code:
void AddMenuItem(HWND bar, int iFlags, UINT iId, UINT iValue, LPTSTR sText)
{

int iLength ; // Real length of the menu item string
TBBUTTON rButton ; // Menu item "button" on the menu bar
wchar_t *lpBuffer ; // Pointer to buffer for text of menu items
int iIndex ;

iLength = wcslen(sText) ;

lpBuffer = (wchar_t *)malloc((iLength + 2) * sizeof(wchar_t)) ;
wcscpy(lpBuffer, sText) ; // Copy the string to the buffer
lpBuffer[iLength+1] = 0;

if ((iIndex = SendMessage(bar, TB_ADDSTRING, 0, (LPARAM) lpBuffer)) == -1) // Add one string
{
char ss[255];
sprintf(ss, "Could not add menu string '%s'", ToChar(sText));
RuntimeError(ss) ;
}

free(lpBuffer) ;

// Now add a toolbar button containing the string

rButton.iBitmap = I_IMAGENONE ; // No bitmap
rButton.idCommand = iId ; // ID for WM_COMMAND
rButton.fsState = TBSTATE_ENABLED ; // Enabled button, text
rButton.fsStyle = iFlags | TBSTYLE_AUTOSIZE ; // Autosize the button, flags indicate dropdown
rButton.dwData = iValue ; // Handle to dropdown menu
rButton.iString = iIndex ; // Use this string for text

CommandBar_AddButtons(bar, 1, &rButton;);
CommandBar_DrawMenuBar(bar, 0);
}

...

HMENU newmenu = CreatePopupMenu();

#define TBSTYLE_NO_DROPDOWN_ARROW 0x0080
AddMenuItem(m, TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW, Id, (UINT)newmenu, mname);



If you have any questions, I will try to help.

Regards,
Kornalius