Cleaning only part of the console output

What I want to do:

The cursor first flashes in the upper left corner of the screen:

160 characters remaining _

When I press 'i':

159 characters remaining

I am

When I press 'a':

158 characters remaining

IA

When I press 'm':

157 characters remaining

RMKO

etc.

What needs to be done (for me):

  • Only the first three characters of the screen need to be cleared.
  • Refresh the newly pressed key on the screen

What I tried:

I tried to clear the entire screen and write everything that was there earlier on it.

Why am I not happy with what I did:

Because it gives a sharp look. And the record is not smooth.

What I need from you to help me:

Some built-in function or other way to clear only part of the screen.

My specifications:

Windows XP SP 3

IDE: Visual C ++ 2010 Express

+4
source share
3 answers

The first thing to understand is that C ++ has no concept of the screen as a standard part of the language. The standard output may be a file, the printer and cout do not know the difference.

The device screen itself is usually a little smarter and recognizes some commands. The most widely implemented of them are "\ r" - carriage return and "\ n" - line. "\ r" moves the cursor to the beginning of the line, and "\ n" moves to the next line, but this does not meet your needs, as you have already tried.

It seems that the only way forward is to use curses (of which ncurses is only one implementation, although standard on Linux). It presents you with a virtual screen with various commands for updating them. Then it accepts only the changed parts and optimizes the terminal.

This is just an example of a typical C program using ncurses, it might be worth a look:

#include <ncurses.h> int main() { int ch; initscr(); /* Start curses mode */ raw(); /* Line buffering disabled */ keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ noecho(); /* Don't echo() while we do getch */ printw("Type any character to see it in bold\n"); ch = getch(); /* If raw() hadn't been called * we have to press enter before it * gets to the program */ printw("The pressed key is "); attron(A_BOLD); printw("%c", ch); attroff(A_BOLD); refresh(); /* Print it on to the real screen */ getch(); /* Wait for user input */ endwin(); /* End curses mode */ return 0; } 

The printw () function is written to the "imaginary" screen. It places the material in the buffer and updates some flags, and also performs some other internal operations with ncurses. In fact, it does not write anything on your real screen (console window).

You can do as many printw () as you want, but the material is not displayed on the real screen until your program does something else to bring the "imaginary" contents of the screen buffer to the real screen.

One thing that causes the real screen to be updated from the printw () buffer is updated () (as the source code example shows).

+5
source
Console

Win32 does not support escape sequences. You can use the Console API .

A small example that clears the first 3 characters in (0, 0) from the console

 #include <windows.h> int main() { HANDLE hOutput = ::GetStdHandle(STD_OUTPUT_HANDLE); COORD coord = {0,0}; ::SetConsoleCursorPosition(hOutput, coord); char buff[] = " "; ::WriteConsoleA(hOutput, buff, 3, NULL, NULL); return 0; } 

If you don't like the Console API and want to use the ncurses analogue, see there .

+2
source

If you want to control full screen, curses is the way to go. Otherwise, you can do a lot by simply using escape sequences; see http://en.wikipedia.org/wiki/ANSI_escape_code , for example. (Historically, such sequences ranged from one terminal to then, and curses were originally a way around this. Today, ANSI escape codes are pretty universal for the console window under the window system, both the console window and xterm use both windows.)

In addition to encapsulating actual sequences, curses supports character-oriented input, with or without echo. This is more difficult to do without curses, and is still very unbearable.

+1
source

All Articles