-
Menus
I am a bit confused about how you create these properly. I understand that you first call CreateMenu to create an empty menu bar. Next you use CreatePopupMenu to create an empty menu.
What I am confused about is how you specify the text for the menu item using InsertMenuItem? Also, I am just plain confused about how InsertMenuItem works. I have read the documentation, but just seem to be missing something about it. I would really prefer to not to use old functions such as AppendMenu and InsertMenu.
Can someone please point me in the right direction? I appologize for making my question so vague. Basically, I would like someone to please explain precisly how to use InsertMenuItem. Thank you very much for you time.
-
Here is how I did it in one of my programs:
Code:
void static CreateMenuSystem(HWND hwnd){
HMENU hMenu;
HMENU hSubMenu;
HMENU hSubSub;
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
AppendMenu(hSubMenu, MF_STRING, MENU_FILE_NEW, "&New\tCtrl+N");
AppendMenu(hSubMenu, MF_STRING, MENU_FILE_OPEN, "&Open...\tCtrl+O");
AppendMenu(hSubMenu, MF_SEPARATOR, 0, "");
AppendMenu(hSubMenu, MF_STRING, MENU_FILE_SAVE, "&Save\tCtrl+S");
AppendMenu(hSubMenu, MF_STRING, MENU_FILE_SAVEAS, "Save &as...");
AppendMenu(hSubMenu, MF_SEPARATOR, 0, "");
AppendMenu(hSubMenu, MF_STRING, MENU_FILE_PRINT, "&Print\tCtrl+P");
AppendMenu(hSubMenu, MF_SEPARATOR, 0, "");
AppendMenu(hSubMenu, MF_STRING, MENU_FILE_EXIT, "E&xit\tCtrl+Q");
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Edit");
AppendMenu(hSubMenu, MF_STRING, MENU_EDIT_UNDO, "&Undo\tCtrl+Z");
AppendMenu(hSubMenu, MF_SEPARATOR, 0, "");
AppendMenu(hSubMenu, MF_STRING, MENU_EDIT_CUT, "Cu&t\tCtrl+X");
AppendMenu(hSubMenu, MF_STRING, MENU_EDIT_COPY, "&Copy\tCtrl+C");
AppendMenu(hSubMenu, MF_STRING, MENU_EDIT_PASTE, "&Paste\tCtrl+V");
AppendMenu(hSubMenu, MF_STRING, MENU_EDIT_DEL, "&Delete\tDel");
AppendMenu(hSubMenu, MF_SEPARATOR, 0, "");
AppendMenu(hSubMenu, MF_STRING, MENU_EDIT_SEL, "Select &All\tCtrl+A");
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Search");
AppendMenu(hSubMenu, MF_STRING, MENU_SEARCH_FIND, "&Find\tAlt+F3");
AppendMenu(hSubMenu, MF_STRING, MENU_SEARCH_NEXT, "Find &Next\tF3");
AppendMenu(hSubMenu, MF_SEPARATOR, 0, "");
AppendMenu(hSubMenu, MF_STRING, MENU_SEARCH_REPLACE, "&Replace");
hSubSub = CreatePopupMenu();
AppendMenu(hSubMenu, MF_POPUP, (UINT)hSubSub, "&Goto");
AppendMenu(hSubSub, MF_STRING, MENU_SEARCH_TOP, "&Top\tCtrl+Home");
AppendMenu(hSubSub, MF_STRING, MENU_SEARCH_END, "&End\tCtrl+End");
AppendMenu(hSubSub, MF_STRING, MENU_SEARCH_LINE, "&Line\tCtrl+G");
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Format");
AppendMenu(hSubMenu, MF_STRING, MENU_FMAT_WRAP, "&Word Wrap");
AppendMenu(hSubMenu, MF_STRING, MENU_FMAT_FONT, "&Font");
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Insert");
AppendMenu(hSubMenu, MF_STRING, MENU_INSERT_TD, "&Time/Date");
AppendMenu(hSubMenu, MF_STRING, MENU_INSERT_DT, "&Date/Time");
AppendMenu(hSubMenu, MF_SEPARATOR, 0, "");
hSubSub = CreatePopupMenu();
AppendMenu(hSubMenu, MF_POPUP, (UINT)hSubSub, "&Html Scripts");
AppendMenu(hSubSub, MF_STRING, MENU_HTML_FRAME, "&Frame Work");
AppendMenu(hSubSub, MF_STRING, MENU_HTML_META, "&Meta Tags");
AppendMenu(hSubSub, MF_STRING, MENU_HTML_LINK, "&Link Css");
AppendMenu(hSubSub, MF_STRING, MENU_HTML_QUICK, "&Quick Link");
AppendMenu(hSubSub, MF_SEPARATOR, 0, "");
AppendMenu(hSubSub, MF_STRING, MENU_HTML_MODIFIED, "Last M&odified");
AppendMenu(hSubSub, MF_STRING, MENU_HTML_BROWSER, "&Browser in Use");
AppendMenu(hSubSub, MF_STRING, MENU_HTML_CURRENT, "&Current URL");
AppendMenu(hSubSub, MF_STRING, MENU_HTML_HIGHLIGHT, "&No Highlight");
AppendMenu(hSubSub, MF_SEPARATOR, 0, "");
AppendMenu(hSubSub, MF_STRING, MENU_HTML_FONT, "&Text Font");
AppendMenu(hSubSub, MF_STRING, MENU_HTML_IMAGE, "&Image");
AppendMenu(hSubSub, MF_STRING, MENU_HTML_ORDERED, "Numb&ered List");
AppendMenu(hSubSub, MF_STRING, MENU_HTML_UNORDERED, "B&ulleted List");
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Extras");
AppendMenu(hSubMenu, MF_STRING, MENU_EXTRA_UPPER, "&Uppercase");
AppendMenu(hSubMenu, MF_STRING, MENU_EXTRA_LOWER, "&Lowercase");
AppendMenu(hSubMenu, MF_SEPARATOR, 0, "");
AppendMenu(hSubMenu, MF_STRING, MENU_EXTRA_LINE, "&Line Count");
AppendMenu(hSubMenu, MF_STRING, MENU_EXTRA_SIZE, "&Estimate Size");
AppendMenu(hSubMenu, MF_STRING, MENU_EXTRA_INSTANCE, "&New Instance");
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");
AppendMenu(hSubMenu, MF_STRING, MENU_HELP_TOPIC, "&Help Topics\tF1");
AppendMenu(hSubMenu, MF_STRING, MENU_HELP_SITE, "&Web Site");
AppendMenu(hSubMenu, MF_SEPARATOR, 0, "");
AppendMenu(hSubMenu, MF_STRING, MENU_HELP_ABOUT, "&About");
AppendMenu(hSubMenu, MF_STRING, MENU_HELP_VERSION, "&Version");
SetMenu(hwnd, hMenu);
}
Hope that helps.
- Sean
-
First fill in the menu item info struct
Code:
ZeroMemory( &MenuItemInfo, sizeof(MENUITEMINFO)); //make sure all elements are empty
MenuItemInfo.cbSize = sizeof(MENUITEMINFO);
MenuItemInfo.wID = ID_MENU_ITEM; //item ID# for callback
MenuItemInfo.fMask = MIIM_DATA |MIIM_ID |MIIM_TYPE |MIIM_STATE; // elements we have filled in
MenuItemInfo.fType = MFT_STRING; //is a text string
MenuItemInfo. dwTypeData = szMenuItemName; //the actual text string
MenuItemInfo.cch = 64; // length of szMenuItemName
MenuItemInfo.fState = MFS_ENABLED; //menu item is useable (not greyed out)
MenuItemInfo.hSubMenu = NULL; //has no pop-up menu under it
Now you must call InsertMenuItem() or SetMenuItem()
InsertMenuItem( ) needs to be told if we are adding after an item or in a set numerical position (ie first item in menu)
//as first item in menu
InsertMenuItem(hMenu, 0 , TRUE, &MenuItemInfo);
//as tenth item in menu
InsertMenuItem(hMenu, 10 , TRUE, &MenuItemInfo);
//as last item in menu
iMenuItems = GetMenuItemCount( hMenu );
InsertMenuItem(hMenu, iMenuItems , TRUE, &MenuItemInfo);
//after an ID# ALREADY in the menu
InsertMenuItem(hMenu, ID_LAST_MENU_ITEM , FALSE, &MenuItemInfo);
-
Thank you both so much for your help! :)