Is there a way to add a menu to window that was
created without specifying a menu handle in the
call to CreateWindowEX ?
Posted on 2003-03-28 16:41:58 by ASMMike
Copied from API reference:

The CreateMenu function creates a menu. The menu is initially empty, but it can be filled with menu items by using the InsertMenuItem, AppendMenu, and InsertMenu functions.

HMENU CreateMenu(VOID)


This function has no parameters.

Return Values

If the function succeeds, the return value is the handle to the newly created menu.
If the function fails, the return value is NULL.


Resources associated with a menu that is assigned to a window are freed automatically. If the menu is not assigned to a window, an application must free system resources associated with the menu before closing. An application frees menu resources by calling the DestroyMenu function.
Windows 95: The system can support a maximum of 16,364 menu handles.

See Also

AppendMenu, CreatePopupMenu, DestroyMenu, InsertMenu, SetMenu, InsertMenuItem
Posted on 2003-03-28 16:49:34 by Odyssey
Hi ASMMike,

If the menu is in your resource section you would load the menu using the LoadMenu API function then assign the menu to your window using the SetMenu API function :

Assuming that your menu resource ID is 10000

invoke LoadMenu, hInstance, 10000
mov hMenu, eax
invoke SetMenu, hWnd, hMenu

If you used a named resource change the 10000 to a pointer to a zero terminated string name.
Posted on 2003-03-28 19:09:00 by donkey

invoke LoadMenu,hInst,600 ; menu ID
mov hMnu, eax
invoke SetMenu,hWnd,eax

This is the normal way to load a menu after the CreateWindowEx function has returned.

hInst is the instance handle
hWnd is the window handle returned by CreateWindowEx.
600 in this example is the menu resource ID.

Posted on 2003-03-28 20:01:09 by hutch--
First, I'm doing this from code, there's no resource for the menu.
Second, using the CreateMenu call:

1. I call CreateMenu.
2. AppendMenu the return val. of CreateMenu, ID is 55, "File"
3. AppendMenu append to menu 55, ID 66, "Exit"
4. call SetMenu to set the created menu to the window.

When the program runs, the "Exit" option isn't visible.

What am I doing wrong? I have a feeling that the ID isn't the handle to be
appending a menu to, but I'm not seeing anything else to do.

Thanks for your help so far,
- Mike
Posted on 2003-03-28 21:59:04 by ASMMike

I may have not connected what you are trying to do. Are you trying to dynamically build menus without using resources. If so its reasonably straight forward so let us know what you are after and I am sure you will get an answer if it can be done.

Posted on 2003-03-29 03:21:48 by hutch--
Yes, that's exactly what I'm trying to do. If you could perhaps show me the code
or explain how to add a "File" menu and an "Exit" option, I could take it
from there.

- Mike
Posted on 2003-03-29 10:34:48 by ASMMike
Hi ASMMike,

It goes something like this :
; Create a menu for the window

invoke CreateMenu
mov hMenu,eax
invoke SetMenu, hWnd, hMenu

; Make a file menu
invoke InsertMenu, hMenu, -1, MF_BYPOSITION or MF_POPUP, hMenu, ADDR szFileMenuText
; Make an edit menu
invoke InsertMenu, hMenu, -1, MF_BYPOSITION or MF_POPUP, hMenu, ADDR szEditMenuText

invoke GetSubMenu, hMenu, 0
mov hFileMenu, eax
invoke GetSubMenu, hMenu, 1
mov hEditMenu, eax

invoke AppendMenu, hFileMenu, MF_STRING, ItemID, ADDR szFileItem1Text
; repeat for each item in the file menu

invoke AppendMenu, hEditMenu, MF_STRING, ItemID, ADDR szEditItem1Text
; repeat for each item in the Edit menu

invoke DrawMenuBar, hWnd
Posted on 2003-03-29 11:19:42 by donkey
Is it possible to do that by only handles and not using
any positions (like the ones using in GetSubMenu and InsertMenu)?
Posted on 2003-03-29 11:52:01 by ASMMike
GetSubMenu and InsertMenu require position parameters, there is no way around it. Windows must know what the position is because the handle is not yet available/known when the commands are executed. After all InsertMenu is the function that creates the handle and GetSubMenu is only executed because you don't know the handle.
Posted on 2003-03-29 12:01:21 by donkey
:grin: , thanks everyone! my program works great now!

Thanks again,
- Mike
Posted on 2003-03-29 12:54:04 by ASMMike
Um, just create the File menu before you create the main menu, then you'll know the handle. What you're doing right now is to insert the main menu into itself, which probably isn't going to do any good.
Posted on 2003-03-29 15:20:02 by Sephiroth3