How?

So I created my menu, and menu items like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 case WM_CREATE:
{

            HMENU Men1 = CreateMenu();
            HMENU WindowsTextMode = CreateMenu();
            HMENU Options1 = CreateMenu();

            AppendMenu(Men1,MF_POPUP, (UINT_PTR)Options1, "Game Type");
            AppendMenu(Options1, MF_STRING, NULL, "CommandPrompt(Text Based)");
            AppendMenu(Options1, MF_STRING, NULL, "Windows Mode(Semi-Graphics based)");
            AppendMenu(Options1, MF_STRING, NULL, "Windows Mode(Text Based)");
            AppendMenu(Options1, MF_STRING, NULL, "2D Mode");
            AppendMenu(Options1, MF_STRING, NULL, "3D Mode");

            SetMenu(hwnd, Men1);

            break;
}



So how would I command something to happen when someone clicks on Options1?
Anyone? :/
It's been a long time since I did menus in WinAPI. From what I remember, the menu will send a WM_COMMAND message with the assigned ID of the menu option as the low word of the WPARAM.

ref: http://msdn.microsoft.com/en-us/library/windows/desktop/ms647591%28v=vs.85%29.aspx

so...

1
2
3
4
5
6
7
8
9
10
// in your message handler:
case WM_COMMAND:
{
    switch( LOWORD(wParam) )
    {
        case ID_OF_YOUR_MENU_OPTION:
            // <- this code run when your menu option selected
            break;
    }
}break;


Although you seem to be assigning all of your menu items an ID of 'NULL' so they will all be treated the same. You're supposed to give them unique IDs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
enum MenuOptions
{
  ID_CMDPROMPT = 100,  // I just chose 100 at random.  I don't think it matters as long as it's unique
  ID_SEMIGRAPHICS,
  ID_TEXTBASED,
  ID_2DMODE,
  ID_3DMODE
};

// ...

AppendMenu(Options1, MF_STRING, ID_CMDPROMPT, "CommandPrompt(Text Based)");
  //                                ^
  //                                |
  //     Give it the ID here.  This is what is put in the wParam 



Also... you're using the wrong character type for AppendMenu. See this:

http://www.cplusplus.com/forum/windows/106683/


PS: more specific thread topics help for people like me who don't read every thread. A topic like "How?" doesn't tell me anything about the thread so I usually skip over it. If the thread topic looks like something I'd be able to help with, that's usually when I take the time to read the thread. So yeah... something like "How to get notified when user selects a menu item" would have been a better thread title.
Last edited on
Now it works, but after it dose the intended task it deletes the window.
Wait nvm works fine.
Topic archived. No new replies allowed.