简体   繁体   English

在c#中创建简单的控制台菜单

[英]Making simple console menu in c#

I want to make a simple menu in C# like : something like this should be printed out of console : 我想在C#中创建一个简单的菜单:这样的东西应该从控制台打印出来:

FirstOption 
SecondOption
Exit

So far here is my code (there are problems with naming and encapsulation, but this is all just quick prototype, spent ~30 minutes): 到目前为止,这里是我的代码(命名和封装存在问题,但这只是快速原型,花了大约30分钟):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Menu StartGame = new Menu("Start Game");
            Menu EndGame = new Menu("End Game");
            Console.WriteLine(StartGame);
            Console.WriteLine(End Game);
            EndGame.isChecked = false;
        }
    }

    class Menu
    {
        private string Content;
        public bool isChecked = true;

        public Menu(string Content)
        {
            this.Content = Content;
        }
        public void CheckCondition()
        {
            if (isChecked)
            {
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.Black;
            }
            else
            {
                Console.ResetColor();
            }
        }
        public override string ToString()
        {
            this.CheckCondition();
            return this.Content;
        }
    }
}

The idea is when a button is clicked the menu item is highlighted. 这个想法是当点击一个按钮时,菜单项突出显示。 When one come to the last menu item he can't press DownArrow again, the same for the first item and UpArrow . 当一个人到达最后一个菜单项时,他无法再次按下DownArrow ,第一个项目和UpArrow

I'm completely stuck with this. 我完全坚持这个。

I am not completely sure.. May be this could help you to get started. 我不完全确定..可能这可以帮助你开始。

while (true)
        {
            var ch = Console.ReadKey(false).Key;
            switch (ch)
            {
                case ConsoleKey.UpArrow:
                    HighlightStartGame();
                    break;

                case ConsoleKey.DownArrow:
                    HighlightEndGame();
                    break;
            }
        }
static void HighlightStartGame()
    {
        Console.Clear();
        Console.ResetColor();
        StartGame.isChecked = true;
        Console.WriteLine(StartGame);
        EndGame.isChecked = false;
        Console.WriteLine(EndGame);

    }

    static void HighlightEndGame()
    {
        Console.Clear();
        Console.ResetColor();
        StartGame.isChecked = false;
        Console.WriteLine(StartGame);
        EndGame.isChecked = true;
        Console.WriteLine(EndGame);

    }

No you can't just do that because Win32 console doesn't support those methods. 不,你不能这样做,因为Win32控制台不支持这些方法。 You can however use GDI to draw on the console window. 但是,您可以使用GDI在控制台窗口上绘图。

The problem is that the console cannot process any mouse events. 问题是控制台无法处理任何鼠标事件。 How do you want to click on the menu? 你想如何点击菜单? You will have to do everything with keys. 你必须用钥匙做所有事情。 The options you have are to either define keystrokes (like Ctrl-F or Alt-F for "FirstEntry") in order to activate menu entries, or to implement a navigation with arrow keys, allowing you to move around fields (button or menu fields and text fields). 您可以选择定义击键(如Ctrl-F或Alt-F表示“FirstEntry”)以激活菜单项,或使用箭头键实现导航,允许您在字段(按钮或菜单字段)中移动和文本字段)。 This is not built in, so you will have to do everything in code. 这不是内置的,因此您必须在代码中执行所有操作。 You will have to use the SetCursorPosition and the ReadKey methods of the console in order to achieve this. 您必须使用控制台的SetCursorPositionReadKey方法才能实现此目的。 I remember having done this on a VT100 terminal eons ago. 我记得曾经在VT100终端上做过这个。

I've written a console menu library for C# . 为C#编写了一个控制台菜单库 It has no mouse support, but it might still be a starting point for you? 它没有鼠标支持,但它可能仍然是你的起点?

CMenu is a lightweight, low-ceremony framework for building console menus in .Net. CMenu是一个轻量级,低仪式的框架,用于在.Net中构建控制台菜单。 Instead of manually prompting the user for input and parsing it, you define commands in a short, structured and comprehensive way, and let CMenu handle the rest. 您可以以简短,结构化和全面的方式定义命令,而不是手动提示用户输入和解析它,让CMenu处理其余的命令。

CMenu aims for low overhead - simple stuff should be simple to implement. CMenu旨在降低开销 - 简单的东西应该易于实现。 If you don't use a feature, you don't need to know anything about it. 如果您不使用某项功能,则无需了解任何相关信息。

At the same time, complex scenarios are supported. 同时,支持复杂的方案。 Large menus can easily be split into several classes. 大菜单可以轻松分成几个类。 Background self-configuration. 背景自我配置。 You do not have to worry about all the annoying details involved in larger menus, it will just work. 您不必担心较大菜单中涉及的所有烦人细节,它只会起作用。

Most importantly, it is very simple and fast to use. 最重要的是,它使用起来非常简单快捷。 Commands can be abbreviated, a smart parser enables even partial matching. 命令可以缩写,智能解析器甚至可以进行部分匹配。 A help command is integrated. 集成了帮助命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM