window menu

This is a discussion on window menu within the Windows Programming forums, part of the Platform Specific Boards category; I just figured out how to write a simple windows application(A simple window)now I want to add a menu to ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Unhappy window menu

    I just figured out how to write a simple windows application(A simple window)now I want to add a menu to it,with a file button...etc,How do I do that?Im writing it in win32 code.

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    A menu is a resource, so you need a resource file call it menu.rc. Now you write your menu in it
    Code:
    #include "menu.h"
    
    MyMenu MENU
    {
       POPUP "File"
       {
          MENUITEM "Exit", IDM_EXIT
       }
    }
    menu.h contains the menu id values, so all our menu.h has in it is
    Code:
    #define IDM_EXIT 100
    Now you need to add #include "menu.h" in your file which contains your window function as this needs to know the id values.
    In your window class definition change lpszMenuName from NULL to "MyMenu".
    All you need do now is to add to your switch so it processes the menu message, somethin like
    Code:
    switch(message)
    {
       case WM_COMMAND:
          switch(LOWORD(wParam))
          {
             case IDM_EXIT: PostQuitMessage(0);
                break;
          }
       case WM_DESTROY: PostQuitMessage(0);
          break;
       default: return DefWindowProc(hwnd, message, wParam, lParam);
    }
    I think thats it, oh yeah if your compiler has a resource editor you can create resource's visually.
    Apologise's in advance if I left something out.
    A good book is what you need.

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    I guess the easiest way would be to use a resource script, but the best way (IMHO) is with pure code:

    Code:
    #define ID_FNEW 991
    #define IF_FSAVE 992 //etc...
    //...
    	HMENU hMenu    = CreateMenu(),
    	      hMenuPop = CreateMenu();
    //...
    	case WM_CREATE:
    		AppendMenu(hMenuPop, MF_STRING, ID_FNEW, "&New");
    		AppendMenu(hMenuPop, MF_STRING, ID_FSAVE, "&Save Code");
    		AppendMenu(hMenuPop, MF_STRING, ID_FSAVEAS, "&Save Code As...");
    		AppendMenu(hMenuPop, MF_STRING, ID_FCODE, "&View Code");
    		AppendMenu(hMenuPop, MF_STRING, ID_FEXIT, "&Exit");
    
    		AppendMenu(hMenu, MF_POPUP, (int)hMenuPop, "&File");
    	
    		hMenuPop = CreateMenu();
    	
    		AppendMenu(hMenuPop, MF_STRING, ID_IMENU, "&Menu");
    		AppendMenu(hMenuPop, MF_STRING, ID_IBUTTON, "&Button");
    	
    		AppendMenu(hMenu, MF_POPUP, (int)hMenuPop, "&Insert");
    
    		AppendMenu(hMenu, MF_POPUP, ID_CLEARWIN, "&Clear");
    		SetMenu(hwnd, hMenu);
    		return 0;
    Now if you want to intercept one of these when they're clicked on...

    Code:
    case WM_COMMAND:
        switch(LOWORD(wParam)
        {
              case ID_FNEW:
                      //create a new file... 
                      break;
         }
    return 0;
    and on like that...get it?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    If your usings the resource file and header file i know you need to tell the window what the menu name is. I change the

    winclass.lpszMenuName = "MyMenu";

    line where it says "MyMenu" it would normally say NULL.

    If this is wrong someone tell him, this worked for me but my programs r normally wierd written.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 09:23 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 11:50 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 05:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21