home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
progc
/
c_all592.arj
/
TI862.ASC
< prev
next >
Wrap
Text File
|
1992-02-25
|
8KB
|
331 lines
PRODUCT : Borland C++ NUMBER : 862
VERSION : 3.0
OS : DOS
DATE : February 25, 1992 PAGE : 1/5
TITLE : Getting Input In Graphics Mode
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 <graphics.h>
#include <stdio.h>
#include <conio.h>
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, "");
PRODUCT : Borland C++ NUMBER : 862
VERSION : 3.0
OS : DOS
DATE : February 25, 1992 PAGE : 2/5
TITLE : Getting Input In Graphics Mode
/* 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);
PRODUCT : Borland C++ NUMBER : 862
VERSION : 3.0
OS : DOS
DATE : February 25, 1992 PAGE : 3/5
TITLE : Getting Input In Graphics Mode
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 <ENTER> or <LINEFEED>.
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
PRODUCT : Borland C++ NUMBER : 862
VERSION : 3.0
OS : DOS
DATE : February 25, 1992 PAGE : 4/5
TITLE : Getting Input In Graphics Mode
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<0) stringIndex=0;
/* move to (old horz position, current vert
position) */
moveto(xVal[stringIndex],gety());
/* erasing consists of rewriting the old character
in the background color */
setcolor(getbkcolor());
outString[0]=inputString[stringIndex];
outtext(outString);
/* correct the current screen position since it
will have advanced after writing outString */
moveto(xVal[stringIndex],gety());
/* restore the text color we had */
setcolor(oldColor);
}
else { /* put a character into the string and draw it
on screen */
PRODUCT : Borland C++ NUMBER : 862
VERSION : 3.0
OS : DOS
DATE : February 25, 1992 PAGE : 5/5
TITLE : Getting Input In Graphics Mode
/* stuff the input into the string */
inputString[stringIndex]=inputChar;
/* draw the character on screen, as a string
(since that's what outttext() needs) */
outString[0]=inputChar;
outtext(outString);
/* proceed to next char in the string */
++stringIndex;
/* save horz position for possible backspacing */
xVal[stringIndex]=getx();
}
}
/* end getting characters on ENTER or LF */
} while(inputChar!=13 && inputChar!=10);
/* null-terminate input string before returning */
inputString[stringIndex]=0;
}
/* doCursor: draw or undraw the cursor, depending on whether the
parameter
is non-zero (ON) or zero (OFF)
*/
void doCursor(int on) {
int curX,oldColor;
/* we'll use an underbar as a cursor */
char uBarStr[2] = { '_',0 };
/* if cursor goes OFF, erase by drawing w/bkground color */
if (!on) {
oldColor=getcolor();
setcolor(getbkcolor());
}
/* save horizontal position before drawing cursor */
curX=getx();
outtext(uBarStr);
moveto(curX,gety());
/* if we changed the color to erase cursor, change it back */
if (!on) setcolor(oldColor);
}