Software & Finance





Visual C++ Draw Text Mode Tables / Grid Using ASCII characters





Drawing Tables or Grids in Graphics might be easier. How about drawing tables using ASCII characters in Text Mode? Here is the simple code where you can use to draw perfect grid with number of columns and rows.


Source Code


// VCPP_TextModeGrid.cpp : Defines the entry point for the console application.

//

 

#include <stdio.h>

#include <tchar.h>

#include <conio.h>

#include <iostream>

#include <windows.h>

 

 

void gotoxy(int x, int y)

{

    COORD ord;

    ord.X = x;

    ord.Y = y;

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);

}

 

void gotoxytext(int x, int y, const char *str)

{

    COORD ord;

    ord.X = x;

    ord.Y = y;

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);

    std::cout << str;

}

 

class CTextModeGrid

{

    int m_left, m_top, m_right, m_bottom;

    int m_numRows, m_numCols;

    int m_arrRowHeight[24], m_arrColWidth[24];

public:

    CTextModeGrid(int l, int t, int r, int b)

    {

        m_left = l;

        m_top = t;

        m_right = r;

        m_bottom = b;

    }

    void SetRows(int rs) { m_numRows = rs; }

    void SetCols(int cs) { m_numCols = cs; }

    void SetRowHeight(int r, int rh) { m_arrRowHeight[r] = rh; }

    void SetColWidth(int c, int cw) { m_arrColWidth[c] = cw; }

 

    void DrawGrid()

    {

        DrawSquare(m_left, m_top, m_right, m_bottom);

       

        // Draw Vertical Lines

        {

            int pos = m_left;

            for(int i = 0; i < m_numCols; i++)

            {

                pos += m_arrColWidth[i];

                if(pos > m_right)

                    pos = m_right;

 

                for(int j = m_top; j <= m_bottom; j++)

                {

                    gotoxy(pos, j);

                    std::cout << (char)(179);

                }

                if(pos == m_left)

                {

                    gotoxy(m_left,m_top);

                    std::cout << (char) (218);

                    gotoxy(m_left,m_bottom);

                    std::cout << (char) (192);

                }

                else if(pos == m_right)

                {

                    gotoxy(m_right,m_top);

                    std::cout << (char) (191);

                    gotoxy(m_right,m_bottom);

                    std::cout << (char) (217);

                }

                else

                {

                    gotoxy(pos, m_top);

                    std::cout << (char)(194);

 

                    gotoxy(pos, m_bottom);

                    std::cout << (char)(193);

                }

            }

        } 

 

        // Draw Horiz. Lines

        {

            int pos = m_top;

            for(int i = 0; i < m_numRows; i++)

            {

                pos += m_arrRowHeight[i];

                if(pos > m_bottom)

                    pos = m_bottom;

 

                for(int j = m_left; j <= m_right; j++)

                {

                    gotoxy(j, pos);

                    std::cout << (char)(196);

 

                    int xpos = m_left;

                    for(int k = 0; k < m_numCols; k++)

                    {

                        xpos += m_arrColWidth[k];

                        if( xpos == j)

                        {

                            gotoxy(xpos, pos);

                            if(pos == m_bottom)

                            {

                                std::cout << (char)(193);

                            }

                            else if(pos == m_top)

                            {

                                std::cout << (char)(194);

                            }

                            else

                            {

                                std::cout << (char)(197);

                            }

                        }

 

                    }

                }

                if(pos == m_top)

                {

                    gotoxy(m_left,m_top);

                    std::cout << (char) (218);

 

                    gotoxy(m_right,m_top);

                    std::cout << (char) (191);

                }

                else if(pos == m_bottom)

                {

                    gotoxy(m_left,m_bottom);

                    std::cout << (char) (192);

 

                    gotoxy(m_right,m_bottom);

                    std::cout << (char) (217);

                }

                else

                {

                    gotoxy(m_left, pos);

                    std::cout << (char)(195);

 

                    gotoxy(m_right, pos);

                    std::cout << (char)(180);

                }

            }

        }

    }

 

    void DrawSquare(int left, int top, int right, int bottom)

    {

        { // Horz. Lines       

            for(int i = left; i <= right; i++)

            {

                gotoxy(i,top);

                std::cout << (char) (196);

                gotoxy(i,bottom);

                std::cout << (char) (196);

            }

        }

        { // Vertical Lines

            for(int i = top; i <= bottom; i++)

            {

                gotoxy(left,i);

                std::cout << (char)(179);

                gotoxy(right,i);

                std::cout << (char)(179);

            }

        }

        gotoxy(left,top);

        std::cout << (char) (218);

 

        gotoxy(right,top);

        std::cout << (char) (191);

 

        gotoxy(left,bottom);

        std::cout << (char) (192);

 

        gotoxy(right,bottom);

        std::cout << (char) (217);

    }

 

};

 

int _tmain(int argc, _TCHAR* argv[])

{

    CTextModeGrid g1(1,1,70,21);

 

    g1.SetRows(3);

    g1.SetRowHeight(0, 4);

    g1.SetRowHeight(1, 14);

    g1.SetRowHeight(2, 3);

 

    g1.SetCols(5);

    g1.SetColWidth(0, 5);

    g1.SetColWidth(1, 10);

    g1.SetColWidth(2, 25);

    g1.SetColWidth(3, 13);

    g1.SetColWidth(4, 17);   

 

    g1.DrawGrid();

 

    gotoxytext(3,3,"No");

    gotoxytext(9,3,"Item");

    gotoxytext(22,3,"Description");

    gotoxytext(45,3,"Quantity");

    gotoxytext(61,3,"Price");

 

    gotoxytext(3,7,"1");

    gotoxytext(9,7,"P196");

    gotoxytext(21,7,"Samsung Color TV");

    gotoxytext(48,7,"1");

    gotoxytext(60,7,"$829.00");

   

    gotoxytext(3,8,"2");

    gotoxytext(9,8,"P020");

    gotoxytext(21,8,"Uniden Handset");

    gotoxytext(48,8,"1");

    gotoxytext(60,8,"$  29.00");

 

    gotoxytext(3,9,"3");

    gotoxytext(9,9,"P111");

    gotoxytext(21,9,"Folder Blank");

    gotoxytext(48,9,"1");

    gotoxytext(60,9,"$  2.70");

 

    gotoxy(1,23);

 

    getch();

 

    return 0;

}

 

Click here to get the Visual C++ source code and EXE application

 

 

 

 

Output