Graphics in C/C++: Some More Interesting Functions

In this tutorial, we are going to learn about some of the interesting functions that are used for graphics in C/C++ programming language.
Submitted by Mahima Rao, on October 24, 2018

In this Advance Learning Tutorial of C / C ++ today, we are going to tell you about some of the functions that can be used to make the program more attractive. This works on both text and graphics modes. That is why knowing these functions before starting the graphics programming is important and also useful.

1) BLINK

We will talk about an interesting character. You must have heard about the word 'BLINKING'. If you also want to blink any character on your screen, it is possible in C / C++ as well.

A constant word 'BLINK' is defined in C / C ++. With the help of which you can blink any character on the screen. The value of BLINK constant is 128. Here is given an example which shows the usage of 'BLINK'.

    textcolor( BLUE + BLINK );
    OR
    textcolor( Blue + 128 );  

Keep in mind that the BLINK character is used only with textcolor and textbackground functions on TEXT MODE. So writing only BLINK will not make any difference. You can also write value 128 at the place of BLINK. This works in both ways.

See the program below and try it out...

#include <conio.h> 

void main() {
	clrscr();
	 
	textcolor(BLUE + BLINK );
	 
	cprintf("Welcome to the Tutorial of Graphics\n");
	 
	getch(); 
}

After running this program you will see the ' Welcome In Advance Learning Tutorial' line in the output.

2) GOTOXY

Now let's talk about a function with which you can move the cursor to any position on the screen. It is better to understand this function properly as it is useful for creating a program in graphics. The name of this function is gotoxy. Its declaration is given below,

    void gotoxy(int x , int y); 

This function takes two parameters x, y whatever value you give in x and y, the function will take the cursor to that position. Keep in mind that if you pass an invalid coordinate, then using this function will not make any sense because the compiler will ignore it.

See the example below to see how to use the function:

Note: Do not forget to include Header File <conio.h> before using this function.

#include <conio.h> 
#include <stdio.h>
 
void main() {
	 
	clrscr();
	 
	gotoxy(20,12);
	 
	printf("Welcome to the Tutorial of Graphics\n");
	 
	getch(); 
}

Keep in mind that in this example we use printf function instead of cprintf. This is why stdio.h header file has been used. gotoxy works with both printf and cprintf.

3) DELAY

Now we will be telling you about another important function, which delay(), with the help of which you can suspend the execution of the program for some time. You must be wondering how long will the execution of the program be suspended?

Well, it depends on the value passed in the parameter of this function. Delay function takes value in the millisecond. That is, if you want to suspend the program for 5 seconds then the function call will be as follows:

    delay (5000); 

After writing this line, if any line is written below, it will be executed after 5 seconds because the execution of the program will be suspended for 5 seconds.

Note: You must include the header file (DOS.H) before using the function. For example, see the program given below.

#include <conio.h> 
#include <stdio.h>
#include <dos.h>
 
void main() {
 
	clrscr();
	 
	printf("Welcome\n");
	 
	delay(5000);
	 
	printf("You are learning Graphics in C/C++\n");
	 
	getch(); 
}

After running the program, first, you will see Welcome in the output, after 5 seconds, the next line will be You are learning Graphics in C/C++.

If you have any problems understanding the basic topics of C/C ++ such as printf, header file and function etc, then you can read these basic topics from our website too.

That's all for today in Advance Learning Tutorial. If you have any problem in any of the topics mentioned above, then you can ask your questions in the comments section below.





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.