Thread: Menus

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Menus

    I checked and rechecked everything, i dont understand why my menu wouldnt show up.

    Code:
    #include <windows.h>
    #include <tchar.h>
    
    #define IDM_FILE_NEW          40001
    #define IDM_FILE_OPEN         40002
    #define IDM_FILE_SAVE         40003
    #define IDM_FILE_SAVE_AS      40004
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    TCHAR szAppName[] = TEXT ("MenuDemo") ;
    HWND  hwnd;
    HMENU hMenu, h1 ;
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
         switch (message)
         {
         case WM_CREATE:
              h1 = CreateMenu();
              AppendMenu(hMenu, MF_POPUP, (UINT) h1, _T("File"));
              AppendMenu(h1,MF_STRING,IDM_FILE_NEW,  _T("New"));
              AppendMenu(h1,MF_STRING,IDM_FILE_OPEN, _T("Open"));
              AppendMenu(h1,MF_STRING,IDM_FILE_SAVE, _T("Save"));
              AppendMenu(h1,MF_SEPARATOR,0,0);
              AppendMenu(h1,MF_STRING,IDM_FILE_SAVE_AS,_T("Save &As..."));
              SetMenu(hwnd, hMenu);
    
              break ;
    
         case WM_COMMAND:
              hMenu = GetMenu (hwnd) ;
    
              switch (wParam)
              {
              case IDM_FILE_NEW:
              case IDM_FILE_OPEN:
              case IDM_FILE_SAVE:
              case IDM_FILE_SAVE_AS:
                   MessageBeep (0) ;
                   break;
              }
    
         case WM_DESTROY:
              PostQuitMessage (0) ;
              return 0 ;
    
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    {
         MSG      msg ;
         WNDCLASS wndclass ;
    
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH) ;
         wndclass.lpszMenuName  = szAppName ;
         wndclass.lpszClassName = szAppName ;
    
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
    
         hwnd = CreateWindow (szAppName, TEXT ("Menu Demonstration"),
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              HWND_DESKTOP, hMenu, hInstance, NULL) ;
    
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
    
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I do not see where you are initializing the hMenu variable...

    Maybe you should start with resource defined menu sample?
    Using Menus
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    In the beginning of the file: HMENU hMenu, h1 ;

    And i would like to do it without resource file.

    Thanks!
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    you need to SetMenu() to h1, not hMenu. Here is some code from a utility I wrote, ignore the parts that arent relevat to menus

    Code:
            case WM_CREATE:
                for(int x = 0;x<256;x++){
                    hDriveThread[x] = NULL;
                    Pass[x] = 0;
                    }
                hWatchdog = CreateThread(NULL , 0 , Watchdog , NULL , 0 , &ThreadId);
                hMenu = CreateMenu();
                hMenuPopup = CreateMenu();
                pOption = (UINT_PTR)&Option;
                //Option = (UINT)hMenuPopup;
                AppendMenuA(hMenu , MF_STRING | MF_POPUP , (UINT)hMenuPopup , "&Clean Free Space");
                for(DWORD DriveNum = 65;DriveNum<91;DriveNum++){
                    if(GetDriveGeometry(&pdg , DriveNum)){
    					if(1){ //pdg.MediaType == FixedMedia){
    						sprintf(MenuEntry , "%s: - %d" , &DriveNum , pdg.BytesPerSector);
    						//Option = 40000 + DriveNum;
    						AppendMenu(hMenuPopup , MF_STRING , (40000 + DriveNum) , MenuEntry);
    						}
                        }
                    }
                //hMenuPopup2 = CreateMenu();
                AppendMenuA(hMenu , MF_STRING , (50000) , "&About");
                SetMenu(hwnd , hMenu);
                DrawMenuBar(hwnd);
                bResult = GetDriveGeometry(&pdg , 0);
                SetTimer(hwnd , 1 , 50 , NULL);
                return 0;
    Last edited by abachler; 09-02-2009 at 07:55 AM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Ducky View Post
    In the beginning of the file: HMENU hMenu, h1 ;

    And i would like to do it without resource file.

    Thanks!
    this initializes hMenu to NULL.

    How do you plan to append something to NULL-pointer?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks a lot Abachler and Vart!

    I also forgot to hMenu = CreateMenu();

    its working now!

    Cheers!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating menus and other stuff
    By C+noob in forum Windows Programming
    Replies: 4
    Last Post: 07-13-2005, 07:20 PM
  2. Win32 menus and resources help
    By firestorm in forum Windows Programming
    Replies: 24
    Last Post: 04-12-2005, 01:23 PM
  3. Creating pop up menus
    By Ti22 in forum C++ Programming
    Replies: 22
    Last Post: 01-18-2005, 09:27 PM
  4. Menu's
    By Benzakhar in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2004, 10:13 PM
  5. adding menus at runtime
    By bennyandthejets in forum Windows Programming
    Replies: 3
    Last Post: 11-22-2002, 05:07 AM