there are two ways to go about doing this
1) use a resource file
first start a new header file for you project called "resource.h" or something
and put this code in:
Code:
#include <windows.h>
#define IDM_MENU 101
#define ID_FILE_EXIT 102
#define ID_EDIT_COPY 103
#define ID_HELP_ABOUT 104
IDM_MENU is the identifier of our menu
everything else will be the unique IDs our widnow will use to identify the buttons in our menu
next put a resource file in your project and pop this code in:
Code:
#include "resource.h"
IDM_MENU MENU
BEGIN
POPUP "&File"
{
MENUITEM "E&xit", ID_FILE_EXIT
}
POPUP "&Edit"
{
MENUITEM "Copy",ID_EDIT_COPY
POPUP "&Help"
{
MENUITEM "&About", ID_HELP_ABOUT
}
END
you use that syntax in a resource file to actually make your menu, so remember it or save it, hopefully it is pretty straight forward though
the MENU declares the whole menu bar on the top of the window
the POPUP is a button on the menu
MENUITEMs are the buttons on the drop down menus that appear when you click on one of the menu options(POPUPs)
now we have to attach our menu to the window, so include resource.h in your .cpp file and change
Code:
wcl.lpszMenuName = NULL; /* no menu */
to
Code:
wcl.lpszMenuName = MAKEINTRESOURCE(IDM_MENU); /* there is now a menu */
compile and run that ........ homes
2) use HMENU
first make a resource.h header file like we did in the last one but this time you don't need to define the IDM_MENU ID
this way doesn't use a resource file, instead storing our menu in a variable of type HMENU
put this code at the top of your source file:
Code:
HMENU hMenu, hSubMenu;
hMenu will be used to store the menu, and hSubMenu will be used to create the individual options
anyway, go to your windows process and add WM_CREATE to the message switch
under the WM_CREATE put the following code:
Code:
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_EDIT_COPY, "&Copy");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Edit");
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_HELP_ABOUT, "&About");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");
again pretty straightforward, but to explain it let's dissect this
Code:
hMenu = CreateMenu();
creates the menu
Code:
hSubMenu = CreatePopupMenu();
create hSubMenu as a poup menu and clear out anything that may have been in it
Code:
AppendMenu(hSubMenu, MF_STRING, ID_EDIT_COPY, "&Exit");
Add an "exit" button to hSubMenu
Code:
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
Add hSubMenu to hMenu under the label "File"
and then we are just repeating it for each popup menu
Lastly, to attach the menu to our window
Code:
SetMenu(hwnd, hMenu);
if it doesn't work tell me and i will fix what i did wrong but i think i remembered everything right