LittlePaul.com Developer Repository


Home Using a logfile IO Port Programming Registry Programming Email



How to use InsertMenuItem()

This explains how to get up and runing with the Windows API function InsertMenuItem() in VC++. It will explain how to add a menu item to a menu in your vc++ application.
It may not be exactly the right way to do it, but it does add a menu item.
Once you have the function working, it's usually easier to change bits for the correct fail-safe calls.

For the first example, we are going to create a smacer, because it's simpler.


First declare the MENUITEM structure, which stores info about the menuitem to add.
MENUITEMINFO ItemInfo

Declare some other stuff too:
int count, ID;

Set these values:
count = IDM_EXIT;
ID = WM_USER+50;

Setting the count to IDM_EXIT means the spacer/menu option will get created after the File/Exit menu option.

Prepare the MENUITEM structure:
ItemInfo.cbSize = sizeof(MENUITEMINFO);
ItemInfo.fMask = MIIM_ID|MIIM_TYPE;
ItemInfo.wID = ID;
ItemInfo.fType = MFT_SEPARATOR;
ItemInfo.fState = MFS_ENABLED;
The wID value sets the ID of the menu item, something internal in your program to refer to the button as.
The fMask value holds flags to enable each part of the structure. Without the MIIM_TYPE flag it won't even look at the fType value.
The fType value determines what the menu item is: this example shows it as a seperator, the next example will set it as MFT_STRING for a string. Can also be a bitmap etc

Make the function call:
InsertMenuItem(hMenuMain, count, false, &ItemInfo);
It refers to the ItemInfo structure we created, to count for where to put the button, and sets the type to false, which as far as I understand something to tell it what count means; how to place the menu item.
hMenuMain is the handle to the menu in the program. Will be declared by default at the top of the code, as it should be.



Lets add a menu item with text instead of the Seperator.


Define and initialise a string:
char File1Text[MAX_LOADSTRING];
wsprintf(MyMenuButton1, "&Added Menu Button");


Prepare the ITEMINFO structure like this:
ItemInfo1.cbSize = sizeof(MENUITEMINFO);
ItemInfo1.fMask = MIIM_STATE|MIIM_ID|MIIM_TYPE;
ItemInfo1.wID = ID;
ItemInfo1.fType = MFT_STRING;
ItemInfo1.fState = MFS_ENABLED;
ItemInfo1.dwTypeData = MyMenuButton1;
ItemInfo1.cch = strlen(MyMenuButton1);

Other than the fType, we have added a flag to fMask to tell the structure to look in the dwTypeData member for the text to use.
I replaced the whole Separator code with this new one, but to have both you just need to increment the ID variable used in the wID member.

Run the InsertMenuItem call as before:
InsertMenuItem( hMenuMain, count, false, &ItemInfo );



The next thing to do is handle the button click message.
Dead simple, find the procedure with the WM_ messages (WM_COMMAND, WM_CLOSE etc) for the window the menu is in, and add one for each button.
Instead of the WM_ name simply use the WM_USER+50 value we set count to. That's the button's ID, or the wID member of the menu item's ITEMINFO structure.



Not working?
Add#include "Winuser.h" and #include "Windows.h" to the top of the file
Still not working?
Add to the project settings the library User32.lib.



Basically that's how to get up and running.

You can play with the values for wID and the value of the variable count I used above, yourselves. If you have a guide of how these should be set, please let me know!









If you found this useful, or have suggestions please email me.