Software & Finance





Turbo C Graphics - getmaxx function





getmaxx() function is used to get the maximum x value of graphics screen, usually it is 639 for VGA driver and VGAHI graphics mode. getmaxy is used to get the maximum y value of graphics screen, usually it is 479 for VGA driver and VGAHI graphics mode.

 

These functions are always very useful to find out the bounding rectangle for our drawings. Loot at the sample code on how I am drawing a border using rectangle function with the help of getmaxx() and getmaxy() functions.

 

// draw a white color border with rectangle

rectangle(0,0,getmaxx(),getmaxy());

 

 

Back to Turbo C Graphics Index



Source Code


#include <graphics.h>

#include <stdio.h>

#include <math.h>

#include <conio.h>

#include <dos.h>

#include <stdlib.h>

 

void main()

{

 

       int i, grd, grm;

       int x, y, w;

       int left,top,right,bottom;

       int margin, width, height;

       int gresult;

       int cx, cy;

       char msg[512];

       char buffer[] = "softwareandfinance.com";

 

       // Detect the graphics driver and mode

       detectgraph(&grd,&grm);

       // initialize the graphics mode with initgraph

       initgraph(&grd, &grm, "");

 

       gresult = graphresult();

       if(gresult != grOk)

       {

              printf(grapherrormsg(gresult));

              getch();

              return;

       }

 

       // set the background color

       setbkcolor(RED);

       // set the foreground color

       setcolor(WHITE);

       // draw a white color border with rectangle

       rectangle(0,0,getmaxx(),getmaxy());

 

       // find the text width and text height

       width = textwidth(buffer);

       height = textheight(buffer);

       margin = 100; // 100 pixel margin

       left = margin;

       top = margin;

       // calculate the right and bottom with margin and getmaxx and getmaxy function

       bottom = getmaxy() - margin;

       right = getmaxx() - margin;

 

       // draw a bar with solid fill on blue color

       // use rectangle to have a white color border

       setfillstyle(SOLID_FILL, BLUE);

       bar(left, top, right, bottom);

       rectangle(left, top, right, bottom);;

 

       // change the text foreground color to yellow

       setcolor(YELLOW);

       cx = (right + left - width) / 2;

       cy = (bottom + top - height) / 2;

 

       settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);

       outtextxy(cx,cy,buffer);

 

       setcolor(WHITE);

       sprintf(msg, "background: %d, foreground: %d, maxcolors: %d", getbkcolor(), getcolor(), getmaxcolor());

       width = textwidth(msg);

       cx = (right + left - width) / 2;

       cy += textheight(msg) + 10;;

 

       // use moveto and outtext function instead of outtextxy

       moveto(cx, cy);

       outtext(msg);

 

       getch();

       closegraph();

}

 

 

Output