Wikipedia

Search results

Saturday, November 23, 2024

Creating Simple Selectable Menu in Turbo C++ [graphics mode]

 Here is simple code for creating a menu in graphics mode in turbo c++. you can select menus by pressing up and down arrow keys.


#include <graphics.h>

#include <conio.h>


void drawMenu(int x, int y, char* items[], int count, int selected) {

    int height = 20;

    for (int i = 0; i < count; i++) {

        if (i == selected) {

            setcolor(RED);

        } else {

            setcolor(WHITE);

        }

        rectangle(x, y + (i * height), x + 100, y + (i * height) + height);

        outtextxy(x + 5, y + (i * height) + 5, items[i]);

    }

}


int main() {

    int gd = DETECT, gm;

    initgraph(&gd, &gm, "C:\\Turboc3\\BGI");


    char* menuItems[] = {"Option 1", "Option 2", "Option 3"};

    int selectedItem = 0;

    int itemCount = 3;

    

    drawMenu(50, 150, menuItems, itemCount, selectedItem);

    

    while (1) {

        if (kbhit()) {

            int ch = getch();

            if (ch == 0) { // special keys

                ch = getch();

                switch (ch) {

                    case 72: // up arrow key

                        selectedItem = (selectedItem - 1 + itemCount) % itemCount;

                        break;

                    case 80: // down arrow key

                        selectedItem = (selectedItem + 1) % itemCount;

                        break;

                }

                drawMenu(50, 150, menuItems, itemCount, selectedItem);

            } else if (ch == 13) { // Enter key

                outtextxy(50, 250, "You selected: ");

                outtextxy(150, 250, menuItems[selectedItem]);

                break;

            }

        }

    }

    

    getch();

    closegraph();

    return 0;

}


output:




No comments:

Post a Comment

Creating Simple Selectable Menu in Turbo C++ [graphics mode]

 Here is simple code for creating a menu in graphics mode in turbo c++. you can select menus by pressing up and down arrow keys. #include ...