How to draw a rectangle in console application?
Asked Answered
G

5

6

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.

Gaskin answered 15/5, 2011 at 5:12 Comment(2)
possible duplicate of Console.Write() - display extended ascii chars?Twedy
C# doesn't use ASCII, extended or otherwise.Abduction
K
13
public class ConsoleRectangle
{
    private int hWidth;
    private int hHeight;
    private Point hLocation;
    private ConsoleColor hBorderColor;

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

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

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

    public int Height
    {
        get { return hHeight; }
        set { hHeight = value; }
    }

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

    public void Draw()
    {
        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 < Height; 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();
    }
}
Kalin answered 15/5, 2011 at 6:20 Comment(5)
How do you use it,if i need to draw number inside the rectangleGaskin
If you're drawing a box around the entire console area, decrease your width and height by two (because the first and last character in each dimension are not usable/available, being taken up by the corner characters).Staphylorrhaphy
is there any way to modify this so you can draw two rectangles (one inside the other) ? Because of the spaced the second rectangle overwrite the first rectangle left horizontal lineBlighter
If you want to copy/paste:╔ ═ ╗ ╚ ╝Mycenae
@Alex, you forgot Harrod
C
5

This is an extension method to String, which will draw a console box around a given string. Multi-line support included.

i.e. string tmp = "some value"; Console.Write(tmp.DrawInConsoleBox());

        public static string DrawInConsoleBox(this string s)
        {
            string ulCorner = "╔";
            string llCorner = "╚";
            string urCorner = "╗";
            string lrCorner = "╝";
            string vertical = "║";
            string horizontal = "═";

            string[] lines = s.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            

            int longest = 0;
            foreach(string line in lines)
            {
                if (line.Length > longest)
                    longest = line.Length;
            }
            int width = longest + 2; // 1 space on each side

            
            string h = string.Empty;
            for (int i = 0; i < width; i++)
                h += horizontal;

            // box top
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(ulCorner + h + urCorner);

            // box contents
            foreach (string line in lines)
            {
                double dblSpaces = (((double)width - (double)line.Length) / (double)2);
                int iSpaces = Convert.ToInt32(dblSpaces);

                if (dblSpaces > iSpaces) // not an even amount of chars
                {
                    iSpaces += 1; // round up to next whole number
                }

                string beginSpacing = "";
                string endSpacing = "";
                for (int i = 0; i < iSpaces; i++)
                {
                    beginSpacing += " ";

                    if (! (iSpaces > dblSpaces && i == iSpaces - 1)) // if there is an extra space somewhere, it should be in the beginning
                    {
                        endSpacing += " ";
                    }
                }
                // add the text line to the box
                sb.AppendLine(vertical + beginSpacing + line + endSpacing + vertical);
            }

            // box bottom
            sb.AppendLine(llCorner + h + lrCorner);

            // the finished box
            return sb.ToString();
        }

Output like this enter image description here

Chartreuse answered 16/3, 2017 at 0:1 Comment(0)
P
4

Like this?

This worked for me:

Console.OutputEncoding = Encoding.GetEncoding(866);
Console.WriteLine("┌─┐");
Console.WriteLine("│1│");
Console.WriteLine("└─┘");

[EDIT]

Answer to the sub-question in the comment:

Console.OutputEncoding = Encoding.GetEncoding(866);
Console.WriteLine("  ┌─┐");
Console.WriteLine("  │1│");
Console.WriteLine("┌─┼─┘");
Console.WriteLine("│1│");
Console.WriteLine("└─┘");
Perugia answered 15/5, 2011 at 5:32 Comment(4)
@Alex thanks for your example.That will definetely do the trick.Correct if I am wrong but what you did is not using ascii is it?Also what does "Encoding.GetEncoding(866) does?Gaskin
@Alex how did you draw the top of the rectangle?Gaskin
@Gaskin - Hold Alt key and type 218 on numeric keyboard. This will show ┌. Than you do the same with 196 and 191. Important - you need to use numeric keyboard (NumLock is On).Perugia
@Alex you have been very very helful .Can i ask you one last thing.Sorry.I have done the rectangle ,I need to draw another rectangle at the top right hand corner with a numnber inside.Any chance of help?thanksGaskin
B
2

You can use CsConsoleFormat† to draw with ASCII border symbols in console.

Drawing a number within a rectangle with "double" lines:

ConsoleRenderer.RenderDocument(
    new Document()
        .AddChildren(
            new Border {
                    Stroke = LineThickness.Wide,
                    Align = HorizontalAlignment.Left
                }
                .AddChildren(1337)
        )
);

You can change Stroke = LineThickness.Wide line to change the style of lines. LineThickness.Single would produce thin single lines, new LineThickness(LineWidth.Single, LineWidth.Wide) would produce single vertical and double horizontal lines.

Here's what it looks like:

You can also use ConsoleBuffer class to draw lines explicitly (argument names added for clarity):

using static System.ConsoleColor;

var buffer = new ConsoleBuffer(width: 6);
buffer.DrawHorizontalLine(x: 0, y: 0, width: 6, color: White);
buffer.DrawHorizontalLine(x: 0, y: 2, width: 6, color: White);
buffer.DrawVerticalLine(x: 0, y: 0, height: 3, color: White);
buffer.DrawVerticalLine(x: 5, y: 0, height: 3, color: White);
buffer.DrawString(x: 1, y: 1, color: White, text: "1337");
new ConsoleRenderTarget().Render(buffer);

† CsConsoleFormat was developed by me.

Bebebebeerine answered 1/3, 2018 at 22:31 Comment(0)
Z
1

Problem with above code is extra spaces, if you draw multiple rectangles, it causes mess. here is a code which draw rectangles recursively without extra spaces.

public class AsciDrawing
{
    public static void TestMain() {
        var w = Console.WindowWidth;
        var h = Console.WindowHeight;
        RecursiveDraw(16, 8, new Point(w/2-8, h/2-4), ConsoleColor.Black);
        Console.CursorTop = h;
        Console.CursorLeft = 0;
    }
    public static void RecursiveDraw(int Width, int Height, Point Location, ConsoleColor BorderColor) { 
        if(Width < 4 || Height < 2) return;
        Draw(Width, Height, Location, BorderColor); //Commnet this draw and and Uncomment Draw bellow to see the difference. 
        Thread.Sleep(500);
        //Comment or Uncomment to see how many recursive calls you want to make
        //The best is to comment all execpt 1 and then uncomment 1 by 1
        RecursiveDraw(Width/2, Height/2, new Point(Location.X-Width/4, Location.Y-Height/4), ConsoleColor.Green); //Left Top
        RecursiveDraw(Width / 2, Height / 2, new Point(Location.X + 3* Width / 4, Location.Y + 3* Height / 4), ConsoleColor.Red); //Right Bottom
        RecursiveDraw(Width / 2, Height / 2, new Point(Location.X + 3* Width / 4, Location.Y - Height / 4), ConsoleColor.Blue); //Right Top
        RecursiveDraw(Width / 2, Height / 2, new Point(Location.X - Width / 4, Location.Y + 3* Height / 4), ConsoleColor.DarkMagenta); // Left Bottom
        
        //Draw(Width, Height, Location, BorderColor);

    }
    public static void Draw(int Width, int Height, Point Location, ConsoleColor BorderColor)
    {
        Console.ForegroundColor = BorderColor;

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

        s += "╗" + "\n";
        
        Console.CursorTop = Location.Y;
        Console.CursorLeft = Location.X;
        Console.Write(s);



        for (int i = 0; i < Height; i++) {
            Console.CursorTop = Location.Y + 1 + i;
            Console.CursorLeft = Location.X;
            Console.WriteLine("║");
            Console.CursorTop = Location.Y + 1 + i;
            Console.CursorLeft = Location.X + Width+1;
            Console.WriteLine("║");
        }
           

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

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

        
        Console.CursorTop = Location.Y+Height;
        Console.CursorLeft = Location.X;
        Console.Write(s);
        Console.ResetColor();
    }
}

public record Point(int X, int Y);

Zerline answered 18/2, 2023 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.