How to modify a popup menu item on MS Smartphone?
By Yaroslav Goncharov, January 30, 2003.
Question
I have noted that Windows CE does not support the ModifyMenu API function.
How can I modify a popup menu item on MS Smartphone?
Answer
MFC for Windows CE implements the ModifyMenu function using DeleteMenu
and InsertMenu API functions. The Smartphone platform does not support MFC,
but fortunately, it is possible to take this emulation from the MFC library for Windows CE
or from the Menu sample application.
Copy the following code and paste it somewhere to your project.
int wce_GetMenuItemCount(HMENU hMenu)
{
const int MAX_NUM_ITEMS = 256;
int iPos, iCount;
MENUITEMINFO mii;
memset((char *)&mii, 0, sizeof(MENUITEMINFO));
mii.cbSize = sizeof(MENUITEMINFO);
iCount = 0;
for (iPos = 0; iPos < MAX_NUM_ITEMS; iPos++)
{
if(!GetMenuItemInfo(hMenu, (UINT)iPos, TRUE, &mii))
break;
iCount++;
}
return iCount;
}
UINT wce_GetMenuItemID(HMENU hMenu, int nPos)
{
MENUITEMINFO mii;
memset((char *)&mii, 0, sizeof(mii));
mii.cbSize = sizeof(mii);
mii.fMask = MIIM_ID;
GetMenuItemInfo(hMenu, nPos, TRUE, &mii);
return mii.wID;
}
BOOL wce_ModifyMenu(HMENU hMenu, // handle of menu
UINT uPosition, // menu item to modify
UINT uFlags, // menu item flags
UINT uIDNewItem, // menu item identifier or handle of drop-down menu or submenu
LPCTSTR lpNewItem) // menu item content
{
// Handle MF_BYCOMMAND case
if ((uFlags & MF_BYPOSITION) != MF_BYPOSITION)
{
int nMax = wce_GetMenuItemCount(hMenu);
int nCount = 0;
while (uPosition != wce_GetMenuItemID(hMenu, nCount) && (nCount < nMax))
nCount++;
uPosition = nCount;
uFlags |= MF_BYPOSITION;
}
if (!DeleteMenu(hMenu, uPosition, uFlags))
return FALSE;
return InsertMenu(hMenu, uPosition, uFlags, uIDNewItem, lpNewItem);
}
The following sample code takes a menu from a menu bar and
changes id and text of the ID_PLAY menu item.
HMENU hPopupMenu = (HMENU) SendMessage(hMenuBar, SHCMBM_GETSUBMENU, 0, ID_MENU);
wce_ModifyMenu(hPopupMenu, ID_PLAY, MF_BYCOMMAND, ID_STOP, _T("Stop"));
Discuss
Discuss this QA.
Here you can write your comments and read comments of other developers.
|