13.5 – Defining active window – Turbo C

by subbu on January 11, 2014

How to define an active window?

By default the total window of 25 rows and 80 columns is considered as an active window. In previous sessions, we have used clrscr() to clear the output but, it was clearing the total screen.

What if, a portion of screen has to set with a background color, textcolor or to clear? The answer is to define an active window using a function window(). It is defined in “conio.h”, accepts four arguments x1, y1, x2 and y2. Where (x1, y1) are the coordinates of top-left corner and (x2, y2) are the coordinates of bottom-down corner. Say for example

window(10,10,60,20);

defines an active window between (10,10) and (60,20)

clrscr();

clears only the active window

Some of the functions defined in “conio.h” like cprintf(), cscanf(), cputs(), putch(), clrscr() and gotoxy() works only on active window.

#include<stdio.h>
#include<conio.h>
int main()
{
 int i;
 clrscr();
 textcolor(YELLOW);
 for(i=1;i<=200;i++)
  cprintf("Hello World");
 window(20,5,60,20);   /* defining active window              */
 clrscr();                      /* clearing only active window         */
 textcolor(MAGENTA);   /* setting text color to active window */
 gotoxy(1,7);               /* moves the cursor relative to active window */
 for(i=1;i<=10;i++)
  cprintf("Coding Fox"); /* prints only in active window     */
 getch();
 return 0;
}

Output:

overlapping window

In the above example the first loop prints “Hello World” on default active window. The window() function define an active windows between (20,5) and (60,20).
The clrscr() functions clears the active window
The gotoxy() sends the control to (1,7) which is relative to the current active screen
The second loop prints the “Coding Fox” in the current active window

Overlapping a window on another window

The function window() is generally used to overlap a window on another window. Now we will go through a practical example. Say for example, it is required to retain the information and erase the question before showing the answer. The following example shows the common information at the top permanently, only question is erased and the answer is displayed in place of question.

#include<stdio.h>
#include<conio.h>
void printrect(int,int,int,int);
int main()
{
 int sal,p_hra,p_da;
 float hra,da,gross;
 clrscr();
 /* printing heading box */
 textcolor(YELLOW);
 printrect(20,2,60,4);
 gotoxy(26,3);
 /* printing heading text */
 textcolor(WHITE);
 cprintf("EMPLOYEE PAY SLIP GENERATION");
 /* printing data entry box */
 window(15,6,65,15);
 textcolor(MAGENTA);
 printrect(1,1,50,7);
 /* Accepting data */
 textcolor(WHITE);
 gotoxy(17,2);
 cprintf("INPUT SCREEN");
 gotoxy(2,3);
 cprintf("Salary     :");cscanf("%d",&sal);
 gotoxy(2,4);
 cprintf("HRA Percent:");cscanf("%d",&p_hra);
 gotoxy(2,5);
 cprintf("DA Percent :");cscanf("%d",&p_da);
 getch();
 /* overlapping output window */
 window(18,9,68,18);
 clrscr();
 textcolor(BLUE);
 printrect(1,1,50,7);
 textcolor(WHITE);
 gotoxy(17,2);
 /* printing the output */
 cprintf("OUTPUT SCREEN");
 gotoxy(2,3);
 cprintf("Salary : %d",sal);
 gotoxy(2,4);
 hra=(float)sal*p_hra/100;
 cprintf("House Rent Allowence : %f",hra);
 gotoxy(2,5);
 da=(float)sal*p_da/100;
 cprintf("Dearness Allowences  : %f",da);
 gotoxy(2,6);
 gross=sal+hra+da;
 cprintf("Gross Salary         : %f",gross);
 getch();
 return 0;
}
/* function to print rectangle */
void printrect(int x1,int y1,int x2,int y2)
{
 int i;
 gotoxy(x1,y1);
 cprintf("%c",218); /* to print top-left corner */
 gotoxy(x2,y1);
 cprintf("%c",191); /* to print top-right corner */
 gotoxy(x1,y2);
 cprintf("%c",192); /* to print bottom-left corner */
 gotoxy(x2,y2);
 cprintf("%c",217); /* to print bottom-right corner */
 for(i=x1+1;i<x2;i++) /* printing top horizontal line */
 {
  gotoxy(i,y1);
  cprintf("%c",196);
 }
 for(i=x1+1;i<x2;i++) /* printing bottom horizontal line */
 {
  gotoxy(i,y2);
  cprintf("%c",196);
 }
 for(i=y1+1;i<y2;i++) /* printing left virtical line */
 {
  gotoxy(x1,i);
  cprintf("%c",179);
 }
 for(i=y1+1;i<y2;i++) /* printing right virtical line */
 {
  gotoxy(x2,i);
  cprintf("%c",179);
 }
}

Output:

input window

output window

Previous post:

Next post: