Start preparing to learn C, novice Oh ~ ~
Today, a console program, the cursor can be arbitrarily moved under the DOS ~ ~
First put a picture, but very ugly, we can understand, haha, is
I Love you.
Code comments have, in fact, a lot of things I have copied some of the code from other places, after all, is a novice, or need to multi-network of some great God learning.
The principle is to introduce windows.h, the first thing to get input, and then by judging:
1. Arrow key: Perform the move function up or down
2: Enter: The function of line wrapping is performed.
3. Common key: Input function.
The end point is to get the coordinates on the screen, when the arrow key is pressed, the coordinate value of +1, or minus one, thus realizing the free movement of the cursor.
Put the code below.
#include <stdio.h> #include <windows.h> #include <conio.h>handle hout;//get input char getinput () {int ch; /input string coord coord; The coordinates on the screen console_screen_buffer_info CSBI; Console screen buffer information coord. X=10;coord. Y=10;ch=getch ();//0x0d indicates a carriage return, 0xE0 represents the key of the next or next key while (ch==0xe0| | ch==0x0d) {getconsolescreenbufferinfo (HOUT,&CSBI); coord. x=csbi.dwcursorposition.x; Get the value of coordinates x COORD.Y=CSBI.DWCURSORPOSITION.Y; Get the value of the coordinate Y//if it is carriage return if (ch==0x0d) {coord. X=0;coord. y++; SetConsoleCursorPosition (Hout,coord); break;} Ch=getch ();//Upper if (ch==0x48) {if (coord. y!=0) coord. y--;} Under Else if (ch==0x50) {coord. y++;} Left else if (ch==0x4b) {if (coord. x!=0) {coord. x--;}} Right else if (ch==0x4d) {if (coord. x!=79) coord. X++;else{coord. X=0;coord. y++;}} SetConsoleCursorPosition (Hout,coord); Ch=getch ();} return ch;} int main () {char ch;hout=getstdhandle (std_output_handle);//gets input from the keyboard, if the direction key is the direction function, if the return is a newline, if the character is output while (1) {ch= GetInput ();p rintf ("%c", ch); GetChar ();}
The
C language implements the cursor movement in the console arbitrarily