Getting Input In Graphics Mode

by Oct 19, 1993

 Technical Information Database

TI676C.txt   Getting Input In Graphics Mode                    
Category   :General
Platform    :All
Product    :Borland C++  3.0    

Description:
  This program demonstrates how to get input from the user in
  graphics mode, echoed in the current colors and font size and
  font style.
  Functions:
       newLine() advances the (graphic) text position to the next
       line.
       getGrString(): echoes graphically the user input and stores
       it in a buffer.
       doCursor(): a helper function for getGrString, to handle the
       cursor.
       main(): the use of getGrString is demonstrated for string
       and numeric input.
  NOTE:  Although it is believed that this software is fully
  functional as described in the comments, no guarantees are made,
  express or implied.
  /* ....................................................... */
  #define ON 1
  #define OFF 0
  #include 
  #include 
  #include 
  #include 
  void doCursor(int);
  void newLine();
  void getGrString(char *);
  int main(void)
  {
     char nameString[80],ageString[80];
     int age;
     /* request auto detection */
     int gdriver = DETECT, gmode, errorcode;
     /* initialize graphics and local variables */
     initgraph(&gdriver, &gmode, "");
     /* read result of initialization */
     errorcode = graphresult();
     if (errorcode != grOk)  /* an error occurred */
     {
        printf("Graphics error: %sn", grapherrormsg(errorcode));
        printf("Press any key to halt:");
        getch();
        return(1); /* terminate with an error code */
     }
     /* use some colors to show that getGrString() handles
        foreground and background colors successfully. */
     setbkcolor(BLUE);
     setcolor(YELLOW);
     /* left-to-right gothic font, user-sizeable */
     /* change this as you like, except getGrString assumes
        left-to-right text direction! */
     settextstyle(GOTHIC_FONT,HORIZ_DIR,0);
     /* get a reasonable screen position */
     moveto(0,0);
     outtext("Your name please? ");
     getGrString(nameString);
     newLine();
     /* just to demonstrate that you can get numeric input from a
        string! */
     outtext("Your age please? ");
     getGrString(ageString);
     /* note: if atoi() returns 0, the string may not have been a
        valid number!  A real program should check for this. */
     age=atoi(ageString);
     newLine();
     outtext("Name: ");
     outtext(nameString);
     /* increment age to work with it as a number */
     ++age;
     /* make it a string again */
     sprintf(ageString,"%d",age);
     newLine();
     outtext("Next year, you will be ");
     outtext(ageString);
     newLine();
     outtext("Press key to exit! ");
     getch();
     closegraph();
     return 0;
  }
  /* newLine: primitive yet serviceable routine for a new text line
              in graphics mode */
  void newLine()
  {
      moveto(0,gety()+textheight("A"));
  }
  /* getGrString: takes a parameter of an input buffer, echoes
       characters typed, and fills input buffer.
       Function returns upon  or .
       Function responds appropriately to backspace.
       No provision is made to guard against overflow of
       the buffer or going over the right screen border. */
  void getGrString(char *inputString)
  {
     /* stringIndex is the current place in the string, so that we
        may build it as we go along getting input characters */
     int stringIndex=0;
     /* xVal will store the screen position for each char as we go
        along, so that we can erase and move the cursor
        successfully during backspacing */
     int xVal[255];
     /* inputChar: the character typed;  outString: the string
        version of that character */
     char inputChar, outString[2];
     /* oldColor saves the previous color value, to restore after
        erasing */
     int oldColor;
     /* outString is just one char + a null-terminator */
     outString[1]=0;
     /* screen starting position for input char string */
     xVal[0]=getx();
     do
     {
        /* turn on the cursor */
        doCursor(ON);
        /* get a single character, in no-echo mode */
        inputChar=getch();
        /* turn off the cursor before we write a new character */
        doCursor(OFF);
        /* avoid dealing with all special keys */
        if (inputChar==0) getch();
        else
        {
            if (inputChar==8) { /* backspace */
                /* save old character color */
                oldColor=getcolor();
                /* back up in the string */
                --stringIndex;
                /* no backing up past beginning of string! */
                if (stringIndex

Article originally contributed by Borland Staff