C language implementation of the control console cursor move freely

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.