Thread: Switch Case and Menus

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    3

    Switch Case and Menus

    I'm learning the Windows API and I have written just a plain program which opens up with a menu on the top, I have created ther menu so that there is a tools button that expands to a button which brings up a message box which then tells you the program directory. This is fine apart from when you close this message box, the whole program quits. This is not what I want. Is there some special piece of code I'm missing or is it my code thats bad I've tried substituting the hwnd in MessageBox() for NULL/0 but it still does the smae thing:

    The windows procedure,

    Code:
    LRESULT CALLBACK WndProc( HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam )
    {
       switch( Msg )
       {
       case WM_COMMAND:
          switch( LOWORD( wParam ) )
          {
          case ID_FILE_EXIT:
             PostMessage( hwnd, WM_CLOSE, 0, 0 );
             break;
          case ID_TOOLS_PROGRAMDIRECTORY:
             {
                HINSTANCE hInstance = GetModuleHandle( NULL );
                char szFileName[MAX_PATH];
    
                GetModuleFileName( hInstance, szFileName, MAX_PATH );
                MessageBox( hwnd, szFileName, "The Directory Is:", MB_OK );
             }
             break;
          }
       case WM_CLOSE:
          DestroyWindow( hwnd );
          break;
       case WM_DESTROY:
          PostQuitMessage( 0 );
          break;
       default:
          return DefWindowProc( hwnd, Msg, wParam, lParam );
       }
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    12
    Found your problem. You missed putting the break; command at the end of the WM_COMMAND case. This causes WM_CLOSE to be called as part of WM_COMMAND so the window will be destroyed every time. Regardless of the button/menu.

    EG:

    Code:
       case WM_COMMAND:
          switch( LOWORD( wParam ) )
          {
          case ID_FILE_EXIT:
             PostMessage( hwnd, WM_CLOSE, 0, 0 );
             break;
          case ID_TOOLS_PROGRAMDIRECTORY:
             {
                HINSTANCE hInstance = GetModuleHandle( NULL );
                char szFileName[MAX_PATH];
    
                GetModuleFileName( hInstance, szFileName, MAX_PATH );
                MessageBox( hwnd, szFileName, "The Directory Is:", MB_OK );
             }
             break;
          }
          break;
    Add the break statement after the final bracket "}"

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    Thanks, problem solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Menu in C, like File, Edit, Window, etc...
    By Livijn in forum Windows Programming
    Replies: 5
    Last Post: 01-18-2007, 05:49 PM
  3. Need help with menus!!!
    By andrewjf9 in forum Windows Programming
    Replies: 3
    Last Post: 10-29-2002, 01:03 PM
  4. Menu System using switch and case.
    By Spectrum48k in forum C Programming
    Replies: 3
    Last Post: 07-23-2002, 08:23 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM