setbkcolor


Syntax

#include <graphics.h>

void setbkcolor(int color);

Description
setbkcolor sets the background to the color specified by color. The argument color can be a name or a number as listed below. (These symbolic names are defined in graphics.h.)
NameValue
BLACK 0
BLUE1
GREEN2
CYAN3
RED4
MAGENTA5
BROWN6
LIGHTGRAY7
DARKGRAY8
LIGHTBLUE9
LIGHTGREEN10
LIGHTCYAN11
LIGHTRED12
LIGHTMAGENTA13
YELLOW14
WHITE15
For example, if you want to set the background color to blue, you can call

   setbkcolor(BLUE) /* or */ setbkcolor(1)

On CGA and EGA systems, setbkcolor changes the background color by changing the first entry in the palette.

If you use an EGA or a VGA, and you change the palette colors with setpalette or setallpalette, the defined symbolic constants might not give you the correct color. This is because the parameter to setbkcolor indicates the entry number in the current palette rather than a specific color (unless the parameter passed is 0, which always sets the background color to black).

Return Value
None.

Windows Notes [WIN]
The winbgim version allows the color argument to be an ordinary BGI color (from 0 to 15) or an RGB color. Also, only future drawing will use the new background color (anything currently drawn in the old background color will stay in the old color). Calling setbkcolor(0) will change the background color to the current color at index [0] of the palette (rather than always changing the background to black).

See also
getbkcolor
setallpalette
setcolor
setpalette

Example

/* setbkcolor example */ 



#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>



int main(void)

{

   /* _select driver and mode that supports multiple background colors*/

   int gdriver = EGA, gmode = EGAHI, errorcode;

   int bkcol, maxcolor, x, y;

   char msg[80];



   /* initialize graphics and local variables */

   initgraph(&gdriver, &gmode, "");



   /* read result of initialization */

   errorcode = graphresult();

   if (errorcode != grOk) {   /* an error occurred */



      printf("Graphics error: %s\n", grapherrormsg(errorcode));

      printf("Press any key to halt:");

      getch();

      exit(1);               /* terminate with an error code */

   }



   /* maximum color index supported */

   maxcolor = getmaxcolor();



   /* for centering text messages */

   settextjustify(CENTER_TEXT, CENTER_TEXT);

   x = getmaxx() / 2;

   y = getmaxy() / 2;



   /* loop through the available colors */

   for (bkcol=0; bkcol<=maxcolor; bkcol++) {





      /* clear the screen */

      cleardevice();



      /* select a new background color */

      setbkcolor(bkcol);



      /* output a messsage */

      if (bkcol == WHITE)

         setcolor(EGA_BLUE);

      sprintf(msg, "Background color: %d", bkcol);

      outtextxy(x, y, msg);

      getch();

   }



   /* clean up */

   closegraph();

   return 0;

}


Back to index