65.9K
CodeProject is changing. Read more.
Home

Basic Graphics For Your Console Applications

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.80/5 (33 votes)

Feb 2, 2007

CPOL

3 min read

viewsIcon

101648

downloadIcon

5187

An article and Demo Project for basic graphics in console applications.

Introduction

By using the f_graph.h header file you can create new visual affects for your console window.

Background

Generally, students studying at some colleges work with console applications and learn the basics of programming by developing these kind of programs. I have written this header file for whom, who need some basic graphic functions such as, clrscr() especially for DevC++ users or additional functions like box() which creates a box on the console window.

Using the code

Now let's look at the member functions in f_graph.h header file and explain how to use them.

setCursor(CursorMode);

CursorMode's type is enumeration and its members are CURSOR_OFF, CURSOR_ON and CURSOR_BIG. While CursorMode is CURSOR_OFF, no cursor shown on the screen. Because as we see below, its visibility is set false in the function.

cursorInfo.bVisible  = FALSE;

While mode is CURSOR_ON, we see the default cursor on the screen. 10% height and visibility on.

cursorInfo.dwSize  = 10; 
cursorInfo.bVisible  = TRUE;

Respectively, getDimensionX() gets the current length of x dimension, and getDimensionY() gets y's. The whereX() function returns the cursor position on x axis, and whereY() returns y's. goBeginPixels(), sets our cursor to the position (1,1). And goRight(), goLeft(), goUp(), goDown() functions sets our cursor to the given positions.

clrscr() clears the console window. Especially sets the colors with default ones (white text on black background).

gotoxy(int,int), as we know, sets our cursor to the given position.

setTitle(const char*), is used to change console window's title.

wait(int), in fact this function is using default Sleep() function, but I think, wait is much suitable to be meaningful.

setColor(WORD), as we see, we can change the colors of our window screen, and also WORD can be, FOREGROUND_BLUE, FOREGROUND_GREEN, FOREGROUND_RED, FOREGROUND_INTENSITY, BACKGROUND_BLUE, BACKGROUND_GREEN, BACKGROUND_RED or BACKGROUND_INTENSITY. For example, we can make shining green color by writing the code below.

gf.setColor(FOREGROUND_GREEN  || FOREGROUND_INTENSITY);

Next, as I said at the beginning, there are some additional functions, such as box or some other functions that uses all eboves and works as a member for our template shape (makeSkeleton(WORD)) such as headerLine(char *). Ok,it is time to explain them.

First, box(int,int,int,int), as I mentioned before, this function makes a box with the given directions.

Then, clearArea(int,int,int,int), clears the area within those directions. In fact, it fills with space character in those area.

showTextInBox(char *,bool,bool ,int ,int ,int ,int ), with this function, you can write a text within a box that you determine the size. If size of that box comes enough for text, all shown in the box, but, if not, for the remain part, there will be created a new box with the same size with a stream that you control. Also you can specify, if the text will be a new beginning or not.

The functions below were written for our template shape.

makeSkeleton(const WORD), makes our template shape with the given color.

showLeftSide(char *,bool), this function shows the text given, in the box (small one) that positioned on the left side. Also you can specify, if the text will be a new beginning or not.

showRightSide(char *,bool,bool), this function shows the text given, in the box (big one) that positioned on the right side. And you can specify, if the text will be a new beginning or not. Moreover, you can control the page stream.

headerLine(char *), shows the text given in the box on the top. Also, the text is positioned to the center automatically.

footerLine(char *), shows the text given in the box on the bottom. You can use this for taking information from the user.

rightBottomText(char *,const WORD), with this function, you can write some text to outside of the boxes for alerting people. Also, this function positions the text to the right side.

blink(char *,const WORD), this function makes blinked texts by using given text.

warning(char *), makes given text blinked red at the bottom of the outside.

message(char *), makes given text blinked blue at the bottom of the outside.