Menus

This is a discussion on Menus within the Windows Programming forums, part of the Platform Specific Boards category; I checked and rechecked everything, i dont understand why my menu wouldnt show up. Code: #include <windows.h> #include <tchar.h> #define ...

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

    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 ;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

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

    Maybe you should start with resource defined menu sample?
    Using Menus
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

    And i would like to do it without resource file.

    Thanks!
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    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;
    Attached Images Attached Images  
    Last edited by abachler; 09-02-2009 at 08:55 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

    I also forgot to hMenu = CreateMenu();

    its working now!

    Cheers!
    Compiler MSVC++ 2013 with Code::Blocks.

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, 08:20 PM
  2. Win32 menus and resources help
    By firestorm in forum Windows Programming
    Replies: 24
    Last Post: 04-12-2005, 02: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

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