diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c31b73 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +################################################################################ +# Diese .gitignore-Datei wurde von Microsoft(R) Visual Studio automatisch erstellt. +################################################################################ + +/.vs diff --git a/MenuLib.cpp b/MenuLib.cpp new file mode 100644 index 0000000..9546ae6 --- /dev/null +++ b/MenuLib.cpp @@ -0,0 +1,10 @@ +// MenuLib.cpp : Hiermit werden die Funktionen für die statische Bibliothek definiert. +// + +#include "pch.h" +#include "framework.h" + +// TODO: Dies ist ein Beispiel für eine Bibliotheksfunktion. +void fnMenuLib() +{ +} diff --git a/MenuLib.sln b/MenuLib.sln new file mode 100644 index 0000000..4ea7b2b --- /dev/null +++ b/MenuLib.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29613.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MenuLib", "MenuLib.vcxproj", "{5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Debug|x64.ActiveCfg = Debug|x64 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Debug|x64.Build.0 = Debug|x64 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Debug|x86.ActiveCfg = Debug|Win32 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Debug|x86.Build.0 = Debug|Win32 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Release|x64.ActiveCfg = Release|x64 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Release|x64.Build.0 = Release|x64 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Release|x86.ActiveCfg = Release|Win32 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FCB19F7F-D5E2-44E3-8B33-C2797FF62817} + EndGlobalSection +EndGlobal diff --git a/MenuLib.vcxproj b/MenuLib.vcxproj new file mode 100644 index 0000000..e933243 --- /dev/null +++ b/MenuLib.vcxproj @@ -0,0 +1,167 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36} + Win32Proj + MenuLib + 10.0 + + + + StaticLibrary + true + v142 + Unicode + + + StaticLibrary + false + v142 + true + Unicode + + + StaticLibrary + true + v142 + Unicode + + + StaticLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Use + Level3 + true + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + + + + + Use + Level3 + true + _DEBUG;_LIB;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + + + + + Use + Level3 + true + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + true + true + + + + + Use + Level3 + true + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + true + true + + + + + + + + + + + + Create + Create + Create + Create + + + + + + \ No newline at end of file diff --git a/MenuLib.vcxproj.filters b/MenuLib.vcxproj.filters new file mode 100644 index 0000000..14b86da --- /dev/null +++ b/MenuLib.vcxproj.filters @@ -0,0 +1,39 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Headerdateien + + + Headerdateien + + + Headerdateien + + + + + Quelldateien + + + Quelldateien + + + Quelldateien + + + \ No newline at end of file diff --git a/MenuLib.vcxproj.user b/MenuLib.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/MenuLib.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/framework.h b/framework.h new file mode 100644 index 0000000..862e2c2 --- /dev/null +++ b/framework.h @@ -0,0 +1,3 @@ +#pragma once + +#define WIN32_LEAN_AND_MEAN // Selten verwendete Komponenten aus Windows-Headern ausschließen diff --git a/menu.c b/menu.c new file mode 100644 index 0000000..f88fa2e --- /dev/null +++ b/menu.c @@ -0,0 +1,119 @@ +#include "pch.h" +#include "menu.h" + +// Checks if a line index should display a menu item +bool is_item_line(const int line, const int itemc, int* item_index) +{ + if (line >= 2 && line % 2 == 0) + { + // Lines 2, 4, ... + *item_index = (line - 2) / 2; + return *item_index < itemc; + } + return false; +} + +// Acquires the dimensions (width and height) of the console window +void get_console_dimensions(int* width, int* height) +{ + CONSOLE_SCREEN_BUFFER_INFO csbi; + + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + *width = csbi.srWindow.Right - csbi.srWindow.Left + 1; + *height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; +} + +void show_menu(const int itemc, struct MenuItem itemv[], const char title[], bool loopback, bool pause) +{ + unsigned width, height, item_index; + char key; + bool action_performed; + + // Clear the console window + system("cls"); + + // Get the width and height of the console window + get_console_dimensions(&width, &height); + + // Print frame with title + for (unsigned i = 0; i < height - 1; ++i) + { + // Top line with title + if (i == 0) + { + for (unsigned j = 0; j < width - strlen(title) + 1; ++j) + { + if (j == (width - strlen(title)) / 2) + { + fputs(title, stdout); + } + else + { + putchar('*'); + } + } + } + // Line with menu item + else if (is_item_line(i, itemc, &item_index)) + { + // Print item text + printf("*\t%c) %s", itemv[item_index].key, itemv[item_index].text); + + // Print right side of frame + for (unsigned j = 0; j < width - strlen(itemv[item_index].text) - 12; ++j) + { + putchar(' '); + } + putchar('*'); + } + // Line above bottom line + else if (i == height - 2) + { + for (unsigned j = 0; j < width; ++j) + { + putchar('*'); + } + } + // Blank line + else + { + putchar('*'); + for (unsigned j = 0; j < width - 2; ++j) + { + putchar(' '); + } + putchar('*'); + } + putchar('\n'); + } + + // Wait for user selection + putchar('> '); + action_performed = false; + do { + key = _getch(); + for (int i = 0; i < itemc; ++i) + { + if (itemv[i].key == key) + { + // Perform action + system("cls"); + itemv[i].action(); + action_performed = true; + + // Pause if requested + if (pause) + { + putchar('\n'); + system("pause"); + } + } + } + } while (!action_performed); + + // Show menu again if requested + if (loopback) + { + show_menu(itemc, itemv, title, loopback, pause); + } +} \ No newline at end of file diff --git a/menu.h b/menu.h new file mode 100644 index 0000000..bdb7fe1 --- /dev/null +++ b/menu.h @@ -0,0 +1,18 @@ +#pragma once + +#include "pch.h" + +/// Represents one menu item in a menu. +struct MenuItem { + char* text; + char key; + void* (*action)(void); +}; + +/// Displaces a CUI menu to the user and lets them choose an option, then calls the corresponding function. +/// The length of the array itemv of menu items. +/// An array of all menu items to display in the menu. +/// The title of the menu. +/// If this parameter is set to true, the menu will be displayed again after an action is executed. +/// If this parameter is set to true, a pausecommand will be run after an action is executed. +void show_menu(const int itemc, struct MenuItem itemv[], const char title[], bool loopback, bool pause); \ No newline at end of file diff --git a/pch.cpp b/pch.cpp new file mode 100644 index 0000000..1fcbac0 --- /dev/null +++ b/pch.cpp @@ -0,0 +1,5 @@ +// pch.cpp: Quelldatei, die dem vorkompilierten Header entspricht + +#include "pch.h" + +// Bei der Verwendung vorkompilierter Header ist diese Quelldatei für eine erfolgreiche Kompilierung erforderlich. diff --git a/pch.h b/pch.h new file mode 100644 index 0000000..e0e6e72 --- /dev/null +++ b/pch.h @@ -0,0 +1,21 @@ +// pch.h: Dies ist eine vorkompilierte Headerdatei. +// Die unten aufgeführten Dateien werden nur einmal kompiliert, um die Buildleistung für zukünftige Builds zu verbessern. +// Dies wirkt sich auch auf die IntelliSense-Leistung aus, Codevervollständigung und viele Features zum Durchsuchen von Code eingeschlossen. +// Die hier aufgeführten Dateien werden jedoch ALLE neu kompiliert, wenn mindestens eine davon zwischen den Builds aktualisiert wird. +// Fügen Sie hier keine Dateien hinzu, die häufig aktualisiert werden sollen, da sich so der Leistungsvorteil ins Gegenteil verkehrt. + +#ifndef PCH_H +#define PCH_H + +// Fügen Sie hier Header hinzu, die vorkompiliert werden sollen. +#include "framework.h" + +#include +#include +#include +#include + +#include +#include + +#endif //PCH_H