C + + refresh screen

Posted by harkonenn on Tue, 08 Mar 2022 18:59:32 +0100

C + + refresh screen

Method 1: clear the text and reprint

Use the system function to clear the text of the console, and then print again to achieve the effect of refreshing the screen

There is no need to talk more about the system function. Just go to the code:

#include <bits/stdc++.h>
#include <windows.h>			//Sleep()
using namespace std;

int main() {
	for(int i=1;i<=10;i++) {
		system("cls")
		for(int j=1;j<=i;j++)cout<<" ";
		cout<<"Hello, world"<<endl;
		Sleep(100);
	}
	return 0;
}

Run the code and you can see the caption of hello and world moving on the screen. It seems that there is no problem. However, if you change the code to this:

#include <bits/stdc++.h>
#include <windows.h>			//Sleep()
using namespace std;

int main() {
	for(int i=1;i<=10;i++) {
		system("cls");
		for(int k=1;k<=10;k++) {
			for(int j=1;j<=i;j++)cout<<" ";
			cout<<"Hello, world"<<endl;
		}
		Sleep(100);
	}
	return 0;
}

When running the code, the flicker when the text is cleared is clearly visible. Unless the performance of your computer is particularly good, it looks really hard. We have to change another method.

Method 2: directly overwrite the original text

If it doesn't work to clear the screen, just don't clear it.

Experienced people must know that the backspace in the console has no insertion mode, which means that we can overwrite the useless text with the text that needs to be printed.

First of all, we need to move the cursor to the left corner of the console, that is, we need to move the cursor to the first column of the console C + + move cursor.

void gotoxy(int x, int y) {
	COORD pos = {x,y};
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
}

Remember to add header file:

#include <windows.h>

Then, you can start printing text, and overwrite the previously printed text with the text printed this time.
Put the code here:

#include <bits/stdc++.h>
#include <windows.h>
using namespace std;

void gotoxy(int x, int y) {					//Move Cursor 
	COORD pos = {x,y};
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
}

int main() {
	for(int i=1;i<=10;i++) {
		gotoxy(0,0);						//Move the cursor to the upper left corner of the console
		for(int k=1;k<=10;k++) {
			for(int j=1;j<=i;j++)cout<<" ";
			cout<<"Hello, world"<<endl;
		}
		Sleep(100);
	}
	return 0;
}

Run the code and the flicker is finally gone, which shows that we succeeded.

Expansion: local refresh

You may feel that the refresh speed is too slow. You can try local refresh.

The so-called local refresh refers to clearing the part that needs to be cleared and retaining the part that does not need to be cleared, so as to speed up the speed.

Let's take a look. Each time we print, we will move hello and world back one grid. Therefore, the part to be cleared is actually the character in front of hello and world (other characters will be overwritten by hello and world), that is, H. We'll just get rid of H.

Attach complete code:

#include <bits/stdc++.h>
#include <windows.h>
using namespace std;

void gotoxy(int x, int y) {					//Move Cursor 
	COORD pos = {x,y};
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
}

int main() {
	for(int i=1;i<=10;i++) {
		for(int k=1;k<=10;k++) {
			gotoxy(i-1,k-1);				//Move the cursor to row k, column i
			cout<<"Hello, world";
			if(i>1) {						//Do it when i is greater than 1, otherwise the coordinate will become negative
				gotoxy(i-2,k-1);			//Move the cursor to the previous character
				cout<<' ';					//Clear that character
			}
		}
		Sleep(100);
	}
	return 0;
}

Summary

Through the study of this chapter, you must have learned the method of quickly clearing the screen.
If this article is helpful to you, be sure to point out a compliment!

Topics: C++ cmd