C ++控制台屏幕尺寸 [英] c++ console screen size

查看:171
本文介绍了C ++控制台屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用C ++在College中学习一些东西,并且我和老师开始了关于如何将文本实际居中到输出屏幕的讨论。所以我的建议是使用 setw ,但要获取字符串的长度和控制台屏幕的 size ,请执行算法和BAM,我们将文本真正居中。他说,屏幕尺寸为 80 ,但是屏幕可以调整大小,无论输出居中还是用户开始调整大小,这都不起作用。我有一个小问题,如何获得控制台屏幕的实际大小?

So I'm learning some stuff in College on C++, and the teacher and I got into a discussion on how to actually center text to the output screen. So my suggestion was to use setw but get the length of the string and the size of the console screen, do the algorithm and BAM we have truly centered text. He says the screen size is 80 but the screen can be resized, which doesn't work no matter what if the output is centered the the user starts resizing. Just a minor question I have, how to get the actual size of the console screen?

#include <iostream>
#include <string>
using namespace std;

const int SCR_SIZE = 80;//some way of telling size

int main(){
   string randomText = "Hello User!";
   cout << setw( ( (80 / 2) + (randomText.length() / 2 ) ) ) 
        << randomText 
        << endl;

   return 0;
}

稍作搜索后发现这一点

 #include <cstdlib>

 system("MODE CON COLS=25 LINES=22");

是否可以在执行时设置它,以确保我的大小是我想要的大小?请仔细阅读一下,如果实际上是c ++库,我不是100%肯定的

Would that work to set it on execution to make sure my size is what I want it to be? Just read through it so I'm not 100% positive if that in fact is a c++ library

推荐答案

您可以 #include< windows.h> 并调用 GetConsoleScreenBufferInfo 。要使用此功能,您需要一个标准输出流的Windows句柄,您可以使用 GetStdHandle

You can #include <windows.h> and call GetConsoleScreenBufferInfo. To use this, you'll need a Windows handle to your standard output stream, which you can retrieve with GetStdHandle.

请注意,生成的代码将是Windows特定的(而您当前的代码是可移植的,因此它可以在Linux,Mac OS,* BSD等上正常运行)。

Be aware that the resulting code will be Windows-specific (whereas your current code is portable, so it should run fine on Linux, Mac OS, *BSD, etc.)

这篇关于C ++控制台屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆