如何将输出对齐到屏幕中心 - C++?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16980361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:48:04  来源:igfitidea点击:

How to align output to center of screen - C++?

c++windowsformattingconsole-application

提问by ashu

I am working on a C++ console app. I want to execute and print all the stuffs at the center of app window screen ( horizontally + vertically) as shown below.

我正在开发 C++ 控制台应用程序。我想在应用程序窗口屏幕中心(水平+垂直)执行和打印所有内容,如下所示。

 --------------------------------
|                               |
|                               |
|         User : xyz            |
|         Pass : ****           |
|                               |
|                               |
 --------------------------------

I want to run my whole program as how above. Is there any way to do so? Any help or suggestion would be appreciated.

我想按照上面的方式运行我的整个程序。有什么办法吗?任何帮助或建议将不胜感激。

采纳答案by Losiowaty

If you want to keep your application in a console, but want to do some layouts I'd recommend using ncursesas it gives you more control on where you print, and also gives you a possibility to create menus, message boxes and other GUI-like stuff.

如果您想将应用程序保留在控制台中,但想要进行一些布局,我建议您使用ncurses,因为它可以让您更好地控制打印位置,还可以让您创建菜单、消息框和其他 GUI-喜欢的东西。

回答by Boris

Example for WIN:

赢的例子:

#include <windows.h>

int main()
{ 
    HANDLE screen = GetStdHandle( STD_OUTPUT_HANDLE );

    COORD max_size = GetLargestConsoleWindowSize( screen );

    char s[] = "Hello world!";

    COORD pos;
    pos.X = (max_size.X - sizeof(s) ) / 2;
    pos.Y = max_size.Y / 2;
    SetConsoleCursorPosition( screen, pos );

    LPDWORD written;
    WriteConsole( screen, s, sizeof(s), written, 0 );

    return 0;
}

回答by Mark Garcia

How about this (LIVE EXAMPLE):

这个怎么样(现场示例):

#include <iostream>
#include <string>
#include <vector>


void centerify_output(std::string str, int num_cols) {
    // Calculate left padding
    int padding_left = (num_cols / 2) - (str.size() / 2);

    // Put padding spaces
    for(int i = 0; i < padding_left; ++i) std::cout << ' ';

    // Print the message
    std::cout << str;
}


int main() {
    std::vector<std::string> lines = {
        "---------------------------------",
        "|                               |",
        "|                               |",
        "|         User : xyz            |",
        "|         Pass : ****           |",
        "|                               |",
        "|                               |",
        "---------------------------------",
    };

    int num_cols = 100;

    // VIRTUAL BORDER
    std::cout << std::endl;
    for(int i = 0; i < num_cols; ++i) std::cout << ' ';
    std::cout << '|' << std::endl;

    // OUTPUT
    for(int i = 0; i < lines.size(); ++i) {
        centerify_output(lines[i], num_cols);
        std::cout << std::endl;
    }

    // VIRTUAL BORDER
    std::cout << std::endl;
    for(int i = 0; i < num_cols; ++i) std::cout << ' ';
    std::cout << '|' << std::endl;
}

You get the idea. When centering the output vertically, you just put padding end lines at the top of the console.

你明白了。当垂直居中输出时,您只需在控制台顶部放置填充结束线。