在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例常用C#方法 → C# 美化控制台 实例源码下载

C# 美化控制台 实例源码下载

常用C#方法

下载此实例
  • 开发语言:C#
  • 实例大小:0.04M
  • 下载次数:22
  • 浏览次数:823
  • 发布时间:2017-04-07
  • 实例类别:常用C#方法
  • 发 布 人:mmt
  • 文件格式:.zip
  • 所需积分:1
 相关标签: 控制 控制台

实例介绍

【实例简介】

刚学C#的时候写的代码,功能看图便知、请忽略数据库

【实例截图】

【核心代码】

    /// <summary>
    /// 美化控制台的代码块
    /// </summary>
    class cons
    {
        #region 设置控制台样式
        public static void SetConsole()
        {
            //设置控制台窗口尺寸
            Console.SetWindowSize(100,20);
            //设置缓冲区尺寸
            Console.SetBufferSize(100,20);
            //设置控制台背景色
            Console.BackgroundColor = ConsoleColor.Blue;
            //设置字体颜色
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Clear();    //必须清屏才能设置
            //设置控制台标题
            Console.Title = "\nABC";
        }
        #endregion

        #region 菜单
        /// <summary>
        /// 显示菜单
        /// </summary>
        /// <param name="title">菜单标题</param>
        /// <param name="select">菜单选项(长度应不大于10)</param>
        /// <param name="x">菜单标题与控制台左边的距离</param>
        /// <param name="y">菜单标题与控制台上边的距离</param>
        /// <returns>选择的操作数(select 的下标)</returns>
        public static int ShowMenu(String title, string[] select, int x, int y)
        {
            //选中的操作数(select的下标)
            int num = 0;
            //记录当前鼠标是否可见
            bool CurVis0 = Console.CursorVisible;
            //记录当前控制台前景色
            ConsoleColor F = Console.ForegroundColor;
            //记录当前控制台背景色
            ConsoleColor B = Console.BackgroundColor;
            //菜单项数
            int Count = select.Length;
            //设置鼠标不可见
            Console.CursorVisible = false;

            #region 遍历每行,记录所有行中最大的宽度
            //菜单标题占控制台的宽度
            int titleWidth = 0;
            //菜单选项占控制台的宽度
            int selectWidth = 0;
            //设置前景色和背景色一样
            Console.ForegroundColor = B;

            //把标题按换行符拆分成多行
            string[] titles = title.Split(new char[] { '\r', '\n' });
            foreach (string item in titles)
            {
                //设置光标位置
                Console.SetCursorPosition(x, y);
                Console.Write(item);
                //更新菜单宽度
                titleWidth = Math.Max(titleWidth, Console.CursorLeft - x);
            }

            foreach (string item in select)
            {
                //设置光标位置
                Console.SetCursorPosition(x, y);
                Console.Write(item);
                //更新菜单宽度
                selectWidth = Math.Max(selectWidth, Console.CursorLeft - x);
            }
            //清除残留文字
            Console.SetCursorPosition(x, y);
            for (int i = Math.Max(titleWidth, selectWidth); i > 0; i--)
            {
                Console.Write(" ");
            }
            //恢复前景色
            Console.ForegroundColor = F;

            #endregion

            #region 打印菜单标题
            //打印菜单标题
            foreach (string item in titles)
            {
                //设置光标列位置
                Console.CursorLeft = x;
                //打印按换行符拆分后的标题
                Console.WriteLine(item);
            }
            #endregion

            #region 输出菜单选项
            //计算使选项与菜单标题居中对齐的列位置
            x = ((titleWidth - selectWidth) / 2);
            //记录当前行位置
            y = Console.CursorTop;

            for (int i = 0; i < Count; i )
            {
                //设置要显示的项的下标
                int j = (i 1) % Count;

                //设置光标列位置
                Console.CursorLeft = x - Convert.ToString(j).Length;
                Console.WriteLine(j "、" select[j]);
            }

            #endregion

            #region 输入键来选择相应选项
            while (true)
            {
                //设置光标位置
                Console.SetCursorPosition(x, y (num Count - 1) % Count);
                //设置被选择项背景色
                Console.BackgroundColor = ConsoleColor.DarkGreen;
                //设置标注符号的颜色
                Console.ForegroundColor = ConsoleColor.Red;
                //显示标注符号
                Console.Write("●");
                //设置被选择项的前景色
                Console.ForegroundColor = ConsoleColor.Yellow;
                //突出显示被选择项
                Console.Write(select[num]);

                //用户输入一个键
                ConsoleKeyInfo key = Console.ReadKey(true);

                //取消突出显示
                Console.SetCursorPosition(x - Convert.ToString(num).Length, y (num Count - 1) % Count);
                Console.BackgroundColor = B;
                Console.ForegroundColor = F;
                Console.Write(num "、" select[num]);


                //用户输入向上方向键
                if (key.Key == ConsoleKey.UpArrow)
                {
                    num = (num Count - 1) % (Count);
                    continue;
                }
                //用户输入向下方向键
                if (key.Key == ConsoleKey.DownArrow)
                {
                    num = (num Count 1) % (Count);
                    continue;
                }
                //用户输入数字键
                if (key.KeyChar < Count 48 && key.KeyChar >= '0')
                {
                    int newNum = (int)key.KeyChar - 48;
                    for (int i = Count; i > 10; i /= 10)
                    {
                        //用户输入一个键
                        ConsoleKeyInfo k = Console.ReadKey(true);
                        //用户输入数字键
                        if (k.KeyChar < Count 48 && k.KeyChar >= '0')
                        {
                            newNum = newNum * 10 (int)k.KeyChar - 48;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (newNum < Count)
                        num = newNum;
                    continue;
                }
                //用户输入enter键
                if (key.Key == ConsoleKey.Enter)
                {
                    break;
                }
            }
            #endregion


            return num;
        }
        #endregion

        #region 输入密码
        /// <summary>
        /// 输入密码
        /// </summary>
        /// <param name="maxLen">密码最大长度</param>
        /// <returns>输入的密码</returns>
        public static char[] WritePWD(int maxLen)
        {
            char[] pwd = new char[maxLen];    //密码
            const char start = '-'; //输入前的字符
            const char end = '*';   //输入后的字符
            //保存初始光标位置
            int startLeftPos = Console.CursorLeft;
            int startTopPos = Console.CursorTop;
            //保存初始前景色、背景色
            ConsoleColor SF = Console.ForegroundColor;
            ConsoleColor SBG = Console.BackgroundColor;
            //保存光标高度
            int CH = Console.CursorSize;
            //设置密码前景、背景色
            Console.ForegroundColor = ConsoleColor.Black;
            Console.BackgroundColor = ConsoleColor.DarkGray;
            //设置光标高度
            Console.CursorSize = 100;

            for (int i = 0; i < maxLen; i )
            {
                Console.Write(start);
            }
            Console.SetCursorPosition(startLeftPos, startTopPos);
            for (int i = 0; i < maxLen; i )
            {
                //设置光标位置
                Console.CursorTop = startTopPos;
                Console.CursorLeft = startLeftPos i;
                //接收用户输入的一个键
                ConsoleKeyInfo input = Console.ReadKey(true);
                //按Enter或Tab键
                if (input.Key.Equals(ConsoleKey.Enter) || input.Key.Equals(ConsoleKey.Tab))
                    break;

                //按Backspace键
                if (input.Key.Equals(ConsoleKey.Backspace))
                {
                    if (i > 0)
                    {
                        //删掉最后1位
                        pwd[i - 1] = '\u0000';
                        //光标左移
                        Console.CursorLeft--;
                        Console.Write(start);
                        i--;
                    }
                    i--;    //密码位数减一
                    continue;
                }
                if ((int)input.KeyChar == 0)    //输入不符合要求的字符
                {
                    i--;    //密码位数减一
                    Console.WriteLine("\u0007");
                }
                else   //输入符合要求的字符
                {
                    //把输入内容连接在密码后面
                    pwd[i] = input.KeyChar;
                    Console.Write(end);
                }
            }
            //恢复前景、背景色
            Console.ForegroundColor = SF;
            Console.BackgroundColor = SBG; ;
            //恢复光标高度
            Console.CursorSize = CH;
            return pwd;
        }
        #endregion

        #region 输入方向键移动光标
        /// <summary>
        /// 输入方向键移动光标
        /// </summary>
        /// <param name="x">允许光标存在的区域离控制台左端的距离</param>
        /// <param name="y">允许光标存在的区域离控制台上端的距离</param>
        /// <param name="width">允许光标存在的区域宽度</param>
        /// <param name="height">允许光标存在的区域高度</param>
        public void MoveCursor(int x,int y,int width,int height)
        {
            //获取当前光标位置
            int Px = Console.CursorLeft;
            int Py = Console.CursorTop;

            //如果光标不在指定区域就将光标移动到指定区域的中间
            if(Px<x || Px > x width || Py < y || Py > y height)
            {
                Px = x width / 2;
                Py = y height / 2;
                Console.CursorLeft = Px;
                Console.CursorTop = Py;
            }

            //输入方向键移动光标位置
            ConsoleKeyInfo k = new ConsoleKeyInfo();    //用户输入的键
            while(true)
            {
                //获取当前光标位置
                Px = Console.CursorLeft;
                Py = Console.CursorTop;

                k = Console.ReadKey(true);  //用户输入一个键,输入后不可见

                //用户输入向上方向键
                if (k.Key == ConsoleKey.UpArrow)
                {
                    Console.CursorTop = (Py - y height - 1) % height y;
                    continue;
                }
                //用户输入向下方向键
                if (k.Key == ConsoleKey.DownArrow)
                {
                    Console.CursorTop = (Py - y height 1) % height y;
                    continue;
                }
                //用户输入向左方向键
                if (k.Key == ConsoleKey.LeftArrow)
                {
                    Console.CursorLeft = (Px - x width - 1) % width x;
                    continue;
                }
                //用户输入向右方向键
                if (k.Key == ConsoleKey.RightArrow)
                {
                    Console.CursorLeft = (Px - x width 1) % width x;
                    continue;
                }
                //输入backspace键
                if (k.Key == ConsoleKey.Backspace)
                {
                    Console.Write(" ");
                    Console.CursorLeft = (Px - x width - 1) % width x;
                    continue;
                }
                //输入enter键
                if (k.Key == ConsoleKey.Enter)
                {
                    Console.CursorLeft = 0;
                    Console.CursorTop = (Py - y height 1) % height y;
                    continue;
                }

                Console.Write(k.KeyChar);
            }
        }
        #endregion

        #region 显示水平线
        /// <summary>
        /// 显示水平线
        /// </summary>
        /// <param name="color">水平线颜色</param>
        public static void hr(ConsoleColor color)
        {
            //记录前景色
            ConsoleColor F0 = Console.ForegroundColor;
            //设置水平线前景色
            Console.ForegroundColor = color;

            Console.WriteLine();
            //显示水平线
            for (int i = 0; i < Console.WindowWidth; i )
                Console.Write('~');
            //恢复前景色
            Console.ForegroundColor = F0;
        }
        #endregion

    }

标签: 控制 控制台

实例下载地址

C# 美化控制台 实例源码下载

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警