Thread: Menu code is too large

  1. #1
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133

    Menu code is too large

    Hi.
    I have created a menu for console (I don't know how to create dialog boxes :P) which reacts on mouse input, it fully works but the code is (a little) too big, how can I make it smaller?
    Thanks, Yuri.

    PHP Code:
    #include <iostream>
    #include <windows.h>

    #define Selected SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE );
    #define Un_Selected SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );

    void Screen_Position int Xint Y )
    {

        
    COORD Cursor_Position;

        
    Cursor_Position.X;
        
    Cursor_Position.Y;

        
    SetConsoleCursorPosition GetStdHandle STD_OUTPUT_HANDLE ), Cursor_Position );
    }

    int main()
    {

        
    using namespace std;

        
    SetConsoleTitle "Menu" );

        
    Screen_Position 10);

        
    Un_Selected;

        
    cout << " Minimize ";

        
    Screen_Position 10);

        
    Un_Selected;

        
    cout << " Maximize ";

        
    Screen_Position 10);

        
    Un_Selected;

        
    cout << " Restore ";

        
    Screen_Position 1011 );

        
    Un_Selected;

        
    cout << " Close ";

        
    INPUT_RECORD Record_Input;

        
    DWORD Event_Count,
              
    Input_Size;

        
    bool Mouse_On1 false,
             
    Mouse_On2 false,
             
    Mouse_On3 false,
             
    Mouse_On4 false,
             
    Mouse_Moved_On1 true,
             
    Mouse_Moved_On2 true,
             
    Mouse_Moved_On3 true,
             
    Mouse_Moved_On4 true,
             
    Mouse_Pressed;

        while ( 
    EOF )
        {

            
    GetNumberOfConsoleInputEvents GetStdHandle STD_INPUT_HANDLE ), & Event_Count );

            if ( 
    Event_Count != )
            {

                
    ReadConsoleInput GetStdHandle STD_INPUT_HANDLE ), & Record_Input1, & Input_Size );

                if ( 
    Record_Input.EventType == MOUSE_EVENT )
                {

                    
    MOUSE_EVENT_RECORD Mouse Record_Input.Event.MouseEvent;

                    if ( 
    Mouse.dwMousePosition.>= 11 &&
                         
    Mouse.dwMousePosition.<= 18 &&
                         
    Mouse.dwMousePosition.== )
                    {

                        if ( 
    Mouse.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED )
                        {

                            
    mciSendString "Play click.wav"NULL0NULL );

                            
    Screen_Position 10);

                            
    Selected;

                            
    cout << "[Minimize]";

                            
    Mouse_Pressed true;

                            while ( 
    Mouse.dwMousePosition.>= 11 &&
                                    
    Mouse.dwMousePosition.<= 18 &&
                                    
    Mouse.dwMousePosition.== &&
                                    
    Mouse_Pressed == true )
                            {

                                
    ReadConsoleInput GetStdHandle STD_INPUT_HANDLE ), & Record_Input1, & Input_Size );

                                if ( 
    Record_Input.EventType == MOUSE_EVENT )
                                {

                                    
    Mouse Record_Input.Event.MouseEvent;

                                    if ( 
    Mouse.dwButtonState == ! FROM_LEFT_1ST_BUTTON_PRESSED )
                                    {

                                        
    mciSendString "Play open.wav"NULL0NULL );

                                        
    Screen_Position 10);

                                        
    Un_Selected;

                                        
    cout << " Minimize ";

                                        
    ShowWindow FindWindow "ConsoleWindowClass""Menu" ), SW_MINIMIZE );

                                        
    Mouse_Pressed false;
                                    }
                                }
                            }
                        }

                        else if ( 
    Mouse_Moved_On1 == true )
                        {

                            
    mciSendString "Play hover.wav"NULL0NULL );

                            
    Screen_Position 10);

                            
    Selected;

                            
    cout << " Minimize ";

                            
    Mouse_Moved_On1 false;
                        }

                        
    Mouse_On1 true;
                    }

                    else if ( 
    Mouse_On1 == true )
                    {

                        
    Screen_Position 10);

                        
    Un_Selected;

                        
    cout << " Minimize ";

                        
    Mouse_On1 false,
                        
    Mouse_Moved_On1 true;
                    }

                    if ( 
    Mouse.dwMousePosition.>= 11 &&
                         
    Mouse.dwMousePosition.<= 18 &&
                         
    Mouse.dwMousePosition.== )
                    {

                        if ( 
    Mouse.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED )
                        {

                            
    mciSendString "Play click.wav"NULL0NULL );

                            
    Screen_Position 10);

                            
    Selected;

                            
    cout << "[Maximize]";

                            
    Mouse_Pressed true;

                            while ( 
    Mouse.dwMousePosition.>= 11 &&
                                    
    Mouse.dwMousePosition.<= 18 &&
                                    
    Mouse.dwMousePosition.== &&
                                    
    Mouse_Pressed == true )
                            {

                                
    ReadConsoleInput GetStdHandle STD_INPUT_HANDLE ), & Record_Input1, & Input_Size );

                                if ( 
    Record_Input.EventType == MOUSE_EVENT )
                                {

                                    
    Mouse Record_Input.Event.MouseEvent;

                                    if ( 
    Mouse.dwButtonState == ! FROM_LEFT_1ST_BUTTON_PRESSED )
                                    {

                                        
    mciSendString "Play open.wav"NULL0NULL );

                                        
    Screen_Position 10);

                                        
    Un_Selected;

                                        
    cout << " Maximize ";

                                        
    ShowWindow FindWindow "ConsoleWindowClass""Menu" ), SW_SHOWMAXIMIZED );

                                        
    Mouse_Pressed false;
                                    }
                                }
                            }
                        }

                        else if ( 
    Mouse_Moved_On2 == true )
                        {

                            
    mciSendString "Play hover.wav"NULL0NULL );

                            
    Screen_Position 10);

                            
    Selected;

                            
    cout << " Maximize ";

                            
    Mouse_Moved_On2 false;
                        }

                        
    Mouse_On2 true;
                    }

                    else if ( 
    Mouse_On2 == true )
                    {

                        
    Screen_Position 10);

                        
    Un_Selected;

                        
    cout << " Maximize ";

                        
    Mouse_On2 false,
                        
    Mouse_Moved_On2 true;
                    }

                    if ( 
    Mouse.dwMousePosition.>= 11 &&
                         
    Mouse.dwMousePosition.<= 17 &&
                         
    Mouse.dwMousePosition.== )
                    {

                        if ( 
    Mouse.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED )
                        {

                            
    mciSendString "Play click.wav"NULL0NULL );

                            
    Screen_Position 10);

                            
    Selected;

                            
    cout << "[Restore]";

                            
    Mouse_Pressed true;

                            while ( 
    Mouse.dwMousePosition.>= 11 &&
                                    
    Mouse.dwMousePosition.<= 17 &&
                                    
    Mouse.dwMousePosition.== &&
                                    
    Mouse_Pressed == true )
                            {

                                
    ReadConsoleInput GetStdHandle STD_INPUT_HANDLE ), & Record_Input1, & Input_Size );

                                if ( 
    Record_Input.EventType == MOUSE_EVENT )
                                {

                                    
    Mouse Record_Input.Event.MouseEvent;

                                    if ( 
    Mouse.dwButtonState == ! FROM_LEFT_1ST_BUTTON_PRESSED )
                                    {

                                        
    mciSendString "Play open.wav"NULL0NULL );

                                        
    Screen_Position 10);

                                        
    Un_Selected;

                                        
    cout << " Restore ";

                                        
    ShowWindow FindWindow "ConsoleWindowClass""Menu" ), SW_SHOWDEFAULT );

                                        
    Mouse_Pressed false;
                                    }
                                }
                            }
                        }

                        else if ( 
    Mouse_Moved_On3 == true )
                        {

                            
    mciSendString "Play hover.wav"NULL0NULL );

                            
    Screen_Position 10);

                            
    Selected;

                            
    cout << " Restore ";

                            
    Mouse_Moved_On3 false;
                        }

                        
    Mouse_On3 true;
                    }

                    else if ( 
    Mouse_On3 == true )
                    {

                        
    Screen_Position 10);

                        
    Un_Selected;

                        
    cout << " Restore ";

                        
    Mouse_On3 false,
                        
    Mouse_Moved_On3 true;
                    }

                    if ( 
    Mouse.dwMousePosition.>= 11 &&
                         
    Mouse.dwMousePosition.<= 15 &&
                         
    Mouse.dwMousePosition.== 11 )
                    {

                        if ( 
    Mouse.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED )
                        {

                            
    mciSendString "Play click.wav"NULL0NULL );

                            
    Screen_Position 1011 );

                            
    Selected;

                            
    cout << "[Close]";

                            
    Mouse_Pressed true;

                            while ( 
    Mouse.dwMousePosition.>= 11 &&
                                    
    Mouse.dwMousePosition.<= 15 &&
                                    
    Mouse.dwMousePosition.== 11 &&
                                    
    Mouse_Pressed == true )
                            {

                                
    ReadConsoleInput GetStdHandle STD_INPUT_HANDLE ), & Record_Input1, & Input_Size );

                                if ( 
    Record_Input.EventType == MOUSE_EVENT )
                                {

                                    
    Mouse Record_Input.Event.MouseEvent;

                                    if ( 
    Mouse.dwButtonState == ! FROM_LEFT_1ST_BUTTON_PRESSED )
                                    {

                                        
    mciSendString "Play back.wav"NULL0NULL );

                                        
    Sleep 250 );

                                        return 
    0;
                                    }
                                }
                            }
                        }

                        else if ( 
    Mouse_Moved_On4 == true )
                        {

                            
    mciSendString "Play hover.wav"NULL0NULL );

                            
    Screen_Position 1011 );

                            
    Selected;

                            
    cout << " Close ";

                            
    Mouse_Moved_On4 false;
                        }

                        
    Mouse_On4 true;
                    }

                    else if ( 
    Mouse_On4 == true )
                    {

                        
    Screen_Position 1011 );

                        
    Un_Selected;

                        
    cout << " Close ";

                        
    Mouse_On4 false,
                        
    Mouse_Moved_On4 true;
                    }
                }
            }

            
    Sleep 10 );
        }


  2. #2
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    umm, learn the Win32 API? (Make some real Windows programs.) That's the only way I can think of..

    Other than that, if the code is too big for your liking to go in main(), just put it in a function called menu() (or something else) and stick it in main() whenever you need the menu.
    Last edited by homeyg; 03-15-2006 at 10:17 AM.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You should identify common properties in your menu options and build a class from it. Each menu option should inherit and implement from that class. Then have some kind of generic menu handler class.
    Code:
    class CMenu
    {
      ...
    };
    
    class CMenuMinimize : public CMenu
    {
      ...
    };
    
    class CMenuMaximize : public CMenu
    {
      ...
    };
    
    class CMenuHandler
    {
      void RegisterMenu(CMenu* Menu);
    };
    
    MenuHandler.RegisterMenu(new CMenuMinimize());
    MenuHandler.RegisterMenu(new CMenuMaximize());
    ...or something, I didn't read much of oyur code knowing what it's about.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Something like this:

    Code:
    class CMenuItem  
    {
    };
    
    class CMenu 
    { 
      CMenuItem *m_pItems; 
    };
    
    class CMenuSystem 
    {
      CMenu *m_pMenus; 
    };
    Of course I've left a lot for you to fill in.

    CMenuItem encaspulates one menu item.
    CMenu encapsulates one complete menu
    CMenuSystem encapsulates/controls the entire menu system.

  5. #5
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Thanks for the ideas but haven't learned classes yet, :S. Though, I had an idea, it's from 12 to 6 KB. Ow, and you really should compile it, it's pretty funny.

    PHP Code:
    #include <iostream>
    #include <windows.h>

    using namespace std;

    bool Mouse_On 16 ],
         
    Mouse_Moved_On 16 ];

    DWORD Event_Count,
          
    Input_Size;

    INPUT_RECORD Record_Input;

    MOUSE_EVENT_RECORD Mouse;

    void Screen_Position int Xint Y )
    {

        
    COORD Cursor_Position;

        
    Cursor_Position.X;
        
    Cursor_Position.Y;

        
    SetConsoleCursorPosition GetStdHandle STD_OUTPUT_HANDLE ), Cursor_Position );
    }

    void Menu int Heightint Option_IDint Function, string Text )
    {

        if ( 
    Mouse.dwMousePosition.>= &&
             
    Mouse.dwMousePosition.<= 10 &&
             
    Mouse.dwMousePosition.== Height )
        {

            if ( 
    Mouse.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED )
            {

                
    mciSendString "Play click.wav"NULL0NULL );

                
    SetConsoleTextAttribute GetStdHandle STD_OUTPUT_HANDLE ), BACKGROUND_INTENSITY BACKGROUND_RED BACKGROUND_GREEN BACKGROUND_BLUE );

                
    Screen_Position 1Height );

                
    cout << "[" << Text << "]";

                
    bool Mouse_Pressed true;

                while ( 
    Mouse.dwMousePosition.>= &&
                        
    Mouse.dwMousePosition.<= 10 &&
                        
    Mouse.dwMousePosition.== Height &&
                        
    Mouse_Pressed == true )
                {

                    
    ReadConsoleInput GetStdHandle STD_INPUT_HANDLE ), & Record_Input1, & Input_Size );

                    if ( 
    Record_Input.EventType == MOUSE_EVENT )
                    {

                        
    Mouse Record_Input.Event.MouseEvent;

                        if ( 
    Mouse.dwButtonState == ! FROM_LEFT_1ST_BUTTON_PRESSED )
                        {

                            if ( Function == 
    )
                            {

                                
    mciSendString "Play back.wav"NULL0NULL );

                                
    Sleep 250 );

                                exit ( 
    );
                            }

                            else
                            {

                                
    mciSendString "Play open.wav"NULL0NULL );

                                
    SetConsoleTextAttribute GetStdHandle STD_OUTPUT_HANDLE ), FOREGROUND_INTENSITY FOREGROUND_RED FOREGROUND_GREEN FOREGROUND_BLUE );

                                
    Screen_Position 1Height );

                                
    cout << " " << Text << " ";

                                
    ShowWindow FindWindow "ConsoleWindowClass""Menu" ), Function );
                            }

                            
    Mouse_Pressed false;
                        }
                    }
                }
            }

            else if ( 
    Mouse_Moved_On Option_ID ] == true )
            {

                
    mciSendString "Play hover.wav"NULL0NULL );

                
    SetConsoleTextAttribute GetStdHandle STD_OUTPUT_HANDLE ), BACKGROUND_INTENSITY BACKGROUND_RED BACKGROUND_GREEN BACKGROUND_BLUE );

                
    Screen_Position 1Height );

                
    cout << " " << Text << " ";

                
    Mouse_Moved_On Option_ID ]  = false;
            }

            
    Mouse_On Option_ID ] = true;
        }

        else if ( 
    Mouse_On Option_ID ] == true )
        {

            
    SetConsoleTextAttribute GetStdHandle STD_OUTPUT_HANDLE ), FOREGROUND_INTENSITY FOREGROUND_RED FOREGROUND_GREEN FOREGROUND_BLUE );

            
    Screen_Position 1Height );

            
    cout << " " << Text << " ";

            
    Mouse_On Option_ID ] = false,
            
    Mouse_Moved_On Option_ID ] = true;
        }
    }

    int main()
    {

        
    SetConsoleTitle "Menu" );

        
    SetConsoleTextAttribute GetStdHandle STD_OUTPUT_HANDLE ), FOREGROUND_INTENSITY FOREGROUND_RED FOREGROUND_GREEN FOREGROUND_BLUE );

        
    Screen_Position 0);

        
    cout << "\xC9\xFE\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBB" << endl;
        
    cout << "\xBA Minimize \xBA" << endl;
        
    cout << "\xC7\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xB6" << endl;
        
    cout << "\xBA Maximize \xBA" << endl;
        
    cout << "\xC7\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xB6" << endl;
        
    cout << "\xBA Restore  \xBA" << endl;
        
    cout << "\xC7\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xB6" << endl;
        
    cout << "\xBA Close    \xBA" << endl;
        
    cout << "\xC8\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBC";

        
    Mouse_On ] = false,
        
    Mouse_On ] = false,
        
    Mouse_On ] = false,
        
    Mouse_On ] = false,
        
    Mouse_Moved_On ] = true,
        
    Mouse_Moved_On ] = true,
        
    Mouse_Moved_On ] = true,
        
    Mouse_Moved_On ] = true;

        while ( 
    EOF )
        {

            
    GetNumberOfConsoleInputEvents GetStdHandle STD_INPUT_HANDLE ), & Event_Count );

            if ( 
    Event_Count != )
            {

                
    ReadConsoleInput GetStdHandle STD_INPUT_HANDLE ), & Record_Input1, & Input_Size );

                if ( 
    Record_Input.EventType == MOUSE_EVENT )
                {

                    
    Mouse Record_Input.Event.MouseEvent;

                    
    Menu 11SW_MINIMIZE"Minimize" );
                    
    Menu 32SW_SHOWMAXIMIZED"Maximize" );
                    
    Menu 53SW_SHOWDEFAULT"Restore " );
                    
    Menu 740"Close   " );
                }
            }

           
    Sleep 10 );
        }


  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    1

    im having trouble

    for some reason neither of these menu programs work for me. i get like 102 errors and i have no idea what to do to fix them. does anybody have any suggestions.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use a Windows compiler. That code won't compile with compilers like Turbo C++.

    Try Bloodshed's Dev-C++. bloodshed.net/devcpp.html

    [edit] PHP code tags look weird . . . consider using maxorator's syntax highlighter or my codeform. [/edit]

    [edit=2] Some thoughts:
    Code:
            Mouse_On [ Option_ID ] = false, 
            Mouse_Moved_On [ Option_ID ] = true;
    and
    Code:
        Mouse_On [ 1 ] = false, 
        Mouse_On [ 2 ] = false, 
        Mouse_On [ 3 ] = false, 
        Mouse_On [ 4 ] = false, 
        Mouse_Moved_On [ 1 ] = true, 
        Mouse_Moved_On [ 2 ] = true, 
        Mouse_Moved_On [ 3 ] = true, 
        Mouse_Moved_On [ 4 ] = true;
    Why use the comma operator? It's not like this is the only body of a loop or something. Nor are the assignments on the same line. It just seems like semicolons would be just fine. But it's your code.

    Code:
    while ( EOF )
    That's just weird. If you want an infinite loop, use something like for( ; ; ) or while(1) or while(true).

    I might wrap those calls to SetConsoleTextAttribute() in another function. You call it often enough.

    Oh, and exit() is in <cstdlib>. [/edit]
    Last edited by dwks; 01-09-2008 at 02:59 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Thread is quite old and the OP, Yuri, seems banned, so I don't think there's any point to offer suggestions anymore.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Hmm, you're right. Didn't notice the bump.

    Moderators: Feel free to close the thread . . . .



    My pleasure. -- CornedBee
    Last edited by CornedBee; 01-09-2008 at 04:14 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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 driven code
    By liquidcourage1 in forum C++ Programming
    Replies: 13
    Last Post: 04-23-2006, 05:51 PM
  3. Best "Menu" method?
    By SSJMetroid in forum Game Programming
    Replies: 11
    Last Post: 12-08-2005, 12:05 AM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM