控制台应用程序中的C ++中心文本?

时间:2019-02-08 17:35:11

标签: c++ winapi

我正在创建一个控制台应用程序,我需要将文本居中。 这是我的代码:

this.network.type !== 'none'

但是没有显示“ word / msg”。谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这行吗?

#include <windows.h>

bool centerText(const char* word)
{
    HANDLE hFile = CreateFileW(L"CONOUT$", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);

    if (hFile)
    {
        short len = static_cast<short>(strlen(word));

        CONSOLE_SCREEN_BUFFER_INFO  csbi;
        GetConsoleScreenBufferInfo(hFile, &csbi);
        csbi.dwCursorPosition.X = (csbi.srWindow.Left - len)/2;
        SetConsoleCursorPosition(hFile, csbi.dwCursorPosition);

        puts(word);

        CloseHandle(hFile);
        return true;
    }

    return false;
}