How to Use settextstyle and outtextxy Function by C Language

settextstyle() and outtextxy() functions in the C or C++ programming language

Introduction :

Graphics programming in C or C++ language can be a fun and exciting way to create visual content including drawings, shapes and text on the computer screen. In graphics.h library of the Turbo C++ IDE has two important functions to handle text display are settextstyle() and outtextxy().

The settextstyle() and outtextxy() functions are used for displaying text in the screen or graphics window. These functions allow you to customize the appearance of text and display it at specific coordinates on the screen in graphics mode. In this article, I shall explore you how to use the settextstyle() and outtextxy() functions of graphics.h library of Turbo C++ IDE by C or C++ graphics programming language to display text on the screen.

The settextstyle() and outtextxy() functions:

The settextstyle() and outtextxy() functions are commonly used to set the text style (font, size, direction) and display text at specified coordinates on the screen or graphics window. By setting the text style and specifying coordinates for text display, you can create simple and visually appealing graphical user interfaces (GUI) applications.

These functions are generally used in graphics program to create text-based interfaces, labels or any other text-related elements in a GUI application. The settextstyle() and outtextxy() functions are for use in graphics mode. They will not work in text mode.

The settextstyle() function:

The settextstyle() function is typically used to set the text style (font, size, direction) for text that will be displayed on the screen. This function allows you to customize various text styles such as font, direction and size before displaying text on the screen or graphics window.

The syntax for the settextstyle() function :

void settextstyle(int text_font, int text_direction, int text_size);

The settextstyle() function typically takes three arguments. The first parameter or argument (text_font) specifies the font of the text. It is usually an integer constant representing a specific font such as DEFAULT_FONT, TRIPLEX_FONT, GOTHIC_FONT, SMALL_FONT and more.

The second parameter or argument (text_direction) specifies the text direction which can be either HORIZ_DIR for horizontal text (left to right) or VERT_DIR for vertical text (bottom to top). These constants control the orientation of the text. The default direction is HORIZ_DIR (left to right).

The last parameter or argument (text_size) sets the size of the font. This parameter ranges from 1 to 10 where 1 is the smallest and 10 is the largest font size.

The outtextxy() function :

The outtextxy() function is used to display text at specific coordinates on the screen or graphics window. It allows you to output text at a particular location on the screen. After setting the text style using settextstyle() function, you can use the outtextxy() function to display text at specific coordinates.

The syntax for the outtextxy() function :

void outtextxy(int x_coordinate, int y_coordinate, const char *text_string);

The outtextxy() function typically takes three arguments or parameters. The first argument or parameter (x_coordinate) specifies the horizontal position on the screen or graphics window where the text will be displayed. It represents the x coordinate.

The second argument or parameter (y_coordinate) specifies the vertical position on the screen or graphics window where the text will be displayed. It represents the y coordinate.

The last argument or parameter (text_string) specifies the text to be displayed on the screen.

usages of settextstyle() and outtextxy() functions :

The settextstyle() and outtextxy() functions in are commonly used in graphics programming for various purposes. You can use settextstyle() function to create custom font styles by various the font, direction, and size. This is useful when you want to create visually distinct text elements in your graphics program.

By adjusting the x and y coordinates of outtextxy() function, you can control the alignment of text on the screen or graphics window. In gaming program, you can use outtextxy() function to display the score of the player or other game-related information on the graphics screen.

In the data visualization program, you can use outtextxy() function to label data points, axes and other text in charts and graphs. Using settextstyle() and outtextxy() functions, you can create text animations like scrolling text, moving labels or animated headlines. You can also create buttons, labels, menus and other GUI elements by displaying text at specific positions on the graphics screen.

About the program :

The program is a simple animation where the text “puskarcoding.com” is displayed with different text styles and sizes to create an animation effect on the screen. When you run the program on your pc, you can see a rotating and changing text effect using different text styles and sizes on a graphics window.

Explanation of the program :

At the beginning, include necessary header files such as graphics.h, conio.h and dos.h in the program. For graphics functions, include graphics.h library. To use getch() and delay() functions include conio.h and dos.h library respectively.

In the main function, declare four integer variables such as “i”, “j”, “k” and “m”. The variable “i” and “j” are used in loops while “k” and “m” are used for positioning text on the screen. You have to also declare two integers variables “graphic_driver” and “graphic_mode”. Now you have to initialize the graphics system using the initgraph() function with the specified graphics driver and mode.

Inside the nested loops, set the text style using the settextstyle() function with the arguments for font, direction and size. After that, using outtextxy() function displays the text “puskarcoding.com” at the specified coordinates on the screen. To change the value of “i”, “j”, “k” and “m” variables, you can change the style, direction (horizontal and vertical) and position of text which displays on the screen.

The delay() function is used to introduce a delay (100 milliseconds) for controlling the speed of the animation. The getch() function use for a key press from the user before closing the graphics mode using closegraph() function.

How run the program :

At first, install the Turbo C++ IDE on your pc. After that, open the Turbo C++ IDE and create a C or C++ file with .c or .cpp extension. Now, copy the below code and paste in your C or C++ file of the Turbo C++ IDE. Do you know how to copy paste in the Turbo C++ IDE, if you do not know click here.

You can check my other post to know how to use graphics.h in Turbo C++ IDE. At last, run the program on your pc to see how use settextstyle() and outtextxy() functions using C or C++ programming language.

Code for settextstyle() and outtextxy() functions :

The following code is used for settextstyle() and outtextxy() functions in C or C++ graphics programming language.

/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
    int i, j = 0, k = 0, m = 0;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    for (i = 0; i < 2; i++)
    {
        for (j = 0; j <= 11; j++)
        {
            settextstyle(j, i, 1);
            outtextxy(k, m, "puskarcoding.com");
            m += 40;
            if (i == 1)
            {
                k += 40;
                m = 150;
            }
            delay(100);
        }
        k = 150;
        m = 150;
    }
    getch();
    closegraph();
    return 0;
}

Output :

After compile and run the program, you can see the output like below image.

output of settextstyle() and outtextxy() functions in C or C++

Conclusion :

After reading the above article, you have learned how to use the settextstyle() and outtextxy() functions in C or C++ graphics programming language. Now, you can create visually appealing and informative graphics program using C or C++ graphics programming language. Thank you for visiting my site.

Scroll to Top