RAD Studio 10.4.1 is now available! Learn more. Looking for discounts? Visit our Special Offers page!
C++

Getting Input In Graphics Mode

Author: Embarcadero USA

 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: %s\n", 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


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Learn More About Upgrading

Related posts
News

Using C++17 Algorithms Library Parallel Sorting with C++Builder 10.4 Sydney for Win32 and Win64

C++News

C++Builder and Platforms Support

C++

What's New in the GetIt Package Manager - June 2020

C++

RAD Studio 10.4 Now Available, Learn More

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES