Modifying Smartphone menu bars in runtime
By Alexander Shargin, June 03, 2002.
Introduction
Menu bars are important part of Smartphone user interface. Smartphone 2002 SDK
documentation describes how to create menu bars using SHCreateMenuBar function.
But sometimes you need to modify existing menu bar in runtime. The documentation
doesn't provide any details on how to do this but samples from Smartphone 2002
SDK give some hints. The main idea is to treat menu bar as a toolbar and use
TB_GETBUTTONINFO/TB_SETBUTTONINFO messages to manipulate it.
Note: there are other TB_* messages for toolbars but menu bar seems to
ignore them.
In this article I'll show some code examples illustrating how to change different aspects of menu bars. I'll assume that the
menu bar was created using the following resource template:
IDR_MAINMENU RCDATA
BEGIN
IDR_POPUP,
2,
I_IMAGENONE, IDM_ACTION, TBSTATE_ENABLED, TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE, IDS_ACTION, 0, NOMENU,
I_IMAGENONE, IDM_MENU, TBSTATE_ENABLED, TBSTYLE_DROPDOWN | TBSTYLE_AUTOSIZE, IDS_MENU, 0, 0,
END
The first item of this menu acts like button, the second one has popup menu
associated with it. Also I'll assume that the HWND of menu bar was stored in
hMenuBar variable.
Note: HWND of menu bar is extracted from SHMENUBARINFO::hwndMB member
variable after SHCreateMenuBar functions returns. If you don't want to store
this HWND permanently you can find it any time using SHFindMenuBar function.
Enabling/disabling menu bar items.
This code disables menu bar item IDM_ACTION:
TBBUTTONINFO tbbi;
tbbi.cbSize = sizeof(tbbi);
tbbi.dwMask = TBIF_STATE;
tbbi.fsState = TBSTATE_INDETERMINATE;
::SendMessage (hMenuBar, TB_SETBUTTONINFO, IDM_ACTION, (LPARAM)&tbbi);
To enable item again similar code is used. The only difference is in fsState
variable which is set to TBSTATE_ENABLED instead of TBSTATE_INDETERMINATE.
Changing text of menu bar items
The following code fragment is used to change label of IDM_MENU item to "Tools".
TBBUTTONINFO tbbi;
tbbi.cbSize = sizeof(tbbi);
tbbi.dwMask = TBIF_TEXT;
tbbi.pszText = _T("Tools");
::SendMessage (hMenuBar, TB_SETBUTTONINFO, IDM_MENU, (LPARAM)&tbbi);
You can also obtain item text from menu bar, e. g.:
TBBUTTONINFO tbbi;
TCHAR szBuf[255];
tbbi.cbSize = sizeof(tbbi);
tbbi.dwMask = TBIF_TEXT;
tbbi.pszText = szBuf;
tbbi.cchText = sizeof(szBuf)/sizeof(szBuf[0]);
::SendMessage (hMenuBar, TB_GETBUTTONINFO, IDM_MENU, (LPARAM)&tbbi);
Changing command id associated with menu bar item
SOmetimes you need to change command id associated with menu bar item "on the
fly". The following sample changes IDM_ACTION item's command id to 0x1234.
TBBUTTONINFO tbbi;
tbbi.cbSize = sizeof(tbbi);
tbbi.dwMask = TBIF_COMMAND;
tbbi.idCommand = 0x1234;
::SendMessage (hMenuBar, TB_SETBUTTONINFO, IDM_ACTION, (LPARAM)&tbbi);
Note that after command id is changed you must use new command id to
manipulate the item.
Manipulating popup menu associated with menu bar item
You may also want to change popup menu associated with menu bar item (add/remove
items, enable/disable them etc.). HMENU of this popup menu is obtained through
TBBUTTONINFO::lParam member variable:
TBBUTTONINFO tbbi;
tbbi.cbSize = sizeof(tbbi);
tbbi.dwMask = TBIF_LPARAM;
::SendMessage (hMenuBar, TB_GETBUTTONINFO, IDM_MENU, (LPARAM)&tbbi);
HMENU hPopupMenu = (HMENU)tbbi.lParam;
// Use hPopupMenu to change popup menu
Note that you can not change popup menu handle associated with menu bar item or
remove it completely by setting item style to TBSTYLE_BUTTON.
Conclusion
Smartphone menu bars can be easily updated "on the fly". The documentation is
somewhat obscure but with a little experimentation it's not hard to implement
required functionality.
Discuss
Discuss this article.
Here you can write your comments and read comments of other developers.
|