Published by
May 29, 2012 (last update: May 29, 2012)


Score: 4.0/5 (119 votes)
Some of you probably have a program you are working on, or even just have an idea for one. So, I will teach you how to make a professional looking program by adding in a pop down menu, and even teach you how to make it do basic stuff if you click the drop down menu's item

First off, you find the section where you will be putting your code to make your menu.

You will want to put just under your switch (message) part of your code.

So, you will need to tell it what case it is in. in this instance, it will be in the case WM_CREATE. so, write it out like this:

Next, you will declare all the menu's contained in the one menu at the top of your screen. For this, we will use the CreateMenu function for creating the menu. Your code should look like this:

HMENU hMenubar = CreateMenu();
HMENU hFile = CreateMenu();
HMENU hEdit = CreateMenu();
HMENU hHelp = CreateMenu();

Now, the hMenubar will end up being the strip along the top of our program that will hold the other drop down menus. The rest of the menus declared will be the titles of the drop down menus.

Now we will use AppendMenu to place the menus

AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hFile, "File");           
AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hEdit, "Edit");
AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hHelp, "Help"); 

You don't have to AppendMenu the hMenubar, but you do have to say that the pop down menu titles are going to be on it. You can actually name the hMenubar anything you want, but you need to say the same name that you named it all through your code. You can also name your pop down menu titles anything you want also. But the same applies to those as it did the hMenubar.

Continuing on:
Making items for the pop down menus

AppendMenu(hFile, MF_STRING, ID_Save, "Save");
AppendMenu(hFile, MF_STRING, ID_Load, "Load");
AppendMenu(hFile, MF_STRING, ID_Exit, "Exit");
AppendMenu(hEdit, MF_STRING, ID_Undo, "Undo");
AppendMenu(hEdit, MF_STRING, ID_Redo, "Redo");
AppendMenu(hHelp, MF_STRING, ID_VWS, "Visit website"); 

Now, that was just saying what is going to be in the menu.
Save will be in hFile because it says hFile at the beginning of the commass.

Next, you might notice the ID_Save, ID_Load, and all of those. We can not compile it like this at the moment, you would have to change all the ID's to just NULL like so: ]MF_STRING, NULL, "Save");
But if you have NULL then you have no way of giving it a command when it is clicked.

So, go back in your code to right after:

return messages.wParam;

and then do this:

#define ID_Save 1
#define ID_Load 2
#define ID_Exit 3
#define ID_Undo 4
#define ID_Redo 5
#define ID_VWS 6  

Now you have only one thing left to do before you can compile it, but it won't do anything if you choose an item from the drop down menu. (Note: you must number your #define's, so do it 1, 2, 3, for each one.)

Now head back down to under the last AppendMenu you did, and type this in:

SetMenu(hwnd, hMenubar);

Now you can compile it and see your pretty drop down menu. Drool over it and come back when you're done drooling.

Ok, next we program in the commands, so we will declare case WM_COMMAND:{
Under thta you will start making commands,

Lets just do some basic commands just to keep this short, it is getting too long already.

if(LOWORD(wParam) == ID_Exit) {
exit(0); } 

That is telling it to exit when you click the button with the ID_Exit on it.

if(LOWORD(wParam) == ID_VWS) {
ShellExecute(NULL, "Open", "", NULL, NULL, SW_SHOWNORMAL);} 

Now, this is saying: if you click the button with ID_VWS, then link to where ever it says to link to with your default browser.

You can also do ShellExecute on files, but it only brings up the file with the default thing it opens with (for example: .txt would open in notepad, or what ever you set it to open with)

You can just change the web link to:

and it will open that file when you push the button

I have attached the main.cpp so you ca mess around in the code.

Attachments: [main.cpp]