如何绘制在控制台应用程序中的矩形? [英] How to draw a rectangle in console application?

查看:113
本文介绍了如何绘制在控制台应用程序中的矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要画一个矩形,拥有一批里面,在C#控制台应用程序,并使用ASCII扩展。我该如何去做?

这是一个演示。

感谢。

解决方案

 公共类ConsoleRectangle
{
    私人诠释hWidth;
    私人诠释hHieght;
    私人点hLocation;
    私人ConsoleColor hBorderColor;

    公共ConsoleRectangle(INT宽度,诠释hieght,点位置,ConsoleColor BORDERCOLOR)
    {
        hWidth =宽度;
        hHieght = hieght;
        hLocation =位置;
        hBorderColor = BORDERCOLOR;
    }

    公共点位置
    {
        {返回hLocation; }
        集合{hLocation =价值; }
    }

    公众诠释宽度
    {
        {返回hWidth; }
        集合{hWidth =价值; }
    }

    公众诠释Hieght
    {
        {返回hHieght; }
        集合{hHieght =价值; }
    }

    公共ConsoleColor的BorderColor
    {
        {返回hBorderColor; }
        集合{hBorderColor =价值; }
    }

    公共无效Darw()
    {
        字符串s =╔;
        字符串空间=;
        字符串TEMP =;
        的for(int i = 0; I<宽度;我++)
        {
            空间+ =;
            S + =═;
        }

        对于(INT J = 0; J< Location.X; J ++)
            温度+ =;

        S + =╗+\ N的;

        的for(int i = 0; I< Hieght;我++)
            S + =温度+║+空格+║+\ N的;

        S + =温度+╚;
        的for(int i = 0; I<宽度;我++)
            S + =═;

        S + =╝+\ N的;

        Console.ForegroundColor =边框颜色;
        Console.CursorTop = hLocation.Y;
        Console.CursorLeft = hLocation.X;
        Console.Write(多个);
        Console.ResetColor();
    }
}
 

I need to draw a rectangle, with a number inside, in a C# console app and using extended ASCII. How do I go about it?

This is for a demo.

Thanks.

解决方案

public class ConsoleRectangle
{
    private int hWidth;
    private int hHieght;
    private Point hLocation;
    private ConsoleColor hBorderColor;

    public ConsoleRectangle(int width, int hieght, Point location, ConsoleColor borderColor)
    {
        hWidth = width;
        hHieght = hieght;
        hLocation = location;
        hBorderColor = borderColor;
    }

    public Point Location
    {
        get { return hLocation; }
        set { hLocation = value; }
    }

    public int Width
    {
        get { return hWidth; }
        set { hWidth = value; }
    }

    public int Hieght
    {
        get { return hHieght; }
        set { hHieght = value; }
    }

    public ConsoleColor BorderColor
    {
        get { return hBorderColor; }
        set { hBorderColor = value; }
    }

    public void Darw()
    {
        string s = "╔";
        string space = "";
        string temp = "";
        for (int i = 0; i < Width; i++)
        {
            space += " ";
            s += "═";
        }

        for (int j = 0; j < Location.X ; j++)
            temp += " ";

        s += "╗" + "\n";

        for (int i = 0; i < Hieght; i++)
            s += temp + "║" + space + "║" + "\n";

        s += temp + "╚";
        for (int i = 0; i < Width; i++)
            s += "═";

        s += "╝" + "\n";

        Console.ForegroundColor = BorderColor;
        Console.CursorTop = hLocation.Y;
        Console.CursorLeft = hLocation.X;
        Console.Write(s);
        Console.ResetColor();
    }
}

这篇关于如何绘制在控制台应用程序中的矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆